java 查看jdbc连接数_JDBC连接池的九种查询

package JDBC_Demo;

import java.sql.SQLException;

import java.util.List;

import java.util.Map;

import org.apache.commons.dbutils.DbUtils;

import org.apache.commons.dbutils.QueryRunner;

import org.apache.commons.dbutils.handlers.ArrayHandler;

import org.apache.commons.dbutils.handlers.ArrayListHandler;

import org.apache.commons.dbutils.handlers.BeanHandler;

import org.apache.commons.dbutils.handlers.BeanListHandler;

import org.apache.commons.dbutils.handlers.ColumnListHandler;

import org.apache.commons.dbutils.handlers.KeyedHandler;

import org.apache.commons.dbutils.handlers.MapListHandler;

import org.apache.commons.dbutils.handlers.ScalarHandler;

import com.mchange.v2.c3p0.ComboPooledDataSource;

public class TestQueryRunner2 {

public static void main(String[] args) {

ComboPooledDataSource cp=new ComboPooledDataSource();

try {

DbUtils.loadDriver("com.mysql.jdbc.Driver");

cp.setJdbcUrl("jdbc:mysql://127.0.0.1:3306/ch003?useUnicode=true&characterEncoding=UTF-8");

cp.setUser("root");

cp.setPassword("wsl123456");

cp.setInitialPoolSize(10);

cp.setMaxPoolSize(20);

cp.setMinPoolSize(2);

QueryRunner qr=new QueryRunner(cp);

String sql="select * from account";

/**

* 第一种查询

* ArrayHandler

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

*/

/*Object [] a=qr.query(sql,new ArrayHandler());

for(Object o:a) {

System.out.print(o+"\t");

}

*/

/**

* 第二种查询

* ArrayListHandler

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

*/

/*List l=qr.query(sql,new ArrayListHandler());

for(Object[] o:l) {

for(Object oo:o) {

System.out.print(oo+"\t");

}

System.out.println();

}*/

/**

* 第三种查询

* BeanHandler

* 将结果集中的第一行数据封装到一个对应的JavaBean实例中

*/

Account a=qr.query(sql,new BeanHandler(Account.class));

System.out.println(a);

/**

* 第四种查询

* BeanListHandler

* 将结果集中的每一行数据都封装到一个对应的JavaBean实例中,存放到List里。

*/

/*List ls=qr.query(sql,new BeanListHandler(Account.class));

for(Account a:ls) {

System.out.println(a);

}*/

/**

* 第五种

* ColumnListHandler

* 将结果集中某一列的数据存放到List中。

* 尖括号中的类型根据("cash")指定的列所属的类型来决定

*/

/*List l=qr.query(sql,new ColumnListHandler("cash"));

for(Integer o:l) {

System.out.println(o);

}*/

/**

* 第六种

* KeyedHandler

* 将结果集中的每一行数据都封装到一个Map里,再把这些map再存到一个map里,其key为指定的key

*/

/*Map> m=qr.query(sql,new KeyedHandler(4));

for(String a:m.keySet()) {

System.out.println(m.get(a));

}*/

/**

* 第七种

* MapHandler

*/

/*Map m = qr.query(sql, new MapHandler());

System.out.println(m.get("id")+" "+m.get("name")+" "+m.get("cash"));*/

/**

* 第八种

* MapListHandler

*/

/*List> a=qr.query(sql,new MapListHandler());

for(Map m:a) {

System.out.println(m.get("name")+" "+m.get("cash"));

}*/

/**

* 结果集第九种处理方法、

* 对于查询后,只有一个结果

*/

/*String st = "select count(*) from account";

long a = qr.query(st, new ScalarHandler());

System.out.println(a);

*/

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

public static ComboPooledDataSource getDataSource() {

ComboPooledDataSource cbp=new ComboPooledDataSource();

return null;

}

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值