jdbc 与 mysql 连接 - Blod及批量数据处理

2024-02-19 15:58

本文主要是介绍jdbc 与 mysql 连接 - Blod及批量数据处理,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Blob 类型的数据操作

package com.atguigu5.blob;import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Blob;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;import org.junit.Test;import com.atguigu3.bean.Customer;
import com.atguigu3.util.JDBCUtils;/*** 测试使用 PreparedStatement 操作 Blob 类型的数据* */
public class BlobTest {// 向数据表 customers 中插入 Blob 类型的字段@Testpublic void testInsert1() throws Exception {Connection conn = JDBCUtils.getConnection();String sql = "insert into customers(name,email,birth,photo)values(?,?,?,?)";PreparedStatement ps = conn.prepareStatement(sql);ps.setObject(1, "李阿豪");ps.setObject(2,"li@qq.com");ps.setObject(3, "1992-09-08");// 操纵 Blob 类型的变量FileInputStream is = new FileInputStream(new File("壁纸3.jpg"));ps.setBlob(4, is);ps.execute();is.close();JDBCUtils.closeResource(conn, ps);}// 向数据表 customers 中插入 Blob 类型的字段@Testpublic void testInsert2() throws Exception {Connection conn = JDBCUtils.getConnection();String sql = "insert into customers(name,email,birth,photo)values(?,?,?,?)";PreparedStatement ps = conn.prepareStatement(sql);ps.setObject(1, "元昊");ps.setObject(2,"yuanhao@qq.com");ps.setObject(3, "1992-09-08");// 操纵 Blob 类型的变量FileInputStream is = new FileInputStream(new File("壁纸5.jpg"));ps.setBlob(4, is);ps.execute();is.close();JDBCUtils.closeResource(conn, ps);// 在 mysql 下的 my.ini 文件尾添加: max_allowed_packet=16M// 完成后,"此电脑" - "管理" - "服务和应用程序" - "服务" - "MySQL" - "重新启动"// 完成操作后,就可以上传大于 1M 的图片到 MySQL 了。}// 向数据表 customers 中修改 Blob 类型的字段 @Testpublic void testUpdate() throws Exception {Connection conn = JDBCUtils.getConnection();String sql = "update customers set photo = ? where id = ?";PreparedStatement ps = conn.prepareStatement(sql);// 填充占位符// 操纵 Blob 类型的变量FileInputStream is = new FileInputStream(new File("壁纸1.jpeg"));ps.setBlob(1, is);// 23 : 要修改 id 号ps.setInt(2,23);ps.execute();is.close();JDBCUtils.closeResource(conn, ps);}// 查询数据表 customers 中 Blob 类型的字段@Testpublic void testQuery() {Connection conn = null;PreparedStatement ps = null;InputStream is = null;FileOutputStream fos = null;ResultSet rs = null;try {conn = JDBCUtils.getConnection();String sql = "select id,name,email,birth,photo from customers where id = ?";ps = conn.prepareStatement(sql);ps.setInt(1, 23);rs = ps.executeQuery();if(rs.next()) {// 方式一:通过顺序的方式 类似于 ArrayList
//			int id = rs.getInt(1);
//			String name = rs.getString(2);
//			String email = rs.getString(3);
//			Date birth = rs.getDate(4);// 方式二:通过别名的方式int id = rs.getInt("id");String name = rs.getString("name");String email = rs.getString("email");Date birth = rs.getDate("birth");Customer cust = new Customer(id,name,email,birth);System.out.println(cust);// 将 Blob 类型的字段下载下来,以文件的方式保存在本地Blob photo = rs.getBlob("photo");// photo 是一个比较大的数据,这时需要用 流 的方式获取is = photo.getBinaryStream();fos = new FileOutputStream("zhangyuhao.jpg");byte[] buffer = new byte[1024];int len;while((len = is.read(buffer)) != -1) {fos.write(buffer,0,len);}}} catch (Exception e) {e.printStackTrace();} finally {try {if(is != null)is.close();} catch (IOException e) {e.printStackTrace();}try {if(fos != null)fos.close();} catch (IOException e) {e.printStackTrace();}JDBCUtils.closeResource(conn, ps, rs);}}
}

让 mysql 开启批处理的支持 :

?rewriteBatchedStatements = true 写在配置文件的 url 后面

user=root
password=123456
url=jdbc:mysql://localhost:3306/test?rewriteBatchedStatements = true
driverClass=com.mysql.cj.jdbc.Driver
package com.atguigu5.blob;import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;import org.junit.Test;import com.atguigu3.util.JDBCUtils;/*** 使用 PreparedStatement 实现批量数据的操作** update、delete 本身就具有批量操作的效果。* 此时的批量操作,主要指的时批量插入。使用 PreparedStatement 如何实现更高效的批量插入?* * 题目:向 goods 表中插入 20000 条数据*   CREATE TABLE goods(*       id INT PRIMARY KEY AUTO_INCREMENT,*       NAME VARCHAR(25)*   );* * 方式一:使用 Statement*    Connection  conn = JDBCUtils.getConnection();*    Statement st = conn.createStatement();*    for(int i = 1; i <= 20000; i++){*    		String sql = "insert into goods(name) values('name_" + i + "')";*    		st.execute(sql);*    }* */
public class InsertTest {// 批量插入的方式二:使用 PreparedStatement@Testpublic void testInsert1() {Connection conn = null;PreparedStatement ps = null;try {long start = System.currentTimeMillis();conn = JDBCUtils.getConnection();String sql = "insert into goods(name)values(?)";ps = conn.prepareStatement(sql);for(int i = 1; i <= 20000; i++) {ps.setObject(1, "name_" + i);ps.execute();}long end = System.currentTimeMillis();System.out.println("花费的时间为: " + (end - start));// 20000:76229 } catch (Exception e) {e.printStackTrace();} finally {JDBCUtils.closeResource(conn, ps);}}/**  批量插入的方式三*  		1.addBatch()、executeBatch()、clearBatch()*  		2.mysql 服务器默认是关闭此处理的,我们需要通过一个参数,让 mysql 开启批处理的支持。*  	   		    ?rewriteBatchedStatements = true 写在配置文件的 url 后面*  		3.使用更新的 mysql 驱动:mysql-connector-java-5.1.37-bin.jar*  */@Testpublic void testInsert2() {Connection conn = null;PreparedStatement ps = null;try {long start = System.currentTimeMillis();conn = JDBCUtils.getConnection();String sql = "insert into goods(name)values(?)";ps = conn.prepareStatement(sql);for(int i = 1; i <= 1000000; i++) {ps.setObject(1, "name_" + i);// 1."攒" sqlps.addBatch();if(i % 500 == 0) {// 2.执行 batchps.executeBatch();// 3.清空 batchps.clearBatch();}}long end = System.currentTimeMillis();System.out.println("花费的时间为: " + (end - start));// 20000:76229 -- 1525// 1000000:18780 -- 		} catch (Exception e) {e.printStackTrace();} finally {JDBCUtils.closeResource(conn, ps);}}/**  批量插入的方式四: 设置连接不允许自动提交数据		*  */@Testpublic void testInsert3() {Connection conn = null;PreparedStatement ps = null;try {long start = System.currentTimeMillis();conn = JDBCUtils.getConnection();// 设置不允许自动提交数据(为了提高插入速度,缩短插入时间)conn.setAutoCommit(false);String sql = "insert into goods(name)values(?)";ps = conn.prepareStatement(sql);for(int i = 1; i <= 1000000; i++) {ps.setObject(1, "name_" + i);// 1."攒" sqlps.addBatch();if(i % 500 == 0) {// 2.执行 batchps.executeBatch();// 3.清空 batchps.clearBatch();}}// 提交数据conn.commit();long end = System.currentTimeMillis();System.out.println("花费的时间为: " + (end - start));// 20000:76229 -- 1525// 1000000:18780 -- 9939		} catch (Exception e) {e.printStackTrace();} finally {JDBCUtils.closeResource(conn, ps);}}
}

这篇关于jdbc 与 mysql 连接 - Blod及批量数据处理的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/725153

相关文章

SQL server数据库如何下载和安装

《SQLserver数据库如何下载和安装》本文指导如何下载安装SQLServer2022评估版及SSMS工具,涵盖安装配置、连接字符串设置、C#连接数据库方法和安全注意事项,如混合验证、参数化查... 目录第一步:打开官网下载对应文件第二步:程序安装配置第三部:安装工具SQL Server Manageme

C#连接SQL server数据库命令的基本步骤

《C#连接SQLserver数据库命令的基本步骤》文章讲解了连接SQLServer数据库的步骤,包括引入命名空间、构建连接字符串、使用SqlConnection和SqlCommand执行SQL操作,... 目录建议配合使用:如何下载和安装SQL server数据库-CSDN博客1. 引入必要的命名空间2.

全面掌握 SQL 中的 DATEDIFF函数及用法最佳实践

《全面掌握SQL中的DATEDIFF函数及用法最佳实践》本文解析DATEDIFF在不同数据库中的差异,强调其边界计算原理,探讨应用场景及陷阱,推荐根据需求选择TIMESTAMPDIFF或inte... 目录1. 核心概念:DATEDIFF 究竟在计算什么?2. 主流数据库中的 DATEDIFF 实现2.1

MySQL 多列 IN 查询之语法、性能与实战技巧(最新整理)

《MySQL多列IN查询之语法、性能与实战技巧(最新整理)》本文详解MySQL多列IN查询,对比传统OR写法,强调其简洁高效,适合批量匹配复合键,通过联合索引、分批次优化提升性能,兼容多种数据库... 目录一、基础语法:多列 IN 的两种写法1. 直接值列表2. 子查询二、对比传统 OR 的写法三、性能分析

MySQL中的LENGTH()函数用法详解与实例分析

《MySQL中的LENGTH()函数用法详解与实例分析》MySQLLENGTH()函数用于计算字符串的字节长度,区别于CHAR_LENGTH()的字符长度,适用于多字节字符集(如UTF-8)的数据验证... 目录1. LENGTH()函数的基本语法2. LENGTH()函数的返回值2.1 示例1:计算字符串

浅谈mysql的not exists走不走索引

《浅谈mysql的notexists走不走索引》在MySQL中,​NOTEXISTS子句是否使用索引取决于子查询中关联字段是否建立了合适的索引,下面就来介绍一下mysql的notexists走不走索... 在mysql中,​NOT EXISTS子句是否使用索引取决于子查询中关联字段是否建立了合适的索引。以下

Java通过驱动包(jar包)连接MySQL数据库的步骤总结及验证方式

《Java通过驱动包(jar包)连接MySQL数据库的步骤总结及验证方式》本文详细介绍如何使用Java通过JDBC连接MySQL数据库,包括下载驱动、配置Eclipse环境、检测数据库连接等关键步骤,... 目录一、下载驱动包二、放jar包三、检测数据库连接JavaJava 如何使用 JDBC 连接 mys

SQL中如何添加数据(常见方法及示例)

《SQL中如何添加数据(常见方法及示例)》SQL全称为StructuredQueryLanguage,是一种用于管理关系数据库的标准编程语言,下面给大家介绍SQL中如何添加数据,感兴趣的朋友一起看看吧... 目录在mysql中,有多种方法可以添加数据。以下是一些常见的方法及其示例。1. 使用INSERT I

Qt使用QSqlDatabase连接MySQL实现增删改查功能

《Qt使用QSqlDatabase连接MySQL实现增删改查功能》这篇文章主要为大家详细介绍了Qt如何使用QSqlDatabase连接MySQL实现增删改查功能,文中的示例代码讲解详细,感兴趣的小伙伴... 目录一、创建数据表二、连接mysql数据库三、封装成一个完整的轻量级 ORM 风格类3.1 表结构

MySQL 中的 CAST 函数详解及常见用法

《MySQL中的CAST函数详解及常见用法》CAST函数是MySQL中用于数据类型转换的重要函数,它允许你将一个值从一种数据类型转换为另一种数据类型,本文给大家介绍MySQL中的CAST... 目录mysql 中的 CAST 函数详解一、基本语法二、支持的数据类型三、常见用法示例1. 字符串转数字2. 数字