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);    }
实例:客户查问零碎

客户查问零碎(https://segmentfault.com/a/11...