Apache DbUtils工具类初学
下载jar包:(Linux下载1,Windows下载2)
http://commons.apache.org/proper/commons-dbutils/download_dbutils.cgi
Apache DbUtils跟我们学jdbc时自己写的DBUtil很相似,只不过功能更加强大,我们不需要自己写工具类了,使用Apache DbUtils可以大量节省时间精力。不难,只需要知道有哪些主要的类会用就ok
1.查询:
//以下方法全为ResultSetHandler的实现类
public static void testDbUtilQuery(){
QueryRunner runner = new QueryRunner(DataSourceUtil.getDataSortceByC3P0());
try {
//1.用数组接收查询到数据的第一行
System.out.println("1.用数组接收查询到数据的第一行");
Object[] object = runner.query("select * from student where sno > ?",new ArrayHandler(),1);
System.out.println(Arrays.toString(object));
//2.用集合数组接收查询到的所有数据
System.out.println("*****************************************");
System.out.println("2.用集合数组接收查询到的所有数据");
List<Object[]> objects = runner.query("select * from student where sno > ?",new ArrayListHandler(),1);
for (Object[] objects1 : objects) {
System.out.print(Arrays.toString(objects1));
}
//3.用对象数组接收查询到的数据的第一行
System.out.println("*****************************************");
System.out.println("3.用对象数组接收查询到的数据的第一行");
Student student = runner.query("select * from student where sno > ?",new BeanHandler<Student>(Student.class),1);
System.out.println(student);
//4.用对象数组接收查询到的所有数据
System.out.println("*****************************************");
System.out.println("4.用对象数组接收查询到的所有数据");
List<Student> students = runner.query("select * from student where sno > ?",new BeanListHandler<Student>(Student.class),1);
System.out.println(students);
//5.在4的基础上加个key(Oracle中默认的数据类型是BigDecimal)
System.out.println("*****************************************");
System.out.println("5.在4的基础上加个key");
Map<BigDecimal,Student> studentMap = runner.query("select * from student where sno > ?",new BeanMapHandler<BigDecimal,Student>(Student.class,"sno"),1);
System.out.println(studentMap);
//6.在5的基础上再加个key
System.out.println("*****************************************");
System.out.println("6.在5的基础上加个key");
Map<String,Map<String,Object>> studentMaped = runner.query("select * from student where sno > ?",new KeyedHandler<String>("sname"),1);
System.out.println(studentMaped);
//7.查询某一列
System.out.println("*****************************************");
System.out.println("7.查询某一列");
List<String> studentColumn = runner.query("select * from student where sno > ?",new ColumnListHandler<String>("sname"),1);
System.out.println(studentColumn);
//8.查询行数
System.out.println("*****************************************");
System.out.println("8.查询行数");
BigDecimal studentCount = runner.query("select count(1) from student where sno > ?",new ScalarHandler<BigDecimal>(),1);
System.out.println(studentCount);
} catch (SQLException e) {
e.printStackTrace();
}
}
//增删改
public static void testDbUtilUpdata() throws SQLException {
QueryRunner queryRunner = new QueryRunner(DataSourceUtil.getDataSortceByC3P0());
//删除
System.out.println("删除:" + queryRunner.update("delete from student where sno = ?", new Object[]{12}));
//插入
System.out.println("插入:" + queryRunner.update("insert into student values(?,?,?,?)", new Object[]{12, "郭金", 56, "阿拉尔"}));
//修改
System.out.println("修改:" + queryRunner.update("update student set sname = ? where sno = ?", new Object[]{"郭金xiugai",12}));
}
public static void main(String[] args) throws SQLException {
testDbUtilQuery();
testDbUtilUpdata();
}
运行截图:
愿你心如花木,向阳而开