mysql 导出blob图片_如何批量导出mysql中blob格式图片

本文展示了如何使用Java从MySQL数据库中批量导出Blob类型的图片到本地文件系统。通过建立数据库连接,执行SQL查询,读取Blob数据并将其写入本地文件,实现了图片的导出。主要涉及的类包括Connection、PreparedStatement、ResultSet和Blob。
摘要由CSDN通过智能技术生成

import java.io.File;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.OutputStream;

import java.sql.Blob;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Statement;

public class TestPic {

// 定义 JDBC 驱动串

String jdbcString = "com.mysql.jdbc.Driver";

// 定义 URL 连接串

String urlString = "jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true";

// 定义连接用户名

String userName = "cccc";

// 定义连接用户口令

String password = "cccc";

// 定义连接对象

static Connection conn = null;

static Statement stmt = null;

static PreparedStatement ps = null;

static ResultSet rs = null;

/*

* 加载 JDBC 驱动程序

*

* @throws SQLException 异常

*/

public void loadJdbcDriver() throws SQLException {

try {

System.out.println("Loading JDBC Driver...");

// 加载 JDBC 驱动程序

Class.forName(jdbcString);

} catch (ClassNotFoundException e) {

throw new SQLException("Load JDBC Driver Error1: " + e.getMessage());

} catch (Exception ex) {

throw new SQLException("Load JDBC Driver Error : " + ex.getMessage());

}

}

public void connect() throws SQLException {

try {

System.out.println("Connecting to DM Server...");

// 连接 数据库

conn = DriverManager.getConnection(urlString, userName, password);

System.out.println("数据库连接成功!");

} catch (SQLException e) {

throw new SQLException("Connect to DM Server Error : " + e.getMessage());

}

}

/*

* 关闭连接

*

* @throws SQLException 异常

*/

public void disConnect() throws SQLException {

try {

// 关闭连接

conn.close();

System.out.println("close");

} catch (SQLException e) {

throw new SQLException("close connection error : " + e.getMessage());

}

}

public static void main(String args[]) throws IOException, SQLException {

TestPic basicApp = new TestPic();

// 加载驱动程序

basicApp.loadJdbcDriver();

basicApp.connect();

// 读取图片出来,保存到本地的磁盘上面

String sql = "SELECT * FROM thumb";

ps = conn.prepareStatement(sql);

rs = ps.executeQuery();

String s1 = "";

//将图片存放到本地的路径

String s = "E:\\image\\";

while (rs.next()) {

System.out.println("thumb_" + rs.getInt("id")+".jpg");

Blob blob = rs.getBlob("thumb");

if(blob == null){

continue;

}

//验证图片路径是否存在,不存在就创建

File f = new File(s);

if(!f.exists()){

f.mkdirs();

}

s1 = s + "thumb_" + rs.getInt("id")+".jpg";

File file2 = new File(s1);

OutputStream outputStream = new FileOutputStream(file2);

try {

//blob.getBytes的第一个参数是从第几个字节开始提取数据,第二个是提取字节的长度

outputStream.write(blob.getBytes(1, (int) blob.length()));

} catch (IOException e) {

e.printStackTrace();

}

}

basicApp.disConnect();

System.out.println("数据库连接关闭");

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值