DBUtils和鏈接池

DBUtils:簡化數據庫操做(不用本身設置預處理對象查詢結果集的處理)java

核心類:queryrunner類sql

update()方法:增、刪、改數據庫

public static void insert() throws SQLException{
        QueryRunner qr = new QueryRunner();
        String sql = "insert into sort values(?,?,?)";
        Object[] obj = {9,"茶香小排","特別好吃"};
        Connection conn = JDBCUtils.getConnection();
        int row = qr.update(conn,sql,obj);
        conn.close();
        System.out.println(row);
    }
    public static void update() throws SQLException{
        QueryRunner qr = new QueryRunner();
        String sql = "update sort set sname = ?,sdesc =? where sid = ?";
        Object[] obj = {"紅燒排骨","通常吧",9};
        Connection conn = JDBCUtils.getConnection();
        int row = qr.update(conn, sql,obj);
        conn.close();
        System.out.println(row);
    }
    public static void delete() throws SQLException{
        QueryRunner qr = new QueryRunner();
        String sql = "delete from sort where sid =?";
        Connection conn = JDBCUtils.getConnection();
        int row = qr.update(conn, sql, 9);
        conn.close();
        System.out.println(row);
    }

query()方法:查詢數組

resultrshandler類:結果集處理類工具

arrayhandler:把第一條查詢結果放入到object[]數組中spa

arraylisthandler:把每一條查詢結果放入到object[]數組中,再把數組放入到list集合中scala

beanhandler:把第一條查詢結果封裝到實體類中code

beanlisthandler:把每一條查詢結果封裝到實體類中, 再把實體類放入到list集合中對象

columnlisthandler:把某一列結果放入到list集合中blog

scalarhandler:單一查詢結果時使用此類

maphandler:把第一條查詢結果放入到map集合中,列名和數據

maplisthandler:把每一條查詢結果放入到map集合中,把map放入到list集合中

public static void select1() throws SQLException{
        QueryRunner qr = new QueryRunner();
        String sql = "select * from sort";
        Connection conn = JDBCUtils.getConnection();
        Object[] obj = qr.query(conn, sql, new ArrayHandler());
        conn.close();
        for(Object o:obj){
            System.out.println(o);
        }
    }
    public static void select2() throws SQLException{
        QueryRunner qr = new QueryRunner();
        String sql = "select * from sort";
        Connection conn = JDBCUtils.getConnection();
        List<Object[]> list = qr.query(conn, sql,new ArrayListHandler());
        conn.close();
        for(Object[] obj:list){
            for(Object o:obj){
                System.out.println(o);
            }
            System.out.println();
        }
    }
    public static void select3() throws SQLException{
        QueryRunner qr = new QueryRunner();
        String sql = "select * from sort";
        Connection conn = JDBCUtils.getConnection();
        Sort sort = qr.query(conn, sql,new BeanHandler<Sort>(Sort.class));
        conn.close();
        System.out.println(sort);
    }
    public static void select4() throws SQLException{
        QueryRunner qr = new QueryRunner();
        String sql = "select * from sort";
        Connection conn = JDBCUtils.getConnection();
        List<Sort> list = qr.query(conn, sql,new BeanListHandler<Sort>(Sort.class));
        conn.close();
        for(Sort s:list){
            
            System.out.println(s);
        }
    }
    public static void select5() throws SQLException{
        QueryRunner qr = new QueryRunner();
        String sql = "select * from sort";
        Connection conn = JDBCUtils.getConnection();
        List<String> list = qr.query(conn, sql,new ColumnListHandler<String>("sname"));
        conn.close();
        for(String s:list){
            System.out.println(s);
        }
    }
    public static void select6() throws SQLException{
        QueryRunner qr = new QueryRunner();
        String sql = "select count(*) from sort";
        Connection conn = JDBCUtils.getConnection();
        long count = qr.query(conn, sql,new ScalarHandler<Long>());
        conn.close();
        System.out.println(count);
    }
    public static void select7() throws SQLException{
        QueryRunner qr = new QueryRunner();
        String sql = "select * from sort";
        Connection conn = JDBCUtils.getConnection();
        Map<String, Object> map = qr.query(conn, sql,new MapHandler());
        conn.close();
        for(String key:map.keySet()){
            System.out.println(key+"..."+map.get(key));
        }
    }
    public static void select8() throws SQLException{
        QueryRunner qr = new QueryRunner();
        String sql = "select * from sort";
        Connection conn = JDBCUtils.getConnection();
        List<Map<String,Object>> list = qr.query(conn, sql,new MapListHandler());
        conn.close();
        for(Map<String,Object> map:list){
            for(String s:map.keySet()){
                System.out.println(s+"..."+map.get(s));
            }
            System.out.println();
        }
    }

鏈接池:使用之前的JDBCUtils工具類,每次數據庫交互都會從新開啓和關閉一個鏈接,這樣很耗費資源,鏈接池的出現能夠避免資源的浪費

DBCP鏈接池:實現java提供的鏈接池接口---DataSource---實現類BasicDataSource

用這個DBCP中的實現類來從新寫一個鏈接池工具類DBUtils

public static void insert() throws SQLException{
            QueryRunner qr = new QueryRunner(DBUtils.getDataSource());
            String sql = "insert into sort values(?,?,?)";
            Object[] obj = {9,"茶香小排","特別好吃"};
            int row = qr.update(sql,obj);
            System.out.println(row);
        }
相關文章
相關標籤/搜索