您的位置:首页 > 博客中心 > 数据库 >

数据库基础_JDBC[5]_Dbutil以及一个简单的数据库存储过程

时间:2022-03-16 11:32


DButil

1.什么是DButil
commons-dbutils 是 Apache 组织提供的一个开源 JDBC工具类库,它是对JDBC的简单封装,学习成本极低,并且使用dbutils能极大简化jdbc
编码的工作量,同时也不会影响程序的性能

2.DButil常用的方法以及功能
DbUtils :提供如关闭连接、装载JDBC驱动程序等常规工作的工具类,里面的所有方法都是静态的。主要方法如下:
(1).public static void close(…) throws java.sql.SQLException:DbUtils类提供了三个重载的关闭方法。这些方法检查所提供的参数是不是
NULL,如果不是的话,它们就关闭Connection、Statement和ResultSet。
(2).public static void closeQuietly(…): 这一类方法不仅能在Connection、Statement和ResultSet为NULL情况下避免关闭,还能隐藏一些在程序
中抛出的SQLEeception。
(3).public static void commitAndCloseQuietly(Connection conn): 用来提交连接,然后关闭连接,并且在关闭连接时不抛出SQL异常。
(4).public static boolean loadDriver(java.lang.String driverClassName):这一方装载并注册JDBC驱动程序,如果成功就返回true。
使用该方法,你不需要捕捉这个异常ClassNotFoundException。

3.相关的方法测试

/**
* ScalarHandler: 把结果集转为一个数值(可以是任意基本数据类型和字符串, Date 等)返回
*/
@Test
public void testScalarHandler(){
Connection connection = null;

try {
connection = JDBCTools.getConnection();
String sql = "SELECT name, email " +
"FROM customers";

Object result = queryRunner.query(connection,
sql, new ScalarHandler());

System.out.println(result);
} catch (Exception e) {
e.printStackTrace();
} finally{
JDBCTools.releaseDB(null, null, connection);
}
}

/**
* MapListHandler: 将结果集转为一个 Map 的 List
* Map 对应查询的一条记录: 键: SQL 查询的列名(不是列的别名), 值: 列的值.
* 而 MapListHandler: 返回的多条记录对应的 Map 的集合.
*/
@Test
public void testMapListHandler(){
Connection connection = null;

try {
connection = JDBCTools.getConnection();
String sql = "SELECT id, name, email, birth " +
"FROM customers";

List<Map<String, Object>> result = queryRunner.query(connection,
sql, new MapListHandler());

System.out.println(result);
} catch (Exception e) {
e.printStackTrace();
} finally{
JDBCTools.releaseDB(null, null, connection);
}
}

/**
* MapHandler: 返回 SQL 对应的第一条记录对应的 Map 对象.
* 键: SQL 查询的列名(不是列的别名), 值: 列的值.
*/
@Test
public void testMapHandler(){
Connection connection = null;

try {
connection = JDBCTools.getConnection();
String sql = "SELECT id, name, email, birth " +
"FROM customers";

Map<String, Object> result = queryRunner.query(connection,
sql, new MapHandler());

System.out.println(result);
} catch (Exception e) {
e.printStackTrace();
} finally{
JDBCTools.releaseDB(null, null, connection);
}
}

/**
* BeanListHandler: 把结果集转为一个 List, 该 List 不为 null, 但可能为
* 空集合(size() 方法返回 0)
* 若 SQL 语句的确能够查询到记录, List 中存放创建 BeanListHandler 传入的 Class
* 对象对应的对象.
*/
@Test
public void testBeanListHandler(){
Connection connection = null;

try {
connection = JDBCTools.getConnection();
String sql = "SELECT id, name, email, birth " +
"FROM customers";

List<Customer> customers = queryRunner.query(connection,
sql, new BeanListHandler(Customer.class));

System.out.println(customers);
} catch (Exception e) {
e.printStackTrace();
} finally{
JDBCTools.releaseDB(null, null, connection);
}
}

/**
* BeanHandler: 把结果集的第一条记录转为创建 BeanHandler 对象时传入的 Class
* 参数对应的对象.
*/
@Test
public void testBeanHanlder(){
Connection connection = null;

try {
connection = JDBCTools.getConnection();
String sql = "SELECT id, name customerName, email, birth " +
"FROM customers WHERE id >= ?";

Customer customer = queryRunner.query(connection,
sql, new BeanHandler(Customer.class), 5);

System.out.println(customer);
} catch (Exception e) {
e.printStackTrace();
} finally{
JDBCTools.releaseDB(null, null, connection);
}
}

QueryRunner queryRunner = new QueryRunner();

class MyResultSetHandler implements ResultSetHandler{

@Override
public Object handle(ResultSet resultSet)
throws SQLException {
// System.out.println("handle....");
// return "atguigu";

List<Customer> customers = new ArrayList<>();

while(resultSet.next()){
Integer id = resultSet.getInt(1);
String name = resultSet.getString(2);
String email = resultSet.getString(3);
Date birth = resultSet.getDate(4);

Customer customer =
new Customer(id, name, email, birth);
customers.add(customer);
}

return customers;
}

}

/**
* QueryRunner 的 query 方法的返回值取决于其 ResultSetHandler 参数的
* handle 方法的返回值
*
*/
@Test
public void testQuery(){
Connection connection = null;

try {
connection = JDBCTools.getConnection();
String sql = "SELECT id, name, email, birth " +
"FROM customers";
Object obj = queryRunner.query(connection, sql,
new MyResultSetHandler());

System.out.println(obj);
} catch (Exception e) {
e.printStackTrace();
} finally{
JDBCTools.releaseDB(null, null, connection);
}
}

@Test
public void testUpdate(){
Connection connection = null;

try {
connection = JDBCTools.getConnection();
String sql = "UPDATE customers SET name = ? " +
"WHERE id = ?";
queryRunner.update(connection, sql, "MIKE", 11);
} catch (Exception e) {
e.printStackTrace();
} finally{
JDBCTools.releaseDB(null, null, connection);
}
}

 

JDBC_存储过程

/**
* 如何使用 JDBC 调用存储在数据库中的函数或存储过程
*/
@Test
public void testCallableStatment() {

Connection connection = null;
CallableStatement callableStatement = null;

try {
connection = JDBCTools.getConnection();

// 1. 通过 Connection 对象的 prepareCall()
// 方法创建一个 CallableStatement 对象的实例.
// 在使用 Connection 对象的 preparedCall() 方法时,
// 需要传入一个 String 类型的字符串, 该字符串用于指明如何调用存储过程.
String sql = "{?= call sum_salary(?, ?)}";
callableStatement = connection.prepareCall(sql);

// 2. 通过 CallableStatement 对象的
//reisterOutParameter() 方法注册 OUT 参数.
callableStatement.registerOutParameter(1, Types.NUMERIC);
callableStatement.registerOutParameter(3, Types.NUMERIC);

// 3. 通过 CallableStatement 对象的 setXxx() 方法设定 IN 或 IN OUT 参数. 若想将参数默认值设为
// null, 可以使用 setNull() 方法.
callableStatement.setInt(2, 80);

// 4. 通过 CallableStatement 对象的 execute() 方法执行存储过程
callableStatement.execute();

// 5. 如果所调用的是带返回参数的存储过程,
//还需要通过 CallableStatement 对象的 getXxx() 方法获取其返回值.
double sumSalary = callableStatement.getDouble(1);
long empCount = callableStatement.getLong(3);

System.out.println(sumSalary);
System.out.println(empCount);

} catch (Exception e) {
e.printStackTrace();
} finally {
JDBCTools.releaseDB(null, callableStatement, connection);
}


}


本类排行

今日推荐

热门手游