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:獲取結果集中第一行數據指定列的值,經常使用來進行單值查詢segmentfault

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實例中code

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裏。get

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...io

相關文章
相關標籤/搜索