jdbc操作blob

package demo;
import java.sql.*;
import java.io.*;
import java.sql.PreparedStatement;
import java.util.*;

import oracle.jdbc.driver.*;
import oracle.sql.BLOB;


/**
* Insert record in the MEDIA table
* MEDIA (file_name varchar2(256), file_content BLOB);
*/
public class BlobOracle
{
private final static String hostname = "localhost";
private final static String port = "1521";
private final static String sid = "ORCL";
private final static String username = "scott";
private final static String password = "tiger";
private static String fileLocation;
private static Connection connection;

public BlobOracle()
{
}

/**
*
* @param args
*/
public static void main(String[] args)
{
try
{
if (args.length == 0)
{
System.out.println("\n\n Usage demo.BlobOracle ");
System.exit(0);
}

fileLocation = args[0];

setConnection();
insertBLOB();

} catch (Exception ex)
{
ex.printStackTrace();
} finally
{
}
}


private static void setConnection() throws SQLException
{
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
connection = DriverManager.getConnection("jdbc:oracle:thin:@"+hostname+ ":"+ port +":"+ sid , username , password);
connection.setAutoCommit(false); // we must control the commit
}

private static void insertBLOB() throws SQLException, Exception
{
BLOB blob;
File file ;
FileInputStream is;
OutputStream os;

long ts1 = System.currentTimeMillis();


//Create a statement.
PreparedStatement pstmt = connection.prepareStatement("insert into media (file_name, file_content) values (?,empty_blob())");
file = new File(fileLocation);
String fileName = file.getName();
//Set the file name and execute the query
pstmt.setString(1, fileName);
pstmt.execute();

//Take back the record for update (we will insert the blob)
//supposely the file name is the PK
pstmt = connection.prepareStatement("select file_content from media where file_name = ? for update");
pstmt.setString(1, fileName);

//Execute the query, and we must have one record so take it
ResultSet rset = pstmt.executeQuery();
rset.next();

//Use the OracleDriver resultset, we take the blob locator
blob = ((OracleResultSet)rset).getBLOB("file_content");

is = new FileInputStream(file); //Create a stream from the file
// JDBC 2.0
//os = blob.getBinaryOutputStream(); //get the output stream from the Blob to insert it
// JDBC 3.0
os = blob.setBinaryStream(0); //get the output stream from the Blob to insert it

//Read the file by chuncks and insert them in the Blob. The chunk size come from the blob
byte[] chunk = new byte[blob.getChunkSize()];
int i=-1;
System.out.println("Inserting the Blob");
while((i = is.read(chunk))!=-1)
{
os.write(chunk,0,i); //Write the chunk
System.out.print('.'); // print progression
}

// When done close the streams
is.close();
os.close();

//Close the statement and commit
pstmt.close();
long ts2 = System.currentTimeMillis();

connection.commit();
connection.close();

System.out.println("\n"+ (ts2 - ts1) +" ms" );


}


}
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值