1.描述学生信息的数据类:Student
package exam1; public class Student { private int id; private String name; private String sex; private int age; private float weight; private float hight; 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 String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public float getWeight() { return weight; } public void setWeight(float weight) { this.weight = weight; } public float getHight() { return hight; } public void setHight(float hight) { this.hight = hight; } }
2.数据库连接和关闭的工具:JavaBean
package exam1; import java.sql.*; public class DbConnect { private static String driverName="com.mysql.jdbc.Driver"; private static String userName="root"; private static String userPwd="123456"; private static String dbName="student"; static Connection getDBconnection(){ String url1="jdbc:mysql://localhost:3306/"+dbName; String url2="?user="+userName+"&password="+userPwd; String url3="&useUnicode=true&characterEncoding=UTF-8"; String url=url1+url2+url3; try{ Class.forName(driverName); Connection con=DriverManager.getConnection(url); return con; }catch(Exception e){ e.printStackTrace(); } return null; } public static void closeDB(Connection con,PreparedStatement pstmt,ResultSet rs){ try{ if(rs!=null)rs.close(); if(pstmt!=null)pstmt.close(); if(con!=null)con.close(); }catch(SQLException e){ e.printStackTrace(); } } }
3.实现数据库访问和业务逻辑的结合体DAO
package exam1; import java.sql.*; import java.util.ArrayList; import java.util.List; public class StudentDAO implements IStudentDAO { protected static final String FIELDS_INSERT="id,name,sex,age,weight,hight"; protected static String INSERT_SQL="insert into stu_info("+FIELDS_INSERT+")"+"values(?,?,?,?,?,?)"; protected static String SELECT_SQL="select"+FIELDS_INSERT+"from stu_info where id=?"; protected static String UPDATE_SQL="update stu_info set"+"id=?,name=?,sex=?,age=?,weight=?,hight=? where id=?"; protected static String DELETE_SQL="delete from stu_info where id=?"; public Student create(Student stu)throws Exception{ Connection con=null; PreparedStatement preStmt=null; ResultSet rs=null; try{ con=DbConnect.getDBconnection(); preStmt=con.prepareStatement(INSERT_SQL); preStmt.setInt(1, stu.getId()); preStmt.setString(2, stu.getName()); preStmt.setString(3, stu.getSex()); preStmt.setInt(4, stu.getAge()); preStmt.setFloat(5, stu.getWeight()); preStmt.setFloat(6, stu.getHight()); preStmt.executeUpdate(); }catch(Exception e){ }finally{ DbConnect.closeDB(con, preStmt, rs); } return stu; } @Override public void remove(Student stu) throws Exception { // TODO Auto-generated method stub } @Override public Student find(Student stu) throws Exception { Connection con=null; PreparedStatement preStmt=null; ResultSet rs=null; Student stu2=null; try{ con=DbConnect.getDBconnection(); preStmt=con.prepareStatement(SELECT_SQL); preStmt.setInt(1, stu.getId()); rs=preStmt.executeQuery(); if(rs.next()){ stu2=new Student(); stu2.setId(rs.getInt(1)); stu2.setName(rs.getString(2)); stu2.setSex(rs.getString(3)); stu2.setAge(rs.getInt(4)); stu2.setWeight(rs.getFloat(5)); stu2.setHight(rs.getFloat(6)); } }catch(Exception e){ }finally{ DbConnect.closeDB(con, preStmt, rs); } return stu2; } @Override public List<Student> findAll() throws Exception { Connection con=null; PreparedStatement prepStmt=null; ResultSet rs=null; List<Student> student=new ArrayList<Student>(); con=DbConnect.getDBconnection(); prepStmt=con.prepareStatement("select * from stu_info"); while(rs.next()){ Student stu2=new Student(); stu2.setId(rs.getInt(1)); stu2.setName(rs.getString(2)); stu2.setSex(rs.getString(3)); stu2.setAge(rs.getInt(4)); stu2.setWeight(rs.getFloat(5)); stu2.setHight(rs.getFloat(6)); student.add(stu2); } DbConnect.closeDB(con, prepStmt, rs); return student; } @Override public void update(Student stu) throws Exception { Connection con=null; PreparedStatement preStmt=null; ResultSet rs=null; try{ con=DbConnect.getDBconnection(); preStmt=con.prepareStatement(UPDATE_SQL); preStmt.setInt(1, stu.getId()); preStmt.setString(2, stu.getName()); preStmt.setString(3, stu.getSex()); preStmt.setInt(4, stu.getAge()); preStmt.setFloat(5, stu.getWeight()); preStmt.setFloat(6, stu.getHight()); int rowCount=preStmt.executeUpdate(); if(rowCount==0){ throw new Exception("Update Error:Student Id:"+stu.getId()); } }catch(Exception e){ }finally{ DbConnect.closeDB(con, preStmt, rs); } } }
4.实现业务逻辑处理的接口
package exam1; import java.util.List; public interface IStudentDAO { public abstract Student create(Student stu)throws Exception; public abstract void remove(Student stu)throws Exception; public abstract Student find(Student stu)throws Exception; public abstract List<Student>findAll()throws Exception; public abstract void update(Student stu)throws Exception; }