“使用 commons-dbutils 中的 QueryRunner 测试添加数据”的版本间的差异
		
		
		
		
		
		跳到导航
		跳到搜索
		
				
		
		
	
Jihongchang(讨论 | 贡献)  (建立内容为“https://www.bilibili.com/video/BV1eJ411c7rf?p=51”的新页面)  | 
				Jihongchang(讨论 | 贡献)   | 
				||
| (未显示同一用户的5个中间版本) | |||
| 第1行: | 第1行: | ||
https://www.bilibili.com/video/BV1eJ411c7rf?p=51  | 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 引入类库 ===  | ||
| + | <syntaxhighlight lang="xml">  | ||
| + | <dependency>  | ||
| + |     <groupId>commons-dbutils</groupId>  | ||
| + |     <artifactId>commons-dbutils</artifactId>  | ||
| + |     <version>1.3</version>  | ||
| + | </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>https://github.com/jihch/jdbc_2/blob/main/pom.xml  | ||
| + | |||
| + | https://github.com/jihch/jdbc_2/blob/main/src/main/java/io/github/jihch/dbutils/QueryRunnerTest.java  | ||
2023年1月14日 (六) 06:12的最新版本
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);
        }
    }
}
https://github.com/jihch/jdbc_2/blob/main/pom.xml
https://github.com/jihch/jdbc_2/blob/main/src/main/java/io/github/jihch/dbutils/QueryRunnerTest.java