JDBC_5 DBUtils

DBUtils


commons-dbutilsは、Apache組織が提供するオープンソースのJDBCツールライブラリであり、データベースの追加、削除、変更、およびクエリ操作をカプセル化します。

API

  • QueryRunner
  • ResulSetHandler
  • Dbutils

挿入例

Connection conn = null;
        try {
    
    
            QueryRunner runner = new QueryRunner();
            conn = JBBCUtils.getConnections3();
            String sql = "insert into customers(name,emial,birth) values(?,?,?)";
            int insertCount = runner.update(conn,sql,"蔡旭坤","[email protected]","1993-03-01");
            System.out.println(insertCount);
        } catch (SQLException throwables) {
    
    
            throwables.printStackTrace();
        }finally {
    
    
            JDBCUtils.closeResource(conn,null);
        }

更新


    public static void main(String[] args) {
    
    

        Connection conn = null;
        try {
    
    
            QueryRunner runner = new QueryRunner();
            conn = JBBCUtils.getConnections3();
            String sql = "select id,name,email,birth from customers where id < ?";
            BeanListHandler<Customer> handler = new BeanListHandler<Cunstomer>(customer.class);
            List<Customer> query = runner.query(conn.sql, handler, 23);
            System.out.println(insertCount);
        } catch (SQLException throwables) {
    
    
            throwables.printStackTrace();
        }finally {
    
    
            JDBCUtils.closeResource(conn,null);
        }
    }

ルックアップテーブルの特別な値

public static void main(String[] args) {
    
    

        Connection conn = null;
        try {
    
    
            QueryRunner runner = new QueryRunner();
            conn = JBBCUtils.getConnections3();
            String sql = "select count(*) from customers";
            ScalarHandler handler = new ScalarHandler();
            
            Long count = (Long)runner.query(conn.sql, handler);
            System.out.println(count);
        } catch (SQLException throwables) {
    
    
            throwables.printStackTrace();
        }finally {
    
    
            JDBCUtils.closeResource(conn,null);
        }
    }

カスタムResultSetHandler

public static void main(String[] args) {
    
    

        Connection conn = null;
        try {
    
    
            QueryRunner runner = new QueryRunner();
            conn = JBBCUtils.getConnections3();
            String sql = "select id,name,email,birth from customers where id = ?";
            ResultSetHandler<Customers> resultSetHandler = new ResultSetHandler<Customers>() {
    
    
                @Override
                public Customers handle(ResultSet rs) throws SQLException {
    
    
                    if(resultSet.next()){
    
    
                        int id = rs.getInt(1);
                        String name = rs.getString(2);
                        String email = rs.getString(3);
                        Date birth = rs.getDate(4);
                        Customer customer = new Cunstomer(id,name,email,birth);
                        return customer;
                    }
                    return null;
                }
            };

            Long count = (Long)runner.query(conn.sql, handler);
            System.out.println(count);
        } catch (SQLException throwables) {
    
    
            throwables.printStackTrace();
        }finally {
    
    
            JDBCUtils.closeResource(conn,null);
        }
    }

おすすめ

転載: blog.csdn.net/m0_46656833/article/details/112554242