You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过JDBC从MySQL数据库中获取Blob数据?

通过JDBC从MySQL获取Blob数据的实现方案

嘿,我来帮你搞定通过JDBC从MySQL读取Blob数据的事儿,结合你提供的那段获取房间详情的代码,我给你调整并补充完整Blob数据的读取逻辑:

完整代码示例

import java.sql.*;
import java.util.LinkedList;

// 假设你的RoomDetails类有对应的setter方法,比如setRoomImage(byte[])或者setRoomImage(InputStream)
@Override
public LinkedList<RoomDetails> getRoomsDetails(String hotelId) {
    System.out.println("进入获取房间详情流程");
    LinkedList<RoomDetails> list = new LinkedList<>();
    // 声明资源变量,方便后续统一关闭
    Connection con = null;
    Statement stmt = null;
    ResultSet rs = null;

    try {
        // 1. 注册MySQL驱动(MySQL 5.x用com.mysql.jdbc.Driver,8.x以上用com.mysql.cj.jdbc.Driver)
        Class.forName("com.mysql.jdbc.Driver");
        // 2. 建立数据库连接
        con = DriverManager.getConnection(url, "j2ee", "tiger");
        // 3. 构造查询SQL,建议用PreparedStatement防止SQL注入,这里先按你的写法调整,后面会提优化点
        String query = "select * from roomDetails where hotelId='" + hotelId + "'";
        stmt = con.createStatement();
        rs = stmt.executeQuery(query);

        while (rs.next()) {
            RoomDetails room = new RoomDetails();
            room.setHotelId(rs.getString("hotelId"));
            // --- 重点:读取Blob数据 ---
            // 方式1:获取Blob对象,转成字节数组(适合数据量不大的情况)
            Blob roomBlob = rs.getBlob("room_image"); // 替换成你的Blob字段名
            if (roomBlob != null) {
                byte[] imageBytes = roomBlob.getBytes(1, (int) roomBlob.length());
                room.setRoomImage(imageBytes); // 假设RoomDetails有对应的setter
            }

            // 方式2:获取输入流(适合大文件,比如大图片、视频,减少内存占用)
            // InputStream blobInputStream = rs.getBinaryStream("room_image");
            // room.setRoomImageInputStream(blobInputStream); // 对应RoomDetails的输入流setter

            // 其他字段继续设置
            // ...
            list.add(room);
        }
    } catch (ClassNotFoundException e) {
        System.err.println("找不到MySQL驱动:" + e.getMessage());
        e.printStackTrace();
    } catch (SQLException e) {
        System.err.println("数据库操作异常:" + e.getMessage());
        e.printStackTrace();
    } finally {
        // 4. 务必关闭资源,顺序是ResultSet -> Statement -> Connection
        try {
            if (rs != null) rs.close();
            if (stmt != null) stmt.close();
            if (con != null) con.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    return list;
}

关键注意事项

  • 驱动版本兼容:如果是MySQL 8.0及以上版本,驱动类要改成com.mysql.cj.jdbc.Driver,同时URL需要添加时区参数,比如jdbc:mysql://localhost:3306/your_db?serverTimezone=UTC
  • SQL注入风险:你原来的SQL拼接方式存在注入漏洞,建议改用PreparedStatement,示例如下:
    String query = "select * from roomDetails where hotelId = ?";
    PreparedStatement pstmt = con.prepareStatement(query);
    pstmt.setString(1, hotelId);
    rs = pstmt.executeQuery();
    
  • Blob数据读取方式选择:
    • 字节数组方式:适合小体积Blob(比如小图标),直接把数据加载到内存
    • 输入流方式:适合大体积Blob(比如高清图片、文档),可以边读边处理,避免内存溢出
  • 资源关闭:一定要在finally块中关闭ResultSet、Statement、Connection,避免数据库连接泄漏

内容的提问来源于stack exchange,提问作者izaz

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 06:39:04