“使用 commons-dbutils 中的 QueryRunner 测试添加数据”的版本间的差异
		
		
		
		
		
		跳到导航
		跳到搜索
		
				
		
		
	
Jihongchang(讨论 | 贡献)  | 
				Jihongchang(讨论 | 贡献)   | 
				||
| 第20行: | 第20行: | ||
     <version>1.3</version>  |      <version>1.3</version>  | ||
</dependency>  | </dependency>  | ||
| + | |||
| + | </syntaxhighlight>  | ||
| + | |||
| + | |||
| + | |||
| + | |||
| + | === Java 代码的使用实现 ===  | ||
| + | <syntaxhighlight lang="java">  | ||
| + | package io.github.jihch.dbutils;  | ||
| + | |||
| + | |||
| + | import io.github.jihch.betterutil.JDBCUtils;  | ||
| + | import org.apache.commons.dbutils.QueryRunner;  | ||
| + | import org.junit.Test;  | ||
| + | |||
| + | import java.sql.Connection;  | ||
| + | import java.sql.SQLException;  | ||
| + | |||
| + | /**  | ||
| + |  * commons-dbutils 是 Apache 组织提供的一个开源 JDBC 工具类库,封装了针对于数据库的增删改查操作  | ||
| + |  */  | ||
| + | public class QueryRunnerTest {  | ||
| + | |||
| + |     @Test  | ||
| + |     public void testInsert() {  | ||
| + |         QueryRunner runner = new QueryRunner();  | ||
| + |         Connection conn = JDBCUtils.getConnectionFromDruid();  | ||
| + |         String sql = "insert into customers (name, email, birth) values (?, ?, ?)";  | ||
| + |         int insertCount = 0;  | ||
| + |         try {  | ||
| + |             insertCount = runner.update(conn, sql, "蔡徐坤", "caixukun@126.com", "1997-09-08");  | ||
| + |             System.out.printf("添加了 %d 条记录\n", insertCount);  | ||
| + |         } catch (SQLException e) {  | ||
| + |             e.printStackTrace();  | ||
| + |         } finally {  | ||
| + |             JDBCUtils.closeResource(conn, null);  | ||
| + |         }  | ||
| + |     }  | ||
| + | |||
| + | }  | ||
</syntaxhighlight>  | </syntaxhighlight>  | ||
2023年1月13日 (五) 12:50的版本
https://www.bilibili.com/video/BV1eJ411c7rf?p=51
Apache-DBUtils 简介
- commons-dbutils 是 Apache 组织提供的一个开源 JDBC 工具类库,它是对 JDBC 的简单封装,学习成本极低,并且使用 dbutils 能极大简化 jdbc 编码的工作量,同时也不会影响程序性能。
 
- API 介绍
- org.apache.commons.dbutils.QueryRunner
 - org.apache.commons.dbutils.ResultSetHandler
 - 工具类:org.apache.commons.dbutils.DbUtils
 
 
pom.xml 引入类库
<dependency>
    <groupId>commons-dbutils</groupId>
    <artifactId>commons-dbutils</artifactId>
    <version>1.3</version>
</dependency>
Java 代码的使用实现
package io.github.jihch.dbutils;
import io.github.jihch.betterutil.JDBCUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.junit.Test;
import java.sql.Connection;
import java.sql.SQLException;
/**
 * commons-dbutils 是 Apache 组织提供的一个开源 JDBC 工具类库,封装了针对于数据库的增删改查操作
 */
public class QueryRunnerTest {
    @Test
    public void testInsert() {
        QueryRunner runner = new QueryRunner();
        Connection conn = JDBCUtils.getConnectionFromDruid();
        String sql = "insert into customers (name, email, birth) values (?, ?, ?)";
        int insertCount = 0;
        try {
            insertCount = runner.update(conn, sql, "蔡徐坤", "caixukun@126.com", "1997-09-08");
            System.out.printf("添加了 %d 条记录\n", insertCount);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.closeResource(conn, null);
        }
    }
}