DbUtils使用
一.增删改
1.增加数据
public void add() throws SQLException{
QueryRunner runner = new MyQueryRunner(new ComboPooledDataSource());
runner.update("insert into account values(null,?,?)","c",1000);
}
2.删除数据
public void del() throws SQLException{
QueryRunner runner = new MyQueryRunner(new ComboPooledDataSource());
runner.update("delete from account where id=?",3);
}
3.改变数据
public void update() throws SQLException{
QueryRunner runner = new MyQueryRunner(new ComboPooledDataSource());
runner.update("update account set money=? where name=?", 777,"a");
}
二.查询数据
1.ScalarHandler:获取结果集中第一行数据指定列的值,常用来进行单值查询
public void test() throws SQLException{
QueryRunner runner = new QueryRunner(new ComboPooledDataSource());
Long count = (Long)runner.query("select count(*) from account",new ScalarHandler());
System.out.println(count);
}
2.BeanHandler:将结果集中的第一行数据封装到一个对应的JavaBean实例中
public void test() throws SQLException{
QueryRunner runner = new QueryRunner(new ComboPooledDataSource());
Account acc = runner.query("select * from account where money>?", new BeanHandler<Account>(Account.class),500);
System.out.println(acc);
}
3.BeanListHandler:将结果集中的每一行数据都封装到一个对应的JavaBean实例中,存放到List里。
public void test() throws SQLException{
QueryRunner runner = new QueryRunner(new ComboPooledDataSource());
List<Account>list = runner.query("select * from account where money>?", new BeanListHandler<Account>(Account.class),500);
System.out.println(list);
}
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。