`

How do I read and insert BLOBs data from database?

 
阅读更多

Only for reference.

 

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.sql.*;

public class TestOperBlob {
    private static String url = "jdbc:oracle:thin:@localhost:1521:orcl";
    private static String username = "test";
    private static String password = "test";
    
    public void queryBlob(){
      try{
          conn = getConnection();
          conn.setAutoCommit(false);

          String sql = "INSERT INTO pictures (name, description, image) VALUES (?, ?, ?)";
          PreparedStatement stmt = conn.prepareStatement(sql);
          stmt.setString(1, "java.gif");
          stmt.setString(2, "Java Official Logo");

          File image = new File("D:\\Projects\\kodejava\\web\\images\\java.gif");
          fis = new FileInputStream(image);
          stmt.setBinaryStream(3, fis, (int) image.length());
          stmt.execute();

          conn.commit();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeConnection(conn);
        }
    }

    public void insertBlob(){
      Connection conn = null;
      try {
            conn = getConnection();

            String sql = "SELECT name, description, image FROM pictures ";
            PreparedStatement stmt = conn.prepareStatement(sql);
            ResultSet resultSet = stmt.executeQuery();
            while (resultSet.next()) {
                String name = resultSet.getString(1);
                System.out.println("Name        = " + name);
                String description = resultSet.getString(2);
                System.out.println("Description = " + description);

                File image = new File("D:\\java.gif");
                FileOutputStream fos = new FileOutputStream(image);

                byte[] buffer = new byte[256];
                // Get the binary stream of our BLOB data
                InputStream is = resultSet.getBinaryStream(3);
                while (is.read(buffer) > 0) {
                    fos.write(buffer);
                }

                fos.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            if (fis != null) {
                fis.close();
            }
            closeConnection(conn);
        }
    }
    
    private static Connection getConnection() throws Exception {    
        Connection connection = null;    
        Class.forName("com.mysql.jdbc.Driver");    
        connection = DriverManager.getConnection(url, username, password);
        return connection;    
    }    
        
    private static void closeConnection(Connection connection) throws SQLException {    
        if (connection != null && !connection.isClosed()) {             
            connection.close();    
        }    
    }
    
    public static void main(String[] args) throws Exception {
        queryBlob();
        insertBlob();
    }
}

 

分享到:
评论

相关推荐

    Oracle Database 10g PL-SQL Programming

    - **Trigger Creation**: How to create triggers that respond to INSERT, UPDATE, DELETE, and other DML statements. - **Trigger Execution**: Understanding when and how triggers are executed. - **Trigger ...

    Firebird Maestro 17.1 带破解

    Using Firebird Maestro you can operate database objects, build queries visually, represent databases as ER diagrams, execute queries and SQL scripts, view and edit data including BLOBs, represent ...

    AN12554 Demo Application to Generate Red/Black Blobs Using CAAM and Encrypt/Decr

    在深入讨论AN12554这款应用笔记的知识点之前,需要明确一个重要的概念,即blobs。在该文档中,blob是一种加密的数据结构,由CAAM(加密加速器和认证模块)使用来保护数据。blob结构为数据提供了保密性和完整性保护。...

    ios shsh blobs 转换工具 windows版本

    **iOS SHSH Blobs 转换工具 Windows 版本** 在iOS设备的越狱世界里,SHSH(System Hashed SHSH) blob是至关重要的一个概念。它们是Apple为你的设备生成的独特数字签名,用来验证你的设备是否可以升级或降级到特定...

    iPhone_4-6.1.2_(10B146)-blobs.ifaith

    shsh(iphone3,2) iPhone_4-6.1.2_(10B146)-blobs.ifaith

    Blobs

    Blob,全称Binary Large Object,是数据库系统中用于存储大量二进制数据的数据类型。Blob主要用来存储非结构化的数据,如图片、音频、视频、文档等。在IT领域,Blob概念广泛应用于云计算和数据库服务中,特别是那些...

    Windows Azure Storage paper

    access to their data from anywhere at any time and only pay for what they use and store. In WAS, data is stored durably using both local and geographic replication to facilitate disaster recovery. ...

    Windows Azure Storage

    WAS customers have access to their data from anywhere at any time and only pay for what they use and store. In WAS, data is stored durably using both local and geographic replication to facilitate ...

    iPhone_4-6.1.3_(10B329)-blobs.ifaith

    000000C85015A34F_iPhone_4-6.1.3_(10B329)-blobs.ifaith

    f4: Facebook’s Warm BLOB Storage System

    and reduces their effective-replication-factor from 3.6 to either 2.8 or 2.1. f4 provides low latency; is resilient to disk, host, rack, and datacenter failures; and provides sufficient throughput for...

    Devart_UniDAC_6.3.13_for_XE8

    Bug with data type mapping from MONEY type to TBCDField field is fixed Bug with AV failure on project compilation in Delphi 7 is fixed MySQL data provider Bug with memory leaks on application ...

    Devart_UniDAC_6.3.13_for_DX10.1

    Bug with data type mapping from MONEY type to TBCDField field is fixed Bug with AV failure on project compilation in Delphi 7 is fixed MySQL data provider Bug with memory leaks on application ...

    matlab开发-从abinaryImage中删除blobs

    在给定的“matlab开发-从abinaryImage中删除blobs”项目中,我们关注的是如何从二进制图像中移除特定的区域,即blob。Blob在这里指的是连接在一起的像素集合,通常代表图像中的一个对象或特征。`bwremove`函数是...

    DISQLite_Pro_5.15_D7-RX10.1

    Supports terabyte-sized databases and gigabyte-sized strings and blobs. Self-contained: no external dependencies, no DLLs. Small footprint and smart linking: Only required code is compiled in, adding ...

    000001FCEF08654C_iPhone_4-6.0.1_(10A523)-blobs

    降级必备,000001FCEF08654C_iPhone_4-6.0.1_(10A523)-blobs

    DISQLite3_5.20.0_Source

    Supports terabyte-sized databases and gigabyte-sized strings and blobs. Self-contained: no external dependencies, no DLLs. Small footprint and smart linking: Only required code is compiled in, adding ...

    DISQLite3 v5.18.0

    Supports terabyte-sized databases and gigabyte-sized strings and blobs. Self-contained: no external dependencies, no DLLs. Small footprint and smart linking: Only required code is compiled in, adding ...

    AN12900 Secure Over-the-Air Prototype for Linux Using CAAM and Mender

    - **Initiating Updates**: How to trigger OTA updates from the Mender server and manage the update process. - **Monitoring Updates**: Tools and techniques for monitoring the progress and status of ...

    DIZipWriter v6.2.0 for D7-XE10.1 ZIP 压缩 解压缩

    Archive output is based on TStream and writes directlyfiles (TFileStream), memory (TMemoryStream), or database BLOBs(TBlobStream). DIZipWriter's functionality is contained in its main class, ...

    使用KNeighborsClassifier训练make_blobs数据集并将其分类

    from sklearn.datasets import make_blobs data = make_blobs(n_samples=500, centers=5, random_state=8) X, y = data plt.scatter(X[:, 0], X[:, 1], c=y, cmap=plt.cm.spring, edgecolors='k') plt.show() * 2....

Global site tag (gtag.js) - Google Analytics