当前位置: 技术问答>java相关
利用可更新ResultSet的updateBinaryStream()可以把图片存到mySql数据库,却不能存到Oracle数据库,怎么办?
来源: 互联网 发布时间:2015-05-14
本文导语: 提示错误:java.sql.SQLException: ORA-01008: 并非所有变量都已关联 Oracle数据库版本8.17。 | 试试这个,我看到的 insertBlobData(String fileDate, String fileName, String filePath) ************************* import java...
提示错误:java.sql.SQLException: ORA-01008: 并非所有变量都已关联
Oracle数据库版本8.17。
Oracle数据库版本8.17。
|
试试这个,我看到的
insertBlobData(String fileDate, String fileName, String filePath)
*************************
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;
}
}
insertBlobData(String fileDate, String fileName, String filePath)
*************************
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;
}
}