用JDBC存储ORACLE的BLOB字段模板import java.io.*;import java.sql.*;
import oracle.jdbc.driver.*;
import oracle.sql.*;public class testSuite
{ public void writeBLOB() throws Exception
{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@myhost:1521:mysid","myname","mypassword");
conn.setAutoCommit(false); String filename = "d:/passion.jpg";
Statement st = conn.createStatement();
int b= st.executeUpdate("insert into blobsamp values(60,empty_blob())"); //注意在插入 //BLOB字段时必须先插入一个空字段
st.execute("commit");
System.out.println("Int Value:\t"+b); ResultSet rs= st.executeQuery("select * from blobsamp for update");
rs.next();
BLOB blob=((oracle.jdbc.driver.OracleResultSet)rs).getBLOB(2);
rs.close();
FileInputStream instream = new FileInputStream(filename);
OutputStream outstream = blob.getBinaryOutputStream(); int chunk = blob.getChunkSize();
System.out.println(chunk);
byte[] buff = new byte[chunk];
int le; System.out.println("Writing file " + filename+ " into blob.....");
while( (le=instream.read(buff)) !=-1)
{
outstream.write(buff,0,le);
}
outstream.close();
st.execute("commit");
System.out.println("Blob Written.....");
instream.close();
conn.commit();
st.close();
conn.close();
conn = null;
System.out.println("Database updated.....");
} public void readBLOB() throws Exception
{
//Reading the BLOB :
String sql = "select id,blob_col from blobsamp where id=?";
Class.forName("oracle.jdbc.driver.OracleDriver");
ResultSet rs;
BLOB blob=null;
Connection con = DriverManager.getConnection("jdbc:oracle:thin:@myhost:1521:mysid","myname","mypassword");
PreparedStatement pstmt = con.prepareStatement(sql);
pstmt.setInt(1,70);
rs = pstmt.executeQuery();
while(rs.next())
{
blob = ((OracleResultSet)rs).getBLOB("blob_col");
}
rs.close();
InputStream blobStream = blob.getBinaryStream();
System.out.println("blob length : " + blob.length());
FileOutputStream fileOutStream = new FileOutputStream("d:\\abc.jpg");
byte[] buffer = new byte[10];
int nbytes = 0;
while ((nbytes = blobStream.read(buffer))!= -1)
fileOutStream.write(buffer,0,nbytes);
fileOutStream.flush();
fileOutStream.close();
blobStream.close();
pstmt.close();
con.commit();
con.close();
}}
import oracle.jdbc.driver.*;
import oracle.sql.*;public class testSuite
{ public void writeBLOB() throws Exception
{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@myhost:1521:mysid","myname","mypassword");
conn.setAutoCommit(false); String filename = "d:/passion.jpg";
Statement st = conn.createStatement();
int b= st.executeUpdate("insert into blobsamp values(60,empty_blob())"); //注意在插入 //BLOB字段时必须先插入一个空字段
st.execute("commit");
System.out.println("Int Value:\t"+b); ResultSet rs= st.executeQuery("select * from blobsamp for update");
rs.next();
BLOB blob=((oracle.jdbc.driver.OracleResultSet)rs).getBLOB(2);
rs.close();
FileInputStream instream = new FileInputStream(filename);
OutputStream outstream = blob.getBinaryOutputStream(); int chunk = blob.getChunkSize();
System.out.println(chunk);
byte[] buff = new byte[chunk];
int le; System.out.println("Writing file " + filename+ " into blob.....");
while( (le=instream.read(buff)) !=-1)
{
outstream.write(buff,0,le);
}
outstream.close();
st.execute("commit");
System.out.println("Blob Written.....");
instream.close();
conn.commit();
st.close();
conn.close();
conn = null;
System.out.println("Database updated.....");
} public void readBLOB() throws Exception
{
//Reading the BLOB :
String sql = "select id,blob_col from blobsamp where id=?";
Class.forName("oracle.jdbc.driver.OracleDriver");
ResultSet rs;
BLOB blob=null;
Connection con = DriverManager.getConnection("jdbc:oracle:thin:@myhost:1521:mysid","myname","mypassword");
PreparedStatement pstmt = con.prepareStatement(sql);
pstmt.setInt(1,70);
rs = pstmt.executeQuery();
while(rs.next())
{
blob = ((OracleResultSet)rs).getBLOB("blob_col");
}
rs.close();
InputStream blobStream = blob.getBinaryStream();
System.out.println("blob length : " + blob.length());
FileOutputStream fileOutStream = new FileOutputStream("d:\\abc.jpg");
byte[] buffer = new byte[10];
int nbytes = 0;
while ((nbytes = blobStream.read(buffer))!= -1)
fileOutStream.write(buffer,0,nbytes);
fileOutStream.flush();
fileOutStream.close();
blobStream.close();
pstmt.close();
con.commit();
con.close();
}}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货