0x00 教程内容
- 环境准备
- 编写代码
- 测试
如果没有基础,或者环境没有准备,请参考此两篇教程:
0x01 环境准备
1. 表数据准备
给数据库添加几条数据:
insert into student(name,age) values ("邵奈一",30); insert into student(name,age) values ("邵奈一",28);
2. 项目结构准备
新建相应的包以及类(注意新建的文件类型)
0x02 编写代码
1. Student实体类代码
package com.shaonaiyi.domain; /** * @Auther: shaonaiyi@163.com * @Date: 2021/1/13 15:17 * @Description: 学生实体类 */ public class Student { private Integer id; private String name; private Integer age; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } }
2. StudentDAO访问接口
package com.shaonaiyi.dao; import com.shaonaiyi.domain.Student; import java.util.List; /** * @Auther: shaonaiyi@163.com * @Date: 2021/1/13 15:19 * @Description: Student访问接口 */ public interface StudentDAO { //1、查询所有的学生 public List<Student> query(); //2、新增学生 public Integer save(Student student); //3、删除学生 public Integer delete(Integer id); //4、修改学生 public Integer update(Student student); }
3. StudentDAOImpl访问接口实现类
package com.shaonaiyi.dao; import com.shaonaiyi.domain.Student; import com.shaonaiyi.utils.JDBCUtil; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.util.ArrayList; import java.util.List; /** * @Auther: shaonaiyi@163.com * @Date: 2021/1/13 15:22 * @Description: Student访问接口实现类 */ public class StudentDAOImpl implements StudentDAO { @Override public List<Student> query() { List<Student> studentList = new ArrayList<>(); Connection connection = null; PreparedStatement preparedStatement = null; ResultSet resultSet = null; try { connection = JDBCUtil.getConnection(); preparedStatement = connection.prepareStatement("select * from student"); resultSet = preparedStatement.executeQuery(); Student student = null; while (resultSet.next()) { int id = resultSet.getInt("id"); String name = resultSet.getString("name"); int age = resultSet.getInt("age"); student = new Student(); student.setId(id); student.setName(name); student.setAge(age); studentList.add(student); } } catch (Exception e) { e.printStackTrace(); } finally { JDBCUtil.release(resultSet, preparedStatement, connection); } return studentList; } @Override public Integer save(Student student) { Connection connection = null; PreparedStatement preparedStatement = null; ResultSet resultSet = null; Integer result = 0; try { connection = JDBCUtil.getConnection(); preparedStatement = connection.prepareStatement("insert into student(name,age) values (?,?)"); preparedStatement.setString(1, student.getName()); preparedStatement.setInt(2,student.getAge()); result = preparedStatement.executeUpdate(); } catch (Exception e) { e.printStackTrace(); } finally { JDBCUtil.release(resultSet, preparedStatement, connection); } return result; } @Override public Integer delete(Integer id) { Connection connection = null; PreparedStatement preparedStatement = null; ResultSet resultSet = null; Integer result = 0; try { connection = JDBCUtil.getConnection(); preparedStatement = connection.prepareStatement("delete from student where id = " + id); result = preparedStatement.executeUpdate(); } catch (Exception e) { e.printStackTrace(); } finally { JDBCUtil.release(resultSet, preparedStatement, connection); } return result; } @Override public Integer update(Student student) { Connection connection = null; PreparedStatement preparedStatement = null; ResultSet resultSet = null; Integer result = 0; try { connection = JDBCUtil.getConnection(); preparedStatement = connection.prepareStatement("update student set age = " + student.getAge() + " where name = '" + student.getName() + "'"); result = preparedStatement.executeUpdate(); } catch (Exception e) { e.printStackTrace(); } finally { JDBCUtil.release(resultSet, preparedStatement, connection); } return result; } }