一、实现步骤
- 新建entity实体层
- 新建User类
- 新建dao包
- dao包下新建UserDao接口
- User层代码:
- UserDao接口
- UserDaoImpl类
- 记得关闭,调用DBUtils.CloseAll()
- test测试代码
运行成功!
二、增
以下代码写在XXXDaoImpl层,以Person类为列:
//向Person表中插入数据,定义实体类Person,提供构造方法和set、get方法
public static int insert(Person person){
Connection connection = null;
PreparedStatement preparedStatement = null;
String sql = "insert into Person(name,age,sex,bornDate) values(?,?,?,?);";
connection = DBUtils.getConnection();
preparedStatement = connection.preparedStatement(sql);
preparedStatement.setString(1,person.getName());
preparedStatement.setInt(2,person.getAge());
preparedStatement.setString(3,person.getSex());
preparedStatement.setDate(4,DateUtils.UtilToStr(Person.getBornDate()));
int result = preparedStatement.executeUpdate();
return result;
DBUtils.closeAll(connection,preparedStatement,null);
}.
三、删
//删除Person表中的数据
public static int delete(Person person){
Connection connection = null;
PreparedStatement preparedStatement = null;
String sql = "delete * from Person where id = ?;";
connection = DBUtils.getConnection();
preparedStatement = connection.preparedStatement(sql);
preparedStatement.setString(1,person.getId());
int result = preparedStatement.executeUpdate();
return result;
DBUtils.closeAll(connection,preparedStatement,null);
}
四、改
//修改Person表中数据,当id等于指定时;
public static int Update(Person person){
Connection connection = null;
PreparedStatement preparedStatement = null;
String sql = "update Person set name=?,age=?,sex=?,bornDate=? where id=?;";
connection = DBUtils.getConnection();
preparedStatement = connection.preparedStatement(sql);
preparedStatement.setString(1,person.getName());
preparedStatement.setInt(2,person.getAge());
preparedStatement.setString(3,person.getSex());
preparedStatement.setDate(4,DateUtils.UtilToStr(Person.getBornDate()));
preparedStatement.setId(5,person.getId());
int result = preparedStatement.executeUpdate();
return result;
DBUtils.closeAll(connection,preparedStatement,null);
}
五、查单行
public static Person select(int id){
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
Person person = null;
String sql = "select * from Person where id =?;";
connection = DBUtils.getConnection();
preparedStatement = connection.preparedStatement(sql);
preparedStatement.setId(1,int);
resultSet = preparedStatement.executeQuery();
if(resultSet.next()){
int id = resultSet.getInt("id");
String naem = resultSet.getString("name");
int age = resultSet.getInt("age");
String sex = resultSet.getString("sex");
String bornDate = resultSet.getDate("bornDate");
person = new Person(id,name,age,sex,bornDate);
return person;
}
DBUtils.closeAll(connection,preparedStatement,resultSet);
}
六、查全部
public static List<Person> selectAll(){
List<Person> list = new ArrayList<>();
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
Person person = null;
String sql = "select * from Person;";
connection = DBUtils.getConnection();
preparedStatement = connection.preparedStatement(sql);
resultSet = preparedStatement.executeQuery();
if(resultSet.next()){
int id = resultSet.getInt("id");
String naem = resultSet.getString("name");
int age = resultSet.getInt("age");
String sex = resultSet.getString("sex");
String bornDate = resultSet.getDate("bornDate");
person = new Person(id,name,age,sex,bornDate);
list.add(person);
return list;
}
DBUtils.closeAll(connection,preparedStatement,resultSet);
}