1、DBUtils介紹 apache
什麼是dbutils,它的做用
DBUtils是java編程中的數據庫操做實用工具,小巧簡單實用。
用前導包!!!DBUtils包!!!
2、DBUtils的三個核心對象
> QueryRunner類
>ResultSetHandler接口
> DBUtils類
QueryRunner類
QueryRunner中提供對sql語句操做的API.
它主要有三個方法
query() 用於執行select
update() 用於執行insert update delete
batch() 批處理
ResultSetHandler接口
用於定義select操做後,怎樣封裝結果集.
DbUtils類
它就是一個工具類,定義了關閉資源與事務處理的方法
3、QueryRunner對象
1.1 構造函數:
> new QueryRunner(); 它的事務能夠手動控制。
也就是說此對象調用的方法(如:query、update、batrch)參數中要有Connection對象。
> new QueryRunner(DataSource ds); 它的事務是自動控制的。一個sql一個事務。
此對象調用的方法(如:query、update、batrch)參數中無需Connection對象。
1.2 使用案例
private void doQueryAll() {
//ArrayHandler
// QueryRunner queryRunner = new QueryRunner(UtilC3P0.getDataSource());
// try {
// Object[] objects = queryRunner.query("select * from user", newArrayHandler());
// for(Object object : objects) {
// System.out.println("object = " + object);
//
// }
// } catch(SQLException e) {
// e.printStackTrace();
// }
//--------------------------------------------------------------------------------------------------------
//ArrayListHandler
// QueryRunner queryRunner = new QueryRunner(UtilC3P0.getDataSource());
// try {
// List<Object[]> list = queryRunner.query("select * fromuser", new ArrayListHandler());
// for(Object[] objects : list) {
// for (Object object : objects) {
// System.out.print(object+"\t");
// }
// System.out.println();
//
// }
// } catch (SQLException e) {
// e.printStackTrace();
// }
//--------------------------------------------------------------------------------------------------------
//ColumnListHandler
// QueryRunner queryRunner = new QueryRunner(UtilC3P0.getDataSource());
//
// try {
// List<Object> list = queryRunner.query("select * fromuser", new ColumnListHandler(1));
// for(Object o : list) {
// System.out.println(o);
//
// }
// } catch(SQLException e) {
// e.printStackTrace();
// }
//--------------------------------------------------------------------------------------------------------
//KeyedHandler
//大map的key,是一行數據的一個標識,這個標識取當前行數據的某一列值
//小map就是當前行的數據 ,以鍵值對形式封裝
// QueryRunner queryRunner = new QueryRunner(UtilC3P0.getDataSource());
// try {
// Map<Object, Map<String, Object>> map =queryRunner.query("select * from user", new KeyedHandler(1));
// Set<Object> objects = map.keySet();
// for(Object object : objects) {
// System.out.println("key "+object);
// Map<String, Object> stringObjectMap = map.get(object);
// System.out.println("value: "+stringObjectMap);
// }
//
// } catch(SQLException e) {
// e.printStackTrace();
// }
//--------------------------------------------------------------------------------------------------------
//經常使用1-----ScalarHandler查詢某行某列數據
// QueryRunner queryRunner = new QueryRunner(UtilC3P0.getDataSource());
// try {
// Object o = queryRunner.query("select money from user", newScalarHandler(1));
// System.out.println(o);
// } catch(SQLException e) {
// e.printStackTrace();
// }
//--------------------------------------------------------------------------------------------------------
//經常使用2-----BeanHandler 查詢一行數據(一個對象)
// QueryRunner queryRunner = new QueryRunner(UtilC3P0.getDataSource());
// try {
// Useruser = queryRunner.query("select * from user whereusername='dahuang1'", new BeanHandler<User>(User.class));
// System.out.println(user);
// } catch(SQLException e) {
// e.printStackTrace();
// }
//--------------------------------------------------------------------------------------------------------
//經常使用3-----BeanListHandler 查詢多行數據(一個對象集合)
QueryRunnerqueryRunner = new QueryRunner(UtilC3P0.getDataSource());
try {
List<User> users = queryRunner.query("select * from userwhere money>?", new BeanListHandler<User>(User.class),50);
System.out.println(users);
} catch(SQLException e) {
e.printStackTrace();
}
}
//更新操做
private voiddoUpdate() {
QueryRunnerqueryRunner = new QueryRunner(UtilC3P0.getDataSource());
try {
queryRunner.update("update user set money=? where username=?", 99999, "dahuang1");
} catch(SQLException e) {
e.printStackTrace();
}
}
//刪除操做
private voiddoDelete1() {
QueryRunnerqueryRunner = new QueryRunner(UtilC3P0.getDataSource());
try {
queryRunner.update("delete from user where username=?","hehedada");
} catch(SQLException e) {
e.printStackTrace();
}
}
//插入操做
private voiddoInsert() {
// QueryRunner queryRunner=new QueryRunner(UtilC3P0.getDataSource());
// try {
// queryRunner.update("insert into user values(?,?,?,?)","hehedada","123456","nan",123.45);
// } catch(SQLException e) {
// e.printStackTrace();
// }
QueryRunnerqueryRunner = new QueryRunner();
Connectionconnection = UtilC3P0.openConn();
try {
queryRunner.update(connection, "insert into uservalues(?,?,?,?)", "hehedada22", "123456","nan", 123.45);
} catch(SQLException e) {
e.printStackTrace();
} finally {
try {
connection.close();
} catch(SQLException e) {
e.printStackTrace();
}
}
}
1.3 批處理操做---batch
QueryRunner queryRunner=newQueryRunner(UtilC3P0.getDataSource());
Object[][] objects=new Object[100][];
for (int i = 0; i < objects.length;i++) {
objects[i]=new Object[]{"大黃"+i,"123456"+i};
}
try {
queryRunner.batch("INSERT INTO user(username,userpwd) VALUES(?,?)",objects);
} catch (SQLException e) {
e.printStackTrace();
}
4、ResultSetHandler接口
ResultSetHandler下的全部結果處理器
1. ArrayHandler:適合取1條記錄。把該條記錄的每列值封裝到一個數組中Object[]
QueryRunnerqr = new QueryRunner(C3P0Util.getDataSource());
Object[]arr = qr.query("select * fromusers", new ArrayHandler());
for(Object o : arr) {
System.out.println(o);
}
2.ArrayListHandler:適合取多條記錄。把每條記錄的每列值封裝到一個數組中Object[],把數組封裝到一個List中
QueryRunnerqr = new QueryRunner(C3P0Util.getDataSource());
List<Object[]>query = qr.query("select * from users", new ArrayListHandler());
for(Object[] os : query) {
for(Object o : os) {
System.out.println(o);
}
System.out.println("--------------");
}
3.ColumnListHandler:取某一列的數據。封裝到List中。
QueryRunnerqr = new QueryRunner(C3P0Util.getDataSource());
List<Object>list = qr.query("select username,password from users", newColumnListHandler(1));
for(Object o : list) {
System.out.println(o);
}
4.KeyedHandler:取多條記錄,每一條記錄封裝到一個Map中,再把這個Map封裝到另一個Map中,key爲指定的字段值。
QueryRunnerqr = new QueryRunner(C3P0Util.getDataSource());
//大Map的key是表中的某列數據,小Map中的key是表的列名,因此大Map的key是Object類型,小Map的key是String類型
Map<Object,Map<String,Object>>map = qr.query("select * from users", new KeyedHandler(1));
for(Map.Entry<Object, Map<String,Object>> m : map.entrySet()) {
System.out.println(m.getKey());//大Map中key值就是id列的值
for(Map.Entry<String, Object> mm : m.getValue().entrySet()) {
System.out.println(mm.getKey()+"\t"+mm.getValue());//取出小Map中的列名和列值
}
System.out.println("---------------------");
}
5.MapHandler:適合取1條記錄。把當前記錄的列名和列值放到一個Map中
QueryRunner qr = newQueryRunner(C3P0Util.getDataSource());
Map<String,Object>map = qr.query("select * from users where id=?", newMapHandler(),20);
for(Map.Entry<String, Object> m : map.entrySet()) {
System.out.println(m.getKey()+"\t"+m.getValue());
}
6.MapListHandler:適合取多條記錄。把每條記錄封裝到一個Map中,再把Map封裝到List中
QueryRunner qr = newQueryRunner(C3P0Util.getDataSource());
List<Map<String,Object>>list = qr.query("select * from users", new MapListHandler());
for(Map<String, Object> map : list) {
for(Map.Entry<String, Object> m : map.entrySet()) {
System.out.println(m.getKey()+"\t"+m.getValue());
}
System.out.println("---------------");
}
7.ScalarHandler:適合取單行單列數據(經常使用)
QueryRunner qr = newQueryRunner(C3P0Util.getDataSource());
Objecto = qr.query("select count(*) from users", new ScalarHandler(1));
System.out.println(o.getClass().getName());
8.BeanHandler 返回咱們所須要的實體(經常使用)
QueryRunnerqr = new QueryRunner(C3P0Util.getDataSource());
Useruser = qr.query("select * from users where id=?", newBeanHandler<User>(User.class),1);
System.out.println(user);
9.BeanListHandler 返回一組實體(經常使用)
QueryRunnerqr = new QueryRunner(C3P0Util.getDataSource());
List<User>list = qr.query("select * from users where id=?", newBeanListHandler<User>(User.class),1);
System.out.println(list.size());
java