php dbutils 使用,第十七天dbutils的使用------CommonsDbUtils(Apache)第三方的:只

部分源代码如下:说明已经关闭资源了。

public int update(String sql, Object... params) throws SQLException {

Connection conn = this.prepareConnection();

return this.update(conn, true, sql, params);

}

/**

* Calls update after checking the parameters to ensure nothing is null.

* @param conn The connection to use for the update call.

* @param closeConn True if the connection should be closed, false otherwise.

* @param sql The SQL statement to execute.

* @param params An array of update replacement parameters. Each row in

* this array is one set of update replacement values.

* @return The number of rows updated.

* @throws SQLException If there are database or parameter errors.

*/

private int update(Connection conn, boolean closeConn, String sql, Object... params) throws SQLException {

if (conn == null) {

throw new SQLException("Null connection");

}

if (sql == null) {

if (closeConn) {

close(conn);

}

throw new SQLException("Null SQL statement");

}

PreparedStatement stmt = null;

int rows = 0;

try {

stmt = this.prepareStatement(conn, sql);

this.fillStatement(stmt, params);

rows = stmt.executeUpdate();

} catch (SQLException e) {

this.rethrow(e, sql, params);

} finally {

close(stmt);

if (closeConn) {

close(conn);

}

}

return rows;

}

}

DBUtils框架提供的结果处理器例子:

public class ResultSetHandlerDemo {

private QueryRunner qr = new QueryRunner(DBCPUtil.getDataSource());

//ArrayHandler:把结果集中的第一行数据转成对象数组。

@Test

public void test1() throws SQLException{

//数组中的元素就是记录的每列的值

Object[] objs = qr.query("select * from account where id=?", new ArrayHandler(), 1);

for(Object obj:objs)

System.out.println(obj);

}

//ArrayListHandler:把结果集中的每一行数据都转成一个数组,再存放到List中。

@Test

public void test2() throws SQLException{

//数组中的元素就是记录的每列的值

Listlist = qr.query("select * from account", new ArrayListHandler());

for(Object[] objs:list){

System.out.println("------------------");

for(Object obj:objs){

System.out.println(obj);

}

}

}

//ColumnListHandler:将结果集中某一列的数据存放到List中。 投影

@Test

public void test3() throws SQLException{

//数组中的元素就是记录的每列的值

Listlist = qr.query("select * from account", new ColumnListHandler("name"));

for(Object objs:list){

System.out.println(objs);

}

}

//KeyedHandler(name):将结果集中的每一行数据都封装到一个Map里,再把这些map再存到一个map里,其key为指定的key。

@Test

public void test4() throws SQLException{

Map> bmap= qr.query("select * from account", new KeyedHandler("id"));

for(Map.Entry> bme:bmap.entrySet()){

System.out.println("-----------------");

for(Map.Entrylme:bme.getValue().entrySet()){

System.out.println(lme.getKey()+"="+lme.getValue());

}

}

}

//MapHandler:将结果集中的第一行数据封装到一个Map里,key是列名,value就是对应的值

@Test

public void test5() throws SQLException{

Mapmap = qr.query("select * from account where id=?", new MapHandler(),1);

for(Map.Entrylme:map.entrySet()){

System.out.println(lme.getKey()+"="+lme.getValue());

}

}

//MapListHandler:将结果集中的每一行数据都封装到一个Map里,然后再存放到List

@Test

public void test6() throws SQLException{

List> list= qr.query("select * from account", new MapListHandler());

for(Mapmap:list){

System.out.println("-----------------");

for(Map.Entrylme:map.entrySet()){

System.out.println(lme.getKey()+"="+lme.getValue());

}

}

}

//ScalarHandler :只有一条记录的投影查询 select count(*) from account;

@Test

public void test7() throws SQLException{

//Object obj = qr.query("select * from account where id=?", new ScalarHandler("name"),1);

//System.out.println(obj);

Object obj = qr.query("select count(*) from account", new ScalarHandler(1));

int num = ((Long)obj).intValue();

System.out.println(num);

}

}

本文原创发布php中文网,转载请注明出处,感谢您的尊重!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值