DbUtils

DbUtils

Commons DbUtils是Apache组织提供的一个对JDBC进行简单封装的开源工具类库,使用它能够简化JDBC应用程序的开发,同时也不会影响程序的性能。 –百度百科

DbUtils作用

  1. 对查询的结果集进行封装。
  2. 简化了CRUD的代码。

注意:dbutils能帮我们简化了CRUD的代码,但是连接的创建以及获取工作是需要我们来完成。这里使用C3P0来获取连接。

与封装结果集相关的常用的类

  1. BeanHandler :将ResultSet中第一行的数据转化成类对象
  2. BeanListHandler :将ResultSet中所有的数据转化成List,List中存放的是类对象
  3. ScalarHandler :将ResultSet中一条记录的其中某一列的数据存成Object
  4. MapHandler :将ResultSet中第一行的数据存成Map映射
  5. MapListHandler :将ResultSet中所有的数据存成List。List中存放的是Map

更多参考:Dbutils

使用DbUtils

准备工作
1. Dbutils依赖的包: commons-dbcp-1.4.jar
2. C3P0依赖的包:c3p0-0.9.1.2.jar,配置文件c3p0-config.xml
3. 工具类JDBCUtil,用于获取连接

配置文件c3p0-config.xml


<c3p0-config>
    
    <default-config>
        <property name="driverClass">com.mysql.jdbc.Driverproperty>
        <property name="jdbcUrl">jdbc:mysql://localhost/testproperty>
        <property name="user">rootproperty>
        <property name="password">rootproperty>
        
        <property name="initialPoolSize">10property>
        
        <property name="maxIdleTime">30property>
        
        <property name="maxPoolSize">100property>
        
        <property name="minPoolSize">10property>
        
        <property name="maxStatements">200property>
    default-config>
<c3p0-config>

工具类JDBCUtil

package com.mbc.util;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import com.mchange.v2.c3p0.ComboPooledDataSource;


public class JDBCUtil {
    static ComboPooledDataSource dataSource = null;

    static {
        dataSource = new ComboPooledDataSource();
    }

    public static ComboPooledDataSource getDataSource () {
        return dataSource;
    }
}
package com.mbc.test;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.MapHandler;
import org.apache.commons.dbutils.handlers.MapListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import org.junit.Test;

import com.mbc.util.JDBCUtil;
import com.mchange.v2.c3p0.ComboPooledDataSource;

public class TestDbutil {
    @Test
    public void testBeanHandler() {
        //获取连接
        ComboPooledDataSource dataSource = JDBCUtil.getDataSource();
        try {
            //1.创建QueryRunner对象,用于执行sql语句
            QueryRunner queryRunner = new QueryRunner(dataSource);

            //2.创建sql语句,?表示参数占位符,可在执行sql语句时填入,或者把参数封装在一个Object数据中
            String sql = "SELECT * FROM user WHERE id=?";

            //3.执行查询,往query方法中传入sql语句、实现ResultSetHandler接口的对象,与占位符对应的参数 1
            //这里把查询到结果装换为指定类型的JavaBean对象。
            User user= queryRunner.query(sql, new BeanHandler(User.class), 1);
            System.out.println(user);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            //释放资源
            dataSource.close();
        }
    }

    @Test
    public void testBeanListHandler() {
        ComboPooledDataSource dataSource = JDBCUtil.getDataSource();
        try {
            QueryRunner queryRunner = new QueryRunner(dataSource);
            String sql = "SELECT * FROM user";
            //这里把查询到的结果集装换为指定类型的JavaBean对象列表。
            List list= queryRunner.query(sql, new BeanListHandler(User.class));
            for (User user : list) {
                System.out.println(user);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            //释放资源
            dataSource.close();
        }
    }

    @Test
    public void testScalarHandler() {
        ComboPooledDataSource dataSource = JDBCUtil.getDataSource();
        try {
            QueryRunner queryRunner = new QueryRunner(dataSource);
            String sql = "SELECT COUNT(*) FROM user";
            //查询到结果为count(*)的值
            Long count = (Long)queryRunner.query(sql, new ScalarHandler());
            System.out.println(count);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            //释放资源
            dataSource.close();
        }
    }

    @Test
    public void testMapHandler() {
        ComboPooledDataSource dataSource = JDBCUtil.getDataSource();
        try {
            QueryRunner queryRunner = new QueryRunner(dataSource);
            String sql = "SELECT * FROM user WHERE id=?";
            //这里把查询到结果封装成为map对象
            Map query = queryRunner.query(sql, new MapHandler(), 1);
            for (Map.Entry entry: query.entrySet()) {
                System.out.println(entry.getKey() + ":" + entry.getValue());
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            //释放资源
            dataSource.close();
        }
    }

    @Test
    public void testMapListHandler() {
        ComboPooledDataSource dataSource = JDBCUtil.getDataSource();
        try {
            QueryRunner queryRunner = new QueryRunner(dataSource);
            String sql = "SELECT * FROM user WHERE id";
            //这里把查询到结果封装成为map对象列表
            List> querys = queryRunner.query(sql, new MapListHandler());
            for (Map map : querys) {
                for (Map.Entry entry: map.entrySet()) {
                    System.out.println(entry.getKey() + ":" + entry.getValue());
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            //释放资源
            dataSource.close();
        }
    }

    @Test
    public void testDemo() {
        ComboPooledDataSource dataSource = JDBCUtil.getDataSource();
        try {
            QueryRunner queryRunner = new QueryRunner(dataSource);
            //增
            queryRunner.update("INSERT INTO user VALUE(null, ?)", "test");
            //删
            queryRunner.update("DELETE FROM user WHERE id=?", 5);
            //改
            queryRunner.update("UPDATE user SET name=? WHERE name=?", "haha", "test");
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            dataSource.close();
        }
    }
}

你可能感兴趣的:(JavaWeb基础)