请参考一下以下的代码,看对你是否有用
import oracle.sql.BLOB;
import oracle.jdbc.driver.OracleResultSet;
import oracle.jdbc.driver.*;
import java.io.*;
import java.sql.*;
import java.sql.Blob;public class Upload{
public static void main (String args []){
Connection myConn = null;
Statement stmt = null;
ResultSet rset = null;
FileInputStream fileinput = null;
OutputStream outstream = null;
PreparedStatement ps = null;
try{
// Load the Oracle JDBC driver
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
// Connect to the database
// You must put a database name after the @ sign in the connection URL.
// You can use either the fully specified SQL*net syntax or a short cut
// syntax as <host>:<port>:<sid>. The example uses the short cut syntax.
//myConn = DriverManager.getConnection ("jdbc:oracle:thin:@172.16.4.58:1521:weboa",
// "oa", "password");
myConn = DriverManager.getConnection ("jdbc:oracle:thin:@172.16.4.58:1521:weboa",
"oa", "password");
// Create a Statement
myConn.setAutoCommit(false);
stmt = myConn.createStatement();
String sql = "insert into AttachedDocumentInfo(DocumentID,DocumentContent) "+
"values ('111',empty_blob())";
stmt.execute (sql);
rset = stmt.executeQuery("SELECT DocumentID,DocumentContent FROM AttachedDocumentInfo");
BLOB blob=null;
while ( rset.next() ){
//String test = ((OracleResultSet)rset).getString(1);
//out.println("test="+test);
//blob = (oracle.sql.BLOB)(rset.getBlob(2));
blob = ((OracleResultSet) rset).getBLOB (2);
//blob = (java.sql.Blob)rset.getObject(2);
}
File files = new File("d:\\12345.doc");
System.out.println("FileToOracle FileName="+files.getName());
fileinput = new FileInputStream(files);
outstream = blob.getBinaryOutputStream();
int chunk = blob.getChunkSize();
byte[] buffer = new byte[8*1024];
int length = -1;
while ((length = fileinput.read(buffer)) != -1){
outstream.write(buffer, 0, length);
}
//myConn.commit();
fileinput.close();
outstream.close();
fileinput.close();
rset.close();
stmt.close();
}
catch(SQLException se){System.out.println("SQL Error:"+se);}
catch(Exception E){
/* Handle exception here. */
System.out.println("Service Error: "+E);
}finally{
if(rset != null){
try{
rset.close();
}catch(Exception ignore){};
}
if(stmt != null){
try{
stmt.close();
}catch(Exception ignore){};
}
if(myConn != null){
try{
myConn.close();
}catch(Exception ignore){};
}
if(fileinput != null){
try{
fileinput.close();
}catch(Exception ignore){};
}
if(outstream != null){
try{
outstream.close();
}catch(Exception ignore){};
}
if(ps != null){
try{
ps.close();
}catch(Exception ignore){};
}
}
}
}
import oracle.sql.BLOB;
import oracle.jdbc.driver.OracleResultSet;
import oracle.jdbc.driver.*;
import java.io.*;
import java.sql.*;
import java.sql.Blob;public class Upload{
public static void main (String args []){
Connection myConn = null;
Statement stmt = null;
ResultSet rset = null;
FileInputStream fileinput = null;
OutputStream outstream = null;
PreparedStatement ps = null;
try{
// Load the Oracle JDBC driver
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
// Connect to the database
// You must put a database name after the @ sign in the connection URL.
// You can use either the fully specified SQL*net syntax or a short cut
// syntax as <host>:<port>:<sid>. The example uses the short cut syntax.
//myConn = DriverManager.getConnection ("jdbc:oracle:thin:@172.16.4.58:1521:weboa",
// "oa", "password");
myConn = DriverManager.getConnection ("jdbc:oracle:thin:@172.16.4.58:1521:weboa",
"oa", "password");
// Create a Statement
myConn.setAutoCommit(false);
stmt = myConn.createStatement();
String sql = "insert into AttachedDocumentInfo(DocumentID,DocumentContent) "+
"values ('111',empty_blob())";
stmt.execute (sql);
rset = stmt.executeQuery("SELECT DocumentID,DocumentContent FROM AttachedDocumentInfo");
BLOB blob=null;
while ( rset.next() ){
//String test = ((OracleResultSet)rset).getString(1);
//out.println("test="+test);
//blob = (oracle.sql.BLOB)(rset.getBlob(2));
blob = ((OracleResultSet) rset).getBLOB (2);
//blob = (java.sql.Blob)rset.getObject(2);
}
File files = new File("d:\\12345.doc");
System.out.println("FileToOracle FileName="+files.getName());
fileinput = new FileInputStream(files);
outstream = blob.getBinaryOutputStream();
int chunk = blob.getChunkSize();
byte[] buffer = new byte[8*1024];
int length = -1;
while ((length = fileinput.read(buffer)) != -1){
outstream.write(buffer, 0, length);
}
//myConn.commit();
fileinput.close();
outstream.close();
fileinput.close();
rset.close();
stmt.close();
}
catch(SQLException se){System.out.println("SQL Error:"+se);}
catch(Exception E){
/* Handle exception here. */
System.out.println("Service Error: "+E);
}finally{
if(rset != null){
try{
rset.close();
}catch(Exception ignore){};
}
if(stmt != null){
try{
stmt.close();
}catch(Exception ignore){};
}
if(myConn != null){
try{
myConn.close();
}catch(Exception ignore){};
}
if(fileinput != null){
try{
fileinput.close();
}catch(Exception ignore){};
}
if(outstream != null){
try{
outstream.close();
}catch(Exception ignore){};
}
if(ps != null){
try{
ps.close();
}catch(Exception ignore){};
}
}
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货