DBUtils

 

DBUtils

环境搭建:

1、  复制库文件

c3p0的jar及c3p0-config.xml

dbuitls的jar包

mysql驱动包

2、复制并修改DBManager

CRID

插入

public void insert() throws SQLException{

    QueryRunner runner =

new QueryRunner(DBManager_c3p0.getDataSource());

String sql = "insert into

user(id,name,password,email,birthday) values(null,?,?,?,?)";

    Object[] param={"by","sss","by@wer.com","1980-03-04"};

    runner.update(sql, param);

}

 

删除

public void delete() throws SQLException{

    QueryRunner runner =

new QueryRunner(DBManager_c3p0.getDataSource());

    String sql = "delete from user where name=?";

    Object[] param ={"by"};

    runner.update(sql, param);

}

 

查询

public User find() throws SQLException{

    QueryRunner runner =

new QueryRunner(DBManager_c3p0.getDataSource());

    String sql = "select * from user where id=?";

          

    User u = (User)runner.query(sql, newBeanHandler(User.class),1);

   

    return u;

}

 

 

public void getAll() throws SQLException{

    QueryRunner runner =

new QueryRunner(DBManager_c3p0.getDataSource());

    String sql = "select * from user";

    List<User> list = (List<User>) runner.query(sql,

newBeanListHandler(User.class));

    System.out.println(list.size());

}

批处理

 

public void bath() throws SQLException{

    QueryRunner runner =

new QueryRunner(DBManager_c3p0.getDataSource());

    String sql = "insert into user(id,name,password) values(?,?,?)";

    Object[][] params =

{{5,"Jake","aaa"},{6,"John","bbb"},{7,"Jansen","ccc"}};

    runner.batch(sql, params);

}

 

 


ResultSetHandler 接口的实现类

ArrayHandler

public void testArrayHandler() throws SQLException{

    QueryRunner runner =

new QueryRunner(DBManager_c3p0.getDataSource());

    String sql = "select * from user";

    Object[] result = runner.query(sql, new ArrayHandler());

    System.out.println(Arrays.asList(result));

}

ArrayListHandler

public void testArrayListHandler() throws SQLException{

    QueryRunner runner =

new QueryRunner(DBManager_c3p0.getDataSource());

    String sql = "select * from user";

    List<Object[]> result =

(List<Object[]>) runner.query(sql, new ArrayListHandler());

    for(int i=0; i<result.size(); i++){

       System.out.println(Arrays.asList(result.get(i)));

    }

}

ColumnListHandler

public void testColumnListHandler() throws SQLException{

    QueryRunner runner =

new QueryRunner(DBManager_c3p0.getDataSource());

    String sql = "select * from user";

    List<Object> result =

(List<Object>) runner.query(sql, new ColumnListHandler("name"));

    System.out.println(result);    

}

 

KeyedHandler

public void testKeyedHandler() throws SQLException{

    QueryRunner runner =

new QueryRunner(DBManager_c3p0.getDataSource());

    String sql = "select * from user";

    Map<Integer,Map<String,Object>> map =

(Map) runner.query(sql, new KeyedHandler("id"));

    for(Map.Entry<Integer,Map<String,Object>> me : map.entrySet()){

       int id = me.getKey();

       Map<String,Object> innermap = me.getValue();

       for(Map.Entry<String, Object> innerme: innermap.entrySet()){

           String name = innerme.getKey();

           Object value = innerme.getValue();

           System.out.println(name + "=" + value);

             

       }

    }

}

 

 

MapListHandler

public void testMapListHandler() throws SQLException{

    QueryRunner runner =

new QueryRunner(DBManager_c3p0.getDataSource());

    String sql = "select * from user";

    List<Map<String,Object>> result =

(List<Map<String,Object>>)

runner.query(sql, new MapListHandler());

    for(int i=0; i<result.size(); i++){

       Map<String, Object> map = result.get(i);

      

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

      

       for(Map.Entry<String, Object> me : map.entrySet() ){

           String colName = me.getKey();

            Object value = me.getValue();

          

           System.out.println(colName + " = " + value);

       }

   

    }

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值