如何在oracle里面操作BLOB

王朝java/jsp·作者佚名  2006-01-08
窄屏简体版  字體: |||超大  

看了下面的程序就知道了

如果有问题请回:cgguo@163.net

import java.io.*;

import java.util.*;

import java.sql.*;

import oracle.jdbc.driver.*;

import java.text.*;

//

// @author: guo chang guo

// @time: 2003 08 17

//

// this program shows how to put a very big bytes array into oracle table(long raw)

// you can not use PreparedStatement's method setBytes, because setBytes can not

// support very big bytes array, you must use PreparedStatement's method setBinaryStream.

// if you want to use this method, you must transfer your bytes array into inputstream,

// in this example, we use ByteArrayInputStream which can be constructed by a bytes array.

//

// @copyright(R) cvicse

//

//

//the definition of the oracle table :CREATE TABLE bin_data (name VARCHAR(30),data long raw ).

//you can not define the table: CREATE TABLE bin_data (name VARCHAR(30),data blob )

//

//

//we have test this program in oracle 9.2 the file size is 31M

//

public class test

{

public static void main(String args[]) throws java.io.IOException,java.sql.SQLException

{

try

{

Class.forName("oracle.jdbc.driver.OracleDriver");

}

catch (java.lang.ClassNotFoundException e)

{

System.err.print("ClassNotFoundException: " + e.getMessage());

}

try

{

//open a file, put the content of file into a bytes array

File files = new File("c:\\temp\\jdk140.chm");

FileInputStream fis=new FileInputStream(files);

System.out.println(files.getName());

System.out.println((int)files.length());

byte[] fl = new byte[(int)files.length()];

System.out.println(fl.length);

fis.read(fl);

//use this bytes array to construct a InputStream

ByteArrayInputStream kk = new ByteArrayInputStream(fl) ;

//connect the oracle database

Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@eaiserver01:1521:orcl", "system", "admin");

PreparedStatement ps = conn.prepareStatement("INSERT INTO bin_data (name,data)" +"VALUES (?, ?)");

//set parameters

ps.setString(1,files.getName());

ps.setBinaryStream(2, kk,(int)files.length());

//execute

ps.executeUpdate();

fis.close();

ps.close();

// read from the table

int bytesRead = 0;

int byteSum = 0;

byte[] buffer = new byte[8 * 1924];

FileOutputStream fis2 = new FileOutputStream("c:\\temp\\hi.chm");

PreparedStatement ps2 = conn.prepareStatement( "select data from bin_data");

ResultSet rs = ps2.executeQuery();

if (rs != null)

{

while(rs.next())

{

InputStream is = rs.getBinaryStream(1);

while ((bytesRead = is.read(buffer)) != -1)

{

byteSum += bytesRead;

fis2.write(buffer, 0, bytesRead);

}

fis2.close( );

}

rs.close( );

}

ps2.close( );

}

catch(Exception e)

{

System.out.println("errror :"+e.toString() );

e.printStackTrace();

}

}

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有  導航