JavaWeb开发05——DbUtils使用

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.class),500);
        System.out.println(acc);
    }

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

public void test() throws SQLException{
        QueryRunner runner = new QueryRunner(new ComboPooledDataSource());
        Listlist = runner.query("select * from account where money>?", new BeanListHandler(Account.class),500);
        System.out.println(list);
    }
实例:客户查询系统

客户查询系统(https://segmentfault.com/a/11...

你可能感兴趣的:(java,java-web,后端)