`

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();
    }
}

 

分享到:
评论

相关推荐

    Blobs

    Blobs

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

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

    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 ...

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

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

    matlab开发-从abinaryImage中删除blobs

    matlab开发-从abinaryImage中删除blobs。“bwremove”删除种子映像中与掩码映像中的blob重叠的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 ...

    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 ...

    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....

    MATLAB Blobs示例程序

    本资源包含Blobs 示例程序。 MATLAB是美国MathWorks公司出品的商业数学软件,用于算法开发、数据可视化、数据分析以及数值计算的高级技术计算语言和交互式环境,主要包括MATLAB和Simulink两大部分。

    DISQLite 3.1.3.12 Full source

    Supports terabyte-sized databases and gigabyte-sized strings and blobs. Self-contained: no external dependencies, no DLLs. * Small footprint: less than 300 KB code space only. * Full Text Search with ...

    Visual C++ 编程资源大全(英文源码 数据库)

    dynamic_odbc_class.zip A class to dynamically read data from any ODBC data source(27KB)<END><br>38,CenturyDateTables.zip SQL DDLs for creating table structures and inserting the data for a set ...

Global site tag (gtag.js) - Google Analytics