`

向oracle中插入BLOB对象

阅读更多

package oracle.otnsamples.jdbc.lobtype;

// Package for JDBC classes
import java.sql.Connection;
import java.sql.Clob;
import java.sql.ResultSet;
import java.sql.Blob;
import java.sql.Statement;
import java.sql.SQLException;
import java.sql.PreparedStatement;
import oracle.jdbc.pool.OracleDataSource;

// Java Utility Classes
import java.util.Properties;
import java.util.ResourceBundle;
import java.util.Enumeration;

// Package for using Streams
import java.io.IOException;
import java.io.OutputStream;
import java.io.File;
import java.io.Reader;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.Writer;
import java.io.FileReader;
import java.io.FileInputStream;
/**
* This sample illustrates access and manipulation of CLOB and BLOB columns
* using JDBC2.0. Oracle's JDBC2.0 driver, provides API to perform selects,
* updates, inserts and deletes from LOB columns. Here the methods specified
* by the JDBC 2.0 API specifications are used for database operations.
*
* The sample illustrates the above operations on CLOB and BLOB columns in the
* OTN_AIRPORT_LOB_DETAILS table. It inserts sample .gif and .txt file contents,
* into the LOB columns for the chosen airport. If LOB data already exists for
* the chosen airport, it retrieves and displays them. It also
* illustrates manipulation of LOB columns, by allowing users to enter text to
* be appended to the CLOB column.
*/

public class LOBSample {

  /** Database Connection Object */

  Connection connection;    
 
  /** The GUI handler for the sample */
  LOBFrame   gui;            


  /**
   *  Constructor. Initializes GUI
   */

  public LOBSample() {
    gui = new LOBFrame(this);
    gui.setVisible(true);      
  }

  /**
   *  Main entry point for the class. Instantiates the root frame,
   *  sets up the database connection and populates the JTable with
   *  OTN_AIRPORTS rows
   */

  public static void main(String[] args) {
    LOBSample lobs = new LOBSample(); // Instantiate root frame
    lobs.dbConnection();              // Setup db connection
    if( lobs.connection != null ) {
      lobs.checkTables();   // Check if table exist, if not create it
      lobs.populateAirports();    // Populate the JTable with all airports rows
    }
  }

  /**
   *  Dispatches the GUI events to the appropriate method, which performs
   *  the required JDBC operations. This method is invoked when event occurs
   *  in the GUI (like table Selection, Button clicks etc.).
   */

  public void dispatchEvent( String eventName ) {
    // Dispatch Event
    if( eventName.equals("Load Sample Files"))
      loadSamples(gui.getSelectedCode());
    else if( eventName.equals("Add Suggestions") ) {
      String suggestions = gui.getSuggestionText();
      if( !suggestions.equals("CANCEL")) {
        addSuggestions(gui.getSelectedCode(), suggestions);
        gui.sugArea.append(new String(suggestions));
       }
    } else if( eventName.equals("Airport Selected in Table") )
      airportSelected(gui.getSelectedCode());
    else if( eventName.equals("EXIT") )
      exitApplication();
  }

  /**
   * This method reads a properties file which is passed as
   * the parameter to it and load it into a java Properties
   * object and returns it.
   */


  private static Properties loadParams( String file ) throws IOException {
    // Loads a ResourceBundle and creates Properties from it
    Properties prop = new Properties();
    ResourceBundle bundle = ResourceBundle.getBundle( file );
    Enumeration enum = bundle.getKeys();
    String key = null;
    while( enum.hasMoreElements() ) {
      key = (String)enum.nextElement();
      prop.put( key, bundle.getObject( key ) );
    }
    return prop;
  }

  /**
   * Creates a database connection object using DataSource object. Please
   * substitute the database connection parameters with appropriate values in
   * Connection.properties file
   */

  private void dbConnection() {
    try {
      gui.putStatus("Trying to connect to the Database");

      // Load the properties file to get the connection information
      Properties prop = this.loadParams("Connection");

      // Create a OracleDataSource instance
      OracleDataSource ods = new OracleDataSource();

      // Sets the driver type
      ods.setDriverType("thin");

      // Sets the database server name
      ods.setServerName((String)prop.get("HostName"));

      // Sets the database name
      ods.setDatabaseName((String)prop.get("SID"));

      // Sets the port number
      ods.setPortNumber(new Integer((String)prop.get("Port")).intValue());

      // Sets the user name
      ods.setUser((String)prop.get("UserName"));

      // Sets the password
      ods.setPassword((String)prop.get("Password"));

      // Create a connection  object
      connection = ods.getConnection();

      // Sets the auto-commit property for the connection to be false.
      connection.setAutoCommit(false);

      gui.putStatus(" Connected to " + prop.get("SID") +
                    " Database as " + prop.get("UserName"));

    } catch(SQLException ex) { // Trap SQL errors
        gui.putStatus(
                     "Error in Connecting to the Database "+'\n'+ex.toString());
    } catch(IOException ex) { // Trap I/O errors
        gui.putStatus(
                     "Error in reading the properties file "+'\n'+ex.toString());
    }
  }

  /**
   * Queries all rows from the OTN_AIRPORTS table and populates the JTable with
   * the returned rows
   */


  private void populateAirports() {
    Statement stmt = null;
    try {
      gui.appendStatus("\nPopulating Airports. Please wait...");

      // Statement object for issuing SQL statements
      stmt = connection.createStatement();

      // Execute the query that returns all airport rows
      ResultSet resultSet = stmt.executeQuery(
                         "SELECT airport_code, description, name"+
                         " FROM otn_airports");

      // Loop through the result set and populate JTable with all airports
      while( resultSet.next() ) {
        // Retrieve column values for this row
        String code = resultSet.getString(1);
        String desc = resultSet.getString(2);
        String city = resultSet.getString(3);

        gui.addToJTable(code, desc, city); // Insert into Jtable
      }
      gui.putStatus("Connected to database and retrieved all airport rows");
      gui.appendStatus("\nPlease Choose an airport ");
    } catch( SQLException ex ) {  // Trap SQL errors
      gui.putStatus("Error Querying OTN_AIRPORTS table: \n" + ex.toString());
    } finally {
      try {
        stmt.close(); // Close statement which also closes open result sets
      } catch(SQLException ex) {
       
      }
    }
  }

  /**
   * Creates a new row for the selected airport in OTN_AIRPORT_LOB_DETAILS.
   * It then loads the sample files into the LOB columns, using JDBC2.0.
   */

  private void loadSamples( String airportCode ) {
    PreparedStatement pstmt = null;
    Statement stmt = null;
    try {
      gui.putStatus("Creating row for airport in OTN_AIRPORT_LOB_DETAILS.. ");

      // Insert a row into OTN_AIRPORT_LOB_DETAILS with
      // LOB column values are initialized to empty
      pstmt = connection.prepareStatement(
        "INSERT INTO OTN_AIRPORT_LOB_DETAILS( airport_code, airport_map,"+
        "airport_sug_book) VALUES(? , EMPTY_BLOB() , EMPTY_CLOB())");
     
      pstmt.setString(1, airportCode); // Bind AIRPORT code
      pstmt.execute();                 // Execute SQL statement

      gui.appendStatus("Created.\n Loading <map.gif> into Blob column for airport...");

      // Retrieve the row just inserted, and lock it for insertion of the
      // LOB columns.
      stmt = connection.createStatement();
      ResultSet lobDetails = stmt.executeQuery(
          "SELECT airport_map, airport_sug_book FROM OTN_AIRPORT_LOB_DETAILS "+
          "WHERE airport_code = '" + airportCode + "' FOR UPDATE");

      // Load the properties file to get the sample files information
      Properties prop = this.loadParams("Misc");
      String mapFileName     = (String)prop.get("map");
      String sugBookFileName = (String)prop.get("suggestions");

      // Retrieve Blob and Clob streams for AIRPORT_MAP and AIRPORT_SUG_BOOK
      // columns, and load the sample files
      if( lobDetails.next() ) {
        // Get the Blob locator and open output stream for the Blob
        Blob mapBlob = lobDetails.getBlob(1);
        OutputStream blobOutputStream = ((oracle.sql.BLOB)mapBlob).getBinaryOutputStream();


        // Open the sample file as a stream for insertion into the Blob column
        File mapFile = new File(mapFileName);
        InputStream sampleFileStream = new FileInputStream(mapFile);

        // Buffer to hold chunks of data to being written to the Blob.
        byte[] buffer = new byte[10* 1024];

        // Read a chunk of data from the sample file input stream, and write the
        // chunk to the Blob column output stream. Repeat till file has been
        // fully read.
        int nread = 0;   // Number of bytes read
        while( (nread= sampleFileStream.read(buffer)) != -1 ) // Read from file
          blobOutputStream.write(buffer, 0, nread);         // Write to Blob

        // Close both streams
        sampleFileStream.close();
        blobOutputStream.close();

        // Load the suggestion book sample file into the Clob column
        gui.appendStatus("Done\nLoading <suggestionbook.txt> into Clob column ..");


        // Get the Clob locator and open an output stream for the Clob
        Clob sugBookClob = lobDetails.getClob(2);
        Writer clobWriter = ((oracle.sql.CLOB)sugBookClob).getCharacterOutputStream();

        // Open the sample file as a stream for insertion into the Clob column
        File sugbookFile = new File(sugBookFileName);
        FileReader sugFileReader = new FileReader(sugbookFile);

        // Buffer to hold chunks of data to being written to the Clob.
        char[] cbuffer = new char[10* 1024];

        // Read a chunk of data from the sample file input stream, and write the
        // chunk into the Clob column output stream. Repeat till file has been
        // fully read.
        nread = 0;
        while( (nread= sugFileReader.read(cbuffer)) != -1 ) // Read from File
          clobWriter.write( cbuffer, 0, nread);          // Write to Clob

        // Close both streams
        sugFileReader.close();
        clobWriter.close();

        gui.putStatus("Done Loading sample files");

        gui.appendStatus("\nRetrieving and displaying sample files..");
        // Retrieve and display the LOB data just inserted
        drawBlob(mapBlob, airportCode);
        writeClob(sugBookClob, airportCode);
        gui.putStatus("Done loading and displaying LOB data");
      }
    } catch( Exception ex ) { // Trap SQL errors
      gui.putStatus("Error loading sample files for the selected airport");
      gui.appendStatus("\n" + ex.toString());
    } finally {
      try {
        pstmt.close(); // Close PreparedStatement
        stmt.close(); // Close statement which also closes open result sets
      } catch(SQLException ex) {
       
      }
    }
  }

  /**
   * Accepts suggestions from an JOptionPane and appends the entered
   * suggestions to the Clob column
   */

  private void addSuggestions(String airportCode, String suggestions) {
    Statement stmt = null;
    try {
      gui.putStatus(" Appending entered suggestions to Clob column. Please wait...");
      stmt = connection.createStatement(); // Prepare a statement

      // Retrieve the Clob locator and also lock the row, for the selected
      // Airport
      ResultSet lobDetails = stmt.executeQuery(
                          "SELECT airport_sug_book "+
                          "FROM OTN_AIRPORT_LOB_DETAILS "+
                          "WHERE airport_code ='" + airportCode + "' FOR UPDATE");

      // Obtain the Clob locator and append suggestions
      if( lobDetails.next() ) {
        // Get the Clob locator
        Clob clob = lobDetails.getClob(1);

        // Append the entered suggestions to the end of the CLOB data.
        // The first parameter to putString, is the offset from which to start
        // writing, and the second parameter is the data to be written.
        // clob.length(), returns the length of the data in the CLOB column
        ((oracle.sql.CLOB)clob).putString(((oracle.sql.CLOB)clob).length() + 1,
                                             suggestions);
        gui.appendStatus(" Done");
      }
    } catch( SQLException ex ) {
        gui.putStatus("Error appending suggestions to the Clob column");
      gui.appendStatus("\n" + ex.toString());
    } finally {
      try {
        stmt.close(); // Close statement which also closes open result sets
      } catch(SQLException ex) {
       
      }
    }
  }

  /**
   * This method is called when a row is selected from the airport JTable.
   * It checks if there exists data in OTN_AIRPORT_LOB_DETAILS for the selected
   * airport. If there exists data, it calls drawBlob and writeClob to
   * display the data
   */

  private void airportSelected(String airportCode) {
    Statement stmt = null;
    try {
      gui.putStatus("Retrieving LOB details for selected airport..");
      // Create a SQL statement
      stmt = connection.createStatement();

      // Query OTN_AIRPORT_LOB_DETAILS for the selected AIRPORT
      ResultSet lobDetails = stmt.executeQuery(
               "SELECT airport_map, airport_sug_book FROM OTN_AIRPORT_LOB_DETAILS "+
               "WHERE airport_code = '" + airportCode + "'");

      // Check if LOB columns exist
      if( lobDetails.next() ) {
        // LOB details exist

        // Display airport map and suggestion book (LOB details)
        drawBlob(lobDetails.getBlob(1), airportCode);
        writeClob(lobDetails.getClob(2), airportCode);
        gui.putStatus("Done retrieving and displaying LOB details");
      } else {
        // No LOB details
        gui.loadButton.setEnabled(true);
        gui.putStatus(" No airport map and suggestion book exist for selected airport");
        gui.appendStatus("\n Press <Load Sample Files> to load LOB details");
      }
    } catch( Exception ex ) { // Trap SQL errors
      gui.putStatus("Error retrieving LOB Details for the selected airport");
      gui.appendStatus(ex.toString());
    } finally {
      try {
        stmt.close(); // Close statement which also closes open result sets
      } catch(SQLException ex) {
       
      }
    }
  }

  /**
   * Retrieve the Blob data from input Blob column into a local file,
   * and draws the image
   */

  private void drawBlob(Blob blob, String airPCode) {
    try {
      // Open a stream to read the Blob data
      InputStream blobStream = blob.getBinaryStream();


      // get user home folder name
      String userHome = System.getProperty("user.home");

      // append the file name with user home directory, file separator and
      // file extension GIF
      String fileName = userHome+File.separator+airPCode+".gif";  

      // Open a file stream to save the Blob data
      FileOutputStream fileOutStream = new FileOutputStream(fileName);

      // Read from the Blob data input stream, and write to the file output
      // stream
      byte[] buffer = new byte[10]; // buffer holding bytes to be transferred
      int nbytes = 0// Number of bytes read
      while( (nbytes = blobStream.read(buffer)) != -1 ) // Read from Blob stream
        fileOutStream.write(buffer, 0, nbytes);     // Write to file stream

      // Flush and close the streams
      fileOutStream.flush();
      fileOutStream.close();
      blobStream.close();

      gui.drawMap(fileName); // Draw retrieved image to GUI
    } catch( Exception ex ) { // Trap SQL and IO errors
      gui.putStatus(" Error in retrieving and drawing map for selected airport");
      gui.appendStatus("\n" + ex.toString());
    }
  }

  /**
   * Retrieve the character data from the input Clob, save in a
   * StringBuffer and display the StringBuffer contents in GUI
   */

  void writeClob(Clob clob, String airPCode) {
    try {
      // Open a stream to read Clob data
      Reader clobStream = clob.getCharacterStream();

      // Holds the Clob data when the Clob stream is being read
      StringBuffer suggestions = new StringBuffer();

      // Read from the Clob stream and write to the stringbuffer
      int    nchars = 0; // Number of characters read
      char[] buffer = new char[10];  //  Buffer holding characters being transferred

      while( (nchars = clobStream.read(buffer)) != -1 ) // Read from Clob
        suggestions.append(buffer, 0, nchars);        // Write to StringBuffer

      clobStream.close();  // Close the Clob input stream
      gui.sugArea.append(new String(suggestions)); // Display in GUI
    } catch( Exception ex ) { // Trap SQL and IO errors
      gui.putStatus("Error in getting and drawing Clob for the airport, "+ airPCode +":");
      gui.appendStatus(ex.toString());
    }
  }

  /**
   *  Close the database Connection and exit the application
   */

  public void exitApplication() {
    try {
      gui.putStatus("Closing the connection....please wait.....");
      if( connection != null )
        connection.close(); //Close the connection object.
    } catch( SQLException ex ) { //Trap SQL Errors
      gui.putStatus(ex.toString());
    }
    System.exit(0); //Exit the application
  }
  /**
   * Checks if the tables ('OTN_AIRPORTS' and 'OTN_AIRPORT_LOB_DETAILS')
   * are present, else creates it.
   * Look into PopulateTable.java for more details
   */

  private void checkTables()   {
    Statement stmt = null;
    ResultSet rset = null;
    PopulateTable popTable = null;
    try     {
      stmt = connection.createStatement();
     
      // check from User_tables data dictionary table if the table is existing.
      rset = stmt.executeQuery(" SELECT Table_Name FROM User_Tables "+
                               " WHERE Table_Name = 'OTN_AIRPORTS' ");

      // if the resultset of the above query does not have any record, it means
      // OTN_AIRPORTS table is not existing. So the table is created.
      if (!rset.next()) {
        // call the class to create the table
        if (popTable == null)
          popTable = new PopulateTable(connection,gui);
        popTable.createSchemaTable();
        gui.putStatus("OTN_AIRPORTS Table created.");                 
      }
      // check from User_tables data dictionary table if the table is existing.
      rset = stmt.executeQuery(" SELECT Table_Name FROM User_Tables "+
                               " WHERE Table_Name = 'OTN_AIRPORT_LOB_DETAILS' ");

      // if the resultset of the above query does not have any record, it means
      // OTN_AIRPORT_LOB_DETAILS table is not existing. So the table is created.
      if (!rset.next()) {
        // call the class to create the table
       
        if (popTable == null)
          popTable = new PopulateTable(connection,gui);
        popTable.createLOBTable();
        gui.putStatus("OTN_AIRPORT_LOB_DETAILS Table created.");                 
      }
    } catch (SQLException sqlEx) {
       gui.putStatus("Could not create required tables : "+sqlEx.toString());
    } finally  {
        try {
          if( rset != null ) rset.close( );
          if( stmt != null ) stmt.close( );
        } catch(SQLException ex) { }
    }
  }
}

分享到:
评论

相关推荐

    oracle中的BLOB(照片)转换到mysql中

    然后,通过类似的方法,读取本地文件并将其内容插入到MySQL的BLOB字段中。在Java中,你可以使用`PreparedStatement`的`setBinaryStream()`方法: ```java FileInputStream in = new FileInputStream(...

    在Oracle中存取BLOB对象实现文件的上传和下载.txt

    ### 在Oracle中存取BLOB对象实现文件的上传和下载 #### 核心知识点解析: **一、BLOB数据类型简介** BLOB(Binary Large Object)是数据库中的二进制大对象类型,主要用于存储大量二进制数据,如图像、音频、视频等...

    oracle中读取blob字段.doc

    这通常涉及到从本地文件中读取数据,然后写入到BLOB对象中。 ```java // 读取本地文件数据 InputStream is = new FileInputStream("path/to/local/file"); byte[] buffer = new byte[10240]; int bytesRead; while ...

    mysql oracle 插入blob数据实例源码

    本篇文章将深入探讨如何在MySQL和Oracle数据库中插入Blob数据,并提供相关的源码实例。 一、MySQL中的Blob数据插入 MySQL提供了多种方式插入Blob数据,包括使用PreparedStatement接口。以下是一个Java源码示例: ...

    java对oracle数据库中blob字段的处理

    为了解决这个问题,可以采用一种间接的方法:首先在本地数据库创建一个临时表,并将BLOB数据插入到该临时表中;然后,通过DBLink从远程数据库选择并操作这些数据。这种方法避免了直接通过DBLink处理大对象数据的问题...

    C#访问oracle数据库类,带blob字段读写

    写入Blob字段时,先创建`OracleParameter`对象,将其Direction设置为` ParameterDirection.Input `,OracleDbType设置为`OracleDbType.Blob`,然后赋值。以下是一个示例: ```csharp // 读取Blob字段 using ...

    oracle blob 字段 读写 delphi

    Oracle数据库中的BLOB(Binary Large Object)字段是用来存储大量二进制数据的,例如图片、文档或音频文件。在Delphi编程环境中,处理这类数据时需要掌握特定的API调用和方法。本篇文章将深入探讨如何在Delphi中对...

    oracle中的Blob和Clob区别

    ### Oracle中的BLOB和CLOB的区别 在Oracle数据库中,`BLOB`(Binary Large Object)和`CLOB`(Character Large Object)是用于存储大量数据的两种特殊数据类型。这两种类型都属于`LOB`(Large Object)类别,主要...

    如何将DB2数据库中的大对象(BLOB,CLOB)数据类型复制到Oracle数据库中

    本例中,我们讨论如何将DB2数据库中的BLOB数据类型复制到Oracle数据库。 1. **BLOB与CLOB数据类型**: BLOB(Binary Large Object)存储二进制大对象,如图像、音频或视频文件。CLOB(Character Large Object)...

    blob_oracle.rar_blob_blob oracle_c++ oracle blob_oracle_oracle

    4. **插入BLOB数据**:使用`OCIBlobOpen`打开BLOB对象,然后调用`OCIBlobWrite`将二进制数据写入BLOB字段。这可能涉及从文件读取数据,例如"www.pudn.com.txt"或"row.txt"。 5. **执行SQL语句**:使用`OCIParse`...

    oracle blob转文件读取

    在软件开发过程中,尤其是在处理数据库中的二进制大对象(Binary Large Object, 简称BLOB)时,经常需要将数据库中的BLOB数据转换为文件进行读取或处理。本文将详细介绍如何使用C#语言结合Oracle数据库实现这一功能...

    Oracle中Blob字段的写入处理

    此文档是对于oracle数据库中blob类型字段二进制大对象的读取和解析

    java中读取ORACLE中BLOB字段存储的图片

    // 从结果集中获取BLOB对象 java.sql.Blob blob = res.getBlob("zp"); InputStream inStream = blob.getBinaryStream(); try { // 获取图片数据长度 long nLen = blob.length(); int nSize = (int) nLen; ...

    JDBC操纵Oracle数据库中的BLOB字段

    本文将深入探讨如何使用Java Database Connectivity (JDBC) API来操作Oracle数据库中的BLOB字段,包括创建表、插入和读取BLOB数据的方法。 #### 创建包含BLOB字段的表 在Oracle数据库中创建一个包含BLOB字段的表...

    使用JDBC4.0操作Oracle中BLOB类型的数据方法

    这段代码首先创建一个Blob对象,然后从文件系统读取文件,并将其内容写入Blob对象。最后,通过预编译的SQL语句插入到数据库中。 要从数据库中读出文件,可以使用以下Java代码: ```java public void readBlob() ...

    利用Java向Oracle中插入图片&#40;BLOB&#41;文件.doc

    本文将详细介绍如何使用Java通过JDBC向Oracle数据库中插入图片文件的方法。此过程涉及到的主要技术包括Java编程语言、Oracle数据库以及JDBC驱动。为了实现这一目标,我们将从创建数据库表开始,然后逐步介绍如何使用...

    Oracle blob字段上传下载

    在数据库系统中,二进制大对象(Binary Large Object,简称BLOB)是一种用于存储大量二进制数据的数据类型,例如图像、音频或视频文件等。Oracle数据库支持Blob类型,允许用户在数据库中直接存储和管理这些大型文件。...

    在vb.net中对oracle中的blob的操作.pdf

    - **插入数据**:在Insert按钮的事件处理程序中,创建一个新的OracleCommand对象,设置SQL插入语句,可能包括使用OracleParameter对象处理BLOB数据。这里,将文件读取为字节数组,然后将其作为参数传递给命令对象。...

    图片存入Oracle中,用clob和blob两种方式

    在数据库管理中,存储非结构化数据...综上所述,`CLOB`和`BLOB`都是在Oracle中存储图片的有效方法,具体选择哪种方式取决于应用场景和需求。`ClobAndBlob`工具提供了一种便捷的方式来体验和比较这两种方式的使用效果。

    oracle Blob

    插入Blob数据时,首先需要插入其他非Blob列的值,然后对Blob列使用`empty_blob()`函数创建一个空的Blob对象。例如: ```sql INSERT INTO tLob VALUES (1, 'Gene', empty_clob(), empty_blob(), bfilename('MYDIR'...

Global site tag (gtag.js) - Google Analytics