JDBC总结(含DbUtils组件)

jdbc
1. jdbc:使用java代码(程序)发送sql语句的技术

2. jdbc的核心接口(查看API):
java.sql
Driver,DriverManger,Statement,ResultSet(常用)
javax.sql

3.jdbc 主要应用内容
Statement执行( DDL,DML,DQL)
CallableStatment执行存储过程
PreparedStatment执行sql(预编译可防止恶意密码,防止sql注入)
存储过程的调用
批处理插入,删除,更新数据
事务
存储二进制文件(如图片)

4.DBUtils 组件(commons-dbutils.jar)
Apache 组织提供的一个开源 JDBC工具类库,它是对JDBC的简单封装,
学习成本极低,并且使用dbutils能极大简化jdbc编码的工作量,同时也不会影响程序的性能。
因此dbutils成为很多不喜欢hibernate的公司的首选。

两个常用的 (org.apache.commons.dbutils )classes -- 查看APIdoc
DbUtils
连接数据库,关闭资源

QueryRunner
查询,更新,批处理等

Interface ResultSetHandler<T> All Known Implementing Classes:
---》(一些DbUtils 提供的封装结果集对象)
AbstractKeyedHandler, AbstractListHandler,
ArrayHandler, ArrayListHandler,
BaseResultSetHandler, BeanHandler, BeanListHandler, BeanMapHandler,
ColumnListHandler, KeyedHandler,
MapHandler, MapListHandler,
ScalarHandler

以下较常用:
1)BeanHandler: 查询返回单个对象
2)BeanListHandler: 查询返回list集合,集合元素是指定的对象
3) ArrayHandler, 查询返回结果记录的第一行,封装对对象数组, 即返回:Object[]
4) ArrayListHandler, 把查询的每一行都封装为对象数组,再添加到list集合中
5) ScalarHandler 查询返回结果记录的第一行的第一列 (在聚合函数统计的时候用)
6) MapHandler 查询返回结果的第一条记录封装为map

自定义结果集封装数据:(我查询的是student表中的id=29的学生)
public void testQuery() throws Exception {
        String sql = "select * from student where id=?";

        conn = JdbcUtil.getConnection();

        QueryRunner qr = new QueryRunner();
        // select
        Student student  = qr.query(conn, sql, new ResultSetHandler<Student>() {

                    //封装student对象
                    public Student handle(ResultSet rs) throws SQLException{
                        if (rs.next()) {
                            Student student = new Student();
                            student.setId(rs.getInt("id"));
                            student.setName(rs.getString("name"));
                            student.setAge(rs.getInt("age"));
                            return student;
                        }
                        return null;
                    }
                },29);
        System.out.println(student);
        conn.close();
    }


使用DbUtils 组件封装结果集数据:
public void testQuery()throws Exception{

        String sql = "select * from student where id=?";

        conn = JdbcUtil.getConnection();

        QueryRunner qr = new QueryRunner();
        // 使用DbUtils 中已经封装好的方法(注意好参数)
        Student student = qr.query(conn,sql, new BeanHandler<Student>(Student.class),29);

        System.out.println(student);

        conn.close();
    }


----------------------------------------------------------------------------------------
jdbc连接数据库的方式:
--》基础做法
public class Demo1 {
    
    private String url = "jdbc:mysql://localhost:3306/demo";
    private String user = "root";
    private String password = "root";
    
    public void test() {

        try {
            // 1.驱动
            Driver driver = new com.mysql.jdbc.Driver();

            // 设置用户名和密码
            Properties props = new Properties();
            props.setProperty("user", user);
            props.setProperty("pwd", password);

            // 2.连接数据库
            Connection conn = driver.connect(url, props);

            System.out.println(conn);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

  

    --》优化做法(封装成一个工具类)
  public class JdbcUtil {
        private static String url = "jdbc:mysql:///jdbc_demo";
        private static String user = "root";
        private static String password = "root";

        public static Connection conn;
        public static Statement stat;
        public static ResultSet rs;

        // 连接
        public static Connection getConnection(){

            try {
                //注册驱动
                Class.forName("com.mysql.jdbc.Driver");
                // 连接数据库
               return DriverManager.getConnection(url,user,password);

            } catch (Exception e) {
               throw new RuntimeException(e);
            }
        }

        public static void close(){
            try {
                if (rs != null){
                    rs.close();
                    rs = null;
                }
                if (stat != null){
                    stat.close();
                    stat=null;
                }
                if (conn != null){
                    conn.close();
                    conn=null;
                }
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
    }
原文地址:https://www.cnblogs.com/lifehrx/p/6241145.html