package fuxi;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
public class Dao {
public int insert(Person person){
Connection connection=null;
PreparedStatement preparedStatement=null;
String sql="insert into Preson (name,age,email,address) values(?,?,?,?)";
try {
connection=DBUtils.getConnection();
preparedStatement=connection.prepareStatement(sql);
preparedStatement.setString(1, person.getName());
preparedStatement.setInt(2,person.getAge());
preparedStatement.setString(3, person.getEmail());
preparedStatement.setString(4, person.getAddress());
int result=preparedStatement.executeUpdate();
return result;
} catch (SQLException e) {
e.printStackTrace();
}
finally {
DBUtils.closeAll(connection,preparedStatement,null);
}
return 0;}
public int update(Person person){
Connection connection=null;
PreparedStatement preparedStatement=null;
String sql="update Person set(name=?,age=?,email=?,address=?);";
try {
connection=DBUtils.getConnection();
preparedStatement=connection.prepareStatement(sql);
preparedStatement.setString(1, person.getName());
preparedStatement.setInt(2,person.getAge());
preparedStatement.setString(3, person.getEmail());
preparedStatement.setString(4, person.address);
int result=preparedStatement.executeUpdate();
return result;
} catch (SQLException e) {
e.printStackTrace();
}
finally {
DBUtils.closeAll(connection,preparedStatement,null);
}
return 0;}
public int delete(int id){
Connection connection=null;
PreparedStatement preparedStatement=null;
String sql="delete from Person where id=?;";
try {
connection=DBUtils.getConnection();
preparedStatement=connection.prepareStatement(sql);
preparedStatement.setInt(1,id);
int result=preparedStatement.executeUpdate();
return result;
} catch (SQLException e) {
e.printStackTrace();
}
finally {
DBUtils.closeAll(connection,preparedStatement,null);
}
return 0;}
public Person select(int id){
Connection connection=null;
PreparedStatement preparedStatement=null;
ResultSet resultSet=null;
Person person=null;
String sql="select * from Person where id=?;";
try {
connection=DBUtils.getConnection();
preparedStatement=connection.prepareStatement(sql);
preparedStatement.setInt(1,id);
resultSet= preparedStatement.executeQuery();
if (resultSet.next()) {
person=new Person();//
int pid=resultSet.getInt("id");
String name=resultSet.getString("name");
int age=resultSet.getInt("age");
String email=resultSet.getString("email");
String address=resultSet.getString("address");
person.setId(pid);
person.setName(name);
person.setAge(age);
person.setEmail(email);
person.setAddress(address);
}
return person;
} catch (SQLException e) {
e.printStackTrace();
}
finally {
DBUtils.closeAll(connection,preparedStatement,resultSet);
}
return null;
}
public List<Person> selectAll(){
Connection connection=null;
PreparedStatement preparedStatement=null;
ResultSet resultSet=null;
Person person=null;
List<Person>personList=null;
String sql="select * from Person;";
try {
person=new Person();
connection=DBUtils.getConnection();
preparedStatement=connection.prepareStatement(sql);
resultSet= preparedStatement.executeQuery();
while (resultSet.next()) {
int pid=resultSet.getInt("id");
String name=resultSet.getString("name");
int age=resultSet.getInt("age");
String email=resultSet.getString("email");
String address=resultSet.getString("address");
// person.setId(pid);
// person.setName(name);
// person.setAge(age);
// person.setEmail(email);
// person.setAddress(address);
// personList.add(person);
person=new Person(pid,name,age,email,address);
personList.add(person);
}
return personList;
} catch (SQLException e) {
e.printStackTrace();
}
finally{
DBUtils.closeAll(connection,preparedStatement,resultSet);
}
return null;
}
}