处理blob的完整例子
发表于:2007-07-01来源:作者:点击数:
标签:
import oracle .jdbc.driver. Oracle ResultSet; import oracle. sql .BLOB; import java .sql.DriverManager; import java.sql.Connection; import java.sql.ResultSet; import java.io.*; import java.sql.Statement; public class testBlob { public stati
import
oracle.jdbc.driver.
OracleResultSet;
import oracle.
sql.BLOB;
import
java.sql.DriverManager;
import java.sql.Connection;
import java.sql.ResultSet;
import java.io.*;
import java.sql.Statement;
public class testBlob {
public static void main(String args[]) {
try {
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@java:1521:eteda", "system", "manager");
conn.setAutoCommit(false);
Statement stmt = conn.createStatement();
stmt.execute("insert into testblob values (empty_blob())");
ResultSet rset = stmt.executeQuery("SELECT tt FROM testblob FOR UPDATE");
BLOB blob = null;
while (rset.next()) {
blob = ((OracleResultSet) rset).getBLOB(1);
System.out.println(blob.length());
}
File binaryFile = new File("c:\\meilin.xml");
System.out.println("meilin.xml length = " + binaryFile.length());
FileInputStream instream = new FileInputStream(binaryFile);
OutputStream outstream = blob.getBinaryOutputStream();
int chunk = blob.getChunkSize();
System.out.println("chunk size = " + chunk);
byte[] buffer = new byte[chunk];
int length = -1;
while ((length = instream.read(buffer)) != -1)
outstream.write(buffer, 0, length);
instream.close();
outstream.close();
conn.commit();
} catch (Exception e) {
System.out.println(e.toString());
}
}
}
原文转自:http://www.ltesting.net