//domain层javaBean
package cn.code.dbUtils; public class Stu { private String number; private String name; private int age; private String gender; public Stu() { super(); // TODO Auto-generated constructor stub } public Stu(String number, String name, int age, String agender) { super(); this.number = number; this.name = name; this.age = age; this.gender = agender; } public String getNumber() { return number; } public void setNumber(String number) { this.number = number; } 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; } public String getGender() { return gender; } public void setGender(String agender) { this.gender = agender; } @Override public String toString() { return "Stu [number=" + number + ", name=" + name + ", age=" + age + ", gender=" + gender + "]"; } }
//工具类DBUtils
package cn.code.dbUtils; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import javax.sql.DataSource; public class DBUtilsDemo { DataSource datasource; public DBUtilsDemo(DataSource datasource) { super(); this.datasource = datasource; } public DBUtilsDemo() { super(); // TODO Auto-generated constructor stub } public int update(String sql,Object...params){ Connection con=null; PreparedStatement ps=null; try{ //得到链接 con= datasource.getConnection(); //得到执行sql语句对象 ps = con.prepareStatement(sql); //给sql语句传参赋值 initparams(ps,params); //返回执行语句影响行数 return ps.executeUpdate(); }catch(Exception e){ throw new RuntimeException(e); }finally{ try{ if(ps!=null)ps.close(); if(con!=null)con.close(); }catch(Exception e){ throw new RuntimeException(e); } } } private void initparams(PreparedStatement ps, Object[] params) throws SQLException { for(int x=0;x<params.length;x++){ ps.setObject(x+1, params[x]); } } public <T> T query(String sql,RsHandler<T> rh,Object...params){ Connection con=null; PreparedStatement ps=null; ResultSet rs = null; try{ //获取连接 con= datasource.getConnection(); //获取执行语句对象 ps= con.prepareStatement(sql); //传递参数 initparams(ps, params); //执行sql获取结果集 rs = ps.executeQuery(); //调用接口方法handler()将结果集映射成对象返回 return rh.handler(rs); }catch(Exception e){ throw new RuntimeException(e); }finally{ try{ if(ps!=null)ps.close(); if(con!=null)con.close(); }catch(Exception e){ throw new RuntimeException(e); } } } }
//接口
package cn.code.dbUtils; import java.sql.ResultSet; import java.sql.SQLException; public interface RsHandler<T> { public T handler(ResultSet rs)throws SQLException; }
//测试类
package cn.code.dbUtils; import java.sql.ResultSet; import java.sql.SQLException; import org.junit.Test; import cn.code.jdbc.JDBCUtils2; public class Demo { @Test public void test(){ Stu s = find("0001"); System.out.println(s); //addStu(); } public void addStu(){ //创建对象给出连接池 DBUtilsDemo dd = new DBUtilsDemo(JDBCUtils2.getDataSource()); //给出sql模板 String sql ="insert into stu values(?,?,?,?)"; //给出参数 Object[]params={"0001","zhouxingchi",55,"boy"}; //执行增删改 dd.update(sql, params); } public Stu find(String number){ DBUtilsDemo dd = new DBUtilsDemo(JDBCUtils2.getDataSource()); String sql ="select * from stu where number=?"; Object[]params={number}; //实现RsHandler接口,内部类 RsHandler<Stu> rh = new RsHandler<Stu> (){ @Override public Stu handler(ResultSet rs) throws SQLException { if(!rs.next())return null; Stu stu = new Stu(); stu.setNumber(rs.getString("number")); stu.setName(rs.getString("name")); stu.setAge(rs.getInt("age")); stu.setGender(rs.getString("gender")); return stu; } }; return (Stu)dd.query(sql, rh, params); } }