JDBC读写Oracle10g的CLOB、BLOB

JDBC读写Oracle10g的CLOB、BLOB
 
环境:
Windows XP Professional 5.1 Build 2600 (Service Pack 3)
Oracle Database 10g Enterprise Edition Release 10.2.0.1.0
http://download.oracle.com/otn/utilities_drivers/jdbc/10201/ojdbc14_g.jar
JDK1.6u16
 
TNS名:
# tnsnames.ora Network Configuration File: d:\oracle\product\10.2.0\client_1\NETWORK\ADMIN\tnsnames.ora
# Generated by Oracle configuration tools.

ORADB =
    (DESCRIPTION =
        (ADDRESS_LIST =
            (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.1.100)(PORT = 1521))
        )
        (CONNECT_DATA =
            (SID = ORCL)
        )
    )

MYORCL =
    (DESCRIPTION =
        (ADDRESS_LIST =
            (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.1.100)(PORT = 1521))
        )
        (CONNECT_DATA =
            (SERVICE_NAME = myorcl)
        )
    )

 
数据脚本:
create table TEST_ORALOB
(
    ID         VARCHAR2(20),
    TSBLOB BLOB not null,
    TSCLOB CLOB not null
)
 
测试代码:
package lavasoft.oralob.common;

import oracle.sql.BLOB;

import java.io.*;
import java.sql.*;

/**
* JDBC读写Oracle10g的CLOB、BLOB
*
* @author: leizhimin 2010-5-26 21:12:55
*/
public class TestOraLob {

         public static void main(String[] args) {
                insertBlob();
                queryBlob();
        }

         public static void insertBlob() {
                Connection conn = DBToolkit.getConnection();
                PreparedStatement ps = null;
                 try {
                        String sql = "insert into test_oralob (ID, TSBLOB, TSCLOB) values (?, ?, ?)";
                        ps = conn.prepareStatement(sql);
                        ps.setString(1, "100");
                         //设置二进制BLOB参数
                        File file_blob = new File( "C:\\a.jpg");
                        InputStream in = new BufferedInputStream( new FileInputStream(file_blob));
                        ps.setBinaryStream(2, in, ( int) file_blob.length());
                         //设置二进制CLOB参数
                        File file_clob = new File( "c:\\a.txt");
                        InputStreamReader reader = new InputStreamReader( new FileInputStream(file_clob));
                        ps.setCharacterStream(3, reader, ( int) file_clob.length());
                        ps.executeUpdate();
                        in.close();
                } catch (IOException e) {
                        e.printStackTrace();
                } catch (SQLException e) {
                        e.printStackTrace();
                } finally {
                        DBToolkit.closeConnection(conn);
                }
        }

         public static void queryBlob() {
                Connection conn = DBToolkit.getConnection();
                PreparedStatement ps = null;
                Statement stmt = null;
                ResultSet rs = null;
                 try {
                        String sql = "select TSBLOB from TEST_ORALOB where id ='100'";
                        stmt = conn.createStatement();
                        rs = stmt.executeQuery(sql);
                         if (rs.next()) {
                                 //读取Oracle的BLOB字段
                                InputStream in = rs.getBinaryStream(1);
                                File file = new File( "c:\\a1.jpg");
                                OutputStream out = new BufferedOutputStream( new FileOutputStream(file));
                                 byte[] buff1 = new byte[1024];
                                 for ( int i = 0; (i = in.read(buff1)) > 0;) {
                                        out.write(buff1, 0, i);
                                }
                                out.flush();
                                out.close();
                                in.close();
                                 //读取Oracle的CLOB字段
                                 char[] buff2 = new char[1024];
                                File file_clob = new File( "c:\\a1.txt");
                                OutputStreamWriter writer = new OutputStreamWriter( new FileOutputStream(file_clob));
                                Reader reader = rs.getCharacterStream(1);
                                 for ( int i = 0; (i = reader.read(buff2)) > 0;) {
                                        writer.write(buff2, 0, i);
                                }
                                writer.flush();
                                writer.close();
                                reader.close();
                        }
                        rs.close();
                        stmt.close();
                } catch (IOException e) {
                        e.printStackTrace();
                } catch (SQLException e) {
                        e.printStackTrace();
                } finally {
                        DBToolkit.closeConnection(conn);
                }
        }
}
 
执行后没有异常,查看C盘,文件已经复制出一份,说明写入和读取都是成功的。
 
 
http://download-west.oracle.com/docs/cd/B12037_01/java.101/b10979/toc.htm
http://www.oracle.com/technology/global/cn/sample_code/tech/java/sqlj_jdbc/files/oracle10g/index.html
http://www.oracle.com/technology/global/cn/sample_code/tech/java/sqlj_jdbc/index.html
http://www.oracle.com/technology/global/cn/sample_code/tech/java/codesnippet/jdbc/index.html
http://www.oracle.com/technology/global/cn/software/tech/java/sqlj_jdbc/index.html
 
如果是将一个字符串写入Clob字段,则需要做简单的变化:
   //设置二进制CLOB参数    
  String xxx = "abcdefg";
  ps.setCharacterStream(3, new StringReader(xxx), xxx.getBytes( "GBK").length);    
  ps.executeUpdate();    
  in.close();
 
 

你可能感兴趣的:(oracle,jdbc,clob,blob,10g)