当前位置: 技术问答>java相关
小弟想直接上传图片到Oracle的Blob中,诸位仁兄给点思路或提示或资料或源码
来源: 互联网 发布时间:2015-05-11
本文导语: ....... | 我看到的哈 。。。。。 import java.sql.*; import java.io.*; import oracle.sql.*; import oracle.jdbc.driver.*; public class InsertBlob { public static int insertBlobData(String fileDate, String fileName, String filePa...
.......
|
我看到的哈
。。。。。
import java.sql.*;
import java.io.*;
import oracle.sql.*;
import oracle.jdbc.driver.*;
public class InsertBlob {
public static int insertBlobData(String fileDate, String fileName, String filePath) throws Exception {
int retVal = 0;
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
InputStream fin = null;
OutputStream fout = null;
try {
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
conn = DriverManager.getConnection("jdbc:oracle:thin:@graycell:1521:o8i", "internal", "oracle");
conn.setAutoCommit(false);
stmt = conn.prepareStatement("INSERT INTO tblBlobDemo VALUES (seqBlobDemoID.NextVal, ?, ?, empty_blob())");
stmt.setString(1, fileDate);
stmt.setString(2, fileName);
stmt.execute();
stmt.clearParameters();
stmt = conn.prepareStatement("SELECT FileData FROM tblBlobDemo WHERE FileName = ? FOR UPDATE");
stmt.setString(1, fileName);
rs = stmt.executeQuery();
if(rs.next()) {
BLOB blob = ((OracleResultSet)rs).getBLOB("FileData");
fout = blob.getBinaryOutputStream();
File f = new File(filePath);
fin = new FileInputStream(f);
byte[] buffer = new byte[blob.getBufferSize()];
int bytesRead = 0;
while((bytesRead = fin.read(buffer)) != -1) {
fout.write(buffer, 0, bytesRead);
}
blob = null;
f = null;
buffer = null;
}
stmt.clearParameters();
}
catch(SQLException ex) {
ex.printStackTrace();
retVal = 1;
}
catch(FileNotFoundException ef) {
ef.printStackTrace();
retVal = 2;
}
catch(Exception e) {
e.printStackTrace();
retVal = 3;
}
finally {
try {
fin.close();
fout.close();
conn.commit();
stmt.close();
conn.close();
fin = null;
fout = null;
rs = null;
conn = null;
stmt = null;
}
catch(SQLException e) {
retVal = 1;
}
}
return retVal;
}
}
。。。。。
import java.sql.*;
import java.io.*;
import oracle.sql.*;
import oracle.jdbc.driver.*;
public class InsertBlob {
public static int insertBlobData(String fileDate, String fileName, String filePath) throws Exception {
int retVal = 0;
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
InputStream fin = null;
OutputStream fout = null;
try {
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
conn = DriverManager.getConnection("jdbc:oracle:thin:@graycell:1521:o8i", "internal", "oracle");
conn.setAutoCommit(false);
stmt = conn.prepareStatement("INSERT INTO tblBlobDemo VALUES (seqBlobDemoID.NextVal, ?, ?, empty_blob())");
stmt.setString(1, fileDate);
stmt.setString(2, fileName);
stmt.execute();
stmt.clearParameters();
stmt = conn.prepareStatement("SELECT FileData FROM tblBlobDemo WHERE FileName = ? FOR UPDATE");
stmt.setString(1, fileName);
rs = stmt.executeQuery();
if(rs.next()) {
BLOB blob = ((OracleResultSet)rs).getBLOB("FileData");
fout = blob.getBinaryOutputStream();
File f = new File(filePath);
fin = new FileInputStream(f);
byte[] buffer = new byte[blob.getBufferSize()];
int bytesRead = 0;
while((bytesRead = fin.read(buffer)) != -1) {
fout.write(buffer, 0, bytesRead);
}
blob = null;
f = null;
buffer = null;
}
stmt.clearParameters();
}
catch(SQLException ex) {
ex.printStackTrace();
retVal = 1;
}
catch(FileNotFoundException ef) {
ef.printStackTrace();
retVal = 2;
}
catch(Exception e) {
e.printStackTrace();
retVal = 3;
}
finally {
try {
fin.close();
fout.close();
conn.commit();
stmt.close();
conn.close();
fin = null;
fout = null;
rs = null;
conn = null;
stmt = null;
}
catch(SQLException e) {
retVal = 1;
}
}
return retVal;
}
}