publicclass Demo3 {
@Test
publicvoidfun1(){
Stu s = new Stu(322,"bb1b",123.1);
addStu(s);
Stu s1 = load(322);
System.out.println(s1);
}
publicvoidaddStu(Stu stu){
QR qr = new QR(JdbcUtils.getDataSource());//创建对象时给出连接池
String sql ="insert into student values(?,?,?)";//给出sql模版//给出参数
Object[] params = {stu.getSid(),stu.getSname(),stu.getSal()};
//调用update执行增、删、改
qr.update(sql, params);
}
public Stu load(int sid){
QR qr = new QR(JdbcUtils.getDataSource());//创建对象时给出连接池
String sql ="select * from student where sid=?";//给出sql模版
Object[] params ={sid};
RsHandler<Stu> rh = new RsHandler() {
@Override
public Object handle(ResultSet rs) throws SQLException {
if(!rs.next()) returnnull;
Stu stu = new Stu();
stu.setSid(rs.getInt("sid"));
stu.setSname(rs.getString("sname"));
stu.setSal(rs.getDouble("sal"));
return stu;
}
};
return (Stu) qr.query(sql, rh, params);
}
}
publicclassDemo4 {@Testpublicvoidfun1() throws SQLException{
//创建QueryRunner,需要提供数据库连接池对象
QueryRunner qr = new QueryRunner(JdbcUtils.getDataSource());
//给出sql模版
String sql ="insert into student values(?,?,?)";
//给出参数
Object[] params = {333,"lisiw",132.1};
//
qr.update(sql,params);
}
@Testpublicvoidfun2() throws SQLException{
QueryRunner qr = new QueryRunner(JdbcUtils.getDataSource());
String sql = "select * from student where sid =?";
Object[] params = {333};
// ResultSetHandler rsh = new ResultSetHandler() {// @Override// public Object handle(ResultSet arg0) throws SQLException {// // return null;// }// // };//执行query()方法,需要给出结果集处理器,即ResultSetHandler的实现类对象//我们给的是BeanHandler,它实现了ResultSetHandler//它需要一个类型,然后会把rs中的数据封装到指定类型的javaBean对象中,然后返回javabean
Stu stu = qr.query(sql, new BeanHandler<Stu>(Stu.class),params);
System.out.println(stu);
}
/**
* BeanListHandler的应用,是多行处理器
* 每行对应一个Stu对象
* @throws SQLException
*/@Testpublicvoidfun3() throws SQLException{
QueryRunner qr = new QueryRunner(JdbcUtils.getDataSource());
String sql = "select * from student";
List<Stu> stuList = qr.query(sql,new BeanListHandler<Stu>(Stu.class));
System.out.println(stuList);
}
/**
* MapHandler,是单行处理器,把一行转换成一个Map对象
* @throws SQLException
*/@Testpublicvoidfun4() throws SQLException{
QueryRunner qr = new QueryRunner(JdbcUtils.getDataSource());
String sql = "select * from student where sid = ?";
Object[] params = {333};
Map map = qr.query(sql,new MapHandler(),params);
System.out.println(map);
}
/**
* MapListHandler是多行处理器,把每行都转换成一个Map,即List<Map>
*/@Testpublicvoidfun5() throws SQLException{
QueryRunner qr = new QueryRunner(JdbcUtils.getDataSource());
String sql = "select * from student ";
List<Map<String, Object>> mapList = qr.query(sql,new MapListHandler());
System.out.println(mapList);
}
/**
* ScalarHandler 单行单列使用时 最为合适
* @throws SQLException
*/@Testpublicvoidfun6() throws SQLException{
QueryRunner qr = new QueryRunner(JdbcUtils.getDataSource());
String sql = "select count(*) from student ";
Object obj = qr.query(sql, new ScalarHandler());
//以前返回的版本有Integer、Long、BigInteger //long cnt = (long) qr.query(sql, new ScalarHandler());//number是数字类型的父类 可以先获取number 然后获取想要的数值类型
Number cnt = (Number) qr.query(sql, new ScalarHandler());
long c = cnt.longValue();
System.out.println(obj.getClass().getName());
}
}