student类
package sdut; public class Student { private int id; private String name; private int age; public Student(int id, String name, int age) { this.id = id; this.name = name; this.age = age; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
增删改查类
package sdut; import java.awt.List; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.SQLException; public class StudentDao { Connection con; PreparedStatement pst; public Connection getConn() throws ClassNotFoundException, SQLException{ Class.forName("com.mysql.jdbc.Driver"); //2 获得数据库的连接 Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/school?useUnicde=true&characterEncoding=utf-8", "root", ""); return con; } //增加 public int addstudent(Student student) throws ClassNotFoundException, SQLException{ con = getConn(); String SQL = "insert into student values(null, ?, ?)"; //准备语句对象 pst = con.prepareStatement(SQL); pst.setString(1, student.getName()); pst.setInt(2, student.getAge()); int result = pst.executeUpdate(); pst.close(); con.close(); return result; } //删除 public int deletestudent(int id) throws ClassNotFoundException, SQLException{ con = getConn(); String SQL = "delete from student where id = ?"; //准备语句对象 pst = con.prepareStatement(SQL); pst.setInt(1, id); int result = pst.executeUpdate(); pst.close(); con.close(); return result; } //修改 //查询所有记录 public List<Student> queryAll(){ return null; } //根据主键查询记录 public Student queryById(int id){ return null; } }