如下代码,想通过jdbc往数据库插入一个blob字段类型的数据,但是执行到
psm.setBinaryStream(1, fis, file.length()); 这句话时总是报一个错误,并且Exception捕捉不到。
帮忙分析一下原因?
错误如下:
Exception in thread "main" java.lang.AbstractMethodError: com.microsoft.jdbc.base.BasePreparedStatement.setBinaryStream(ILjava/io/InputStream;J)V
at database.BlobData.writeBlob(BlobData.java:32)
at database.BlobData.main(BlobData.java:117) 代码如下:
public static void writeBlob(Connection con, String infilePath){
FileInputStream fis = null;
PreparedStatement psm = null;
try {
//打开输入文件
File file = new File(infilePath);
fis = new FileInputStream(file);
psm = con.prepareStatement("update users set photo=? where id='001'");
psm.setBinaryStream(1, fis, file.length());
// 执行
psm.executeUpdate();
} catch (Exception ex) {
ex.printStackTrace();
} finally {
//关闭打开的对像
if (fis != null){
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
OperateDB.closeStatement(psm);
}
}
psm.setBinaryStream(1, fis, file.length()); 这句话时总是报一个错误,并且Exception捕捉不到。
帮忙分析一下原因?
错误如下:
Exception in thread "main" java.lang.AbstractMethodError: com.microsoft.jdbc.base.BasePreparedStatement.setBinaryStream(ILjava/io/InputStream;J)V
at database.BlobData.writeBlob(BlobData.java:32)
at database.BlobData.main(BlobData.java:117) 代码如下:
public static void writeBlob(Connection con, String infilePath){
FileInputStream fis = null;
PreparedStatement psm = null;
try {
//打开输入文件
File file = new File(infilePath);
fis = new FileInputStream(file);
psm = con.prepareStatement("update users set photo=? where id='001'");
psm.setBinaryStream(1, fis, file.length());
// 执行
psm.executeUpdate();
} catch (Exception ex) {
ex.printStackTrace();
} finally {
//关闭打开的对像
if (fis != null){
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
OperateDB.closeStatement(psm);
}
}
2,其次使用for update单独将blob字段取出来,select photo from users for update
oracle.sql.BLOB blob = null;
if (rs.next())
{
blob = (oracle.sql.BLOB) rs.getBlob(1);
}
final java.io.BufferedOutputStream out = new java.io.BufferedOutputStream(
blob.getBinaryOutputStream()); out.write(fis, 0, fis.length());
out.close();
3,要注意,要使用事务提交。
而且看的也不是太明白。是不是一定要先insert再update?
UPDATE t
SET blob_col=LOAD_FILE('/tmp/picture')
WHERE id=1;
我想SQLSERVER应该也有对应的函数可以使用
fis = new FileInputStream(file);
psm = con.prepareStatement("update users set photo=? where id='001'");
psm.setBinaryStream(1, fis, file.length()); 这里应该先套一个inputsream吧
改为
File file = new File(infilePath);
fis = new FileInputStream(file);
InputStream is = new new FileInputStream(file);
psm = con.prepareStatement("update users set photo=? where id='001'");
psm.setBinaryStream(1, is, file.length());
现在的问题很可能是我现在用的微软提供的驱动程序很可能不支持这种代码的更新方法。、
也就是说微软sql2000的JDBC驱动类实现setBinaryStream接口方法时有问题(bug?)或者说我的驱动有问题(不是最新的?)就是这个实现类:com.microsoft.jdbc.base.BasePreparedStatement.setBinaryStream
总之问题归结为有没有哪位兄弟用JDBC使用比较通用的代码实现过sql2000数据库二进制数据(image类型)的更新?
如有这方面经验的请谈谈,非常谢谢!
现在的问题很可能是我现在用的微软提供的驱动程序很可能不支持这种代码的更新方法。、
也就是说微软sql2000的JDBC驱动类实现setBinaryStream接口方法时有问题(bug?)或者说我的驱动有问题(不是最新的?)就是这个实现类:com.microsoft.jdbc.base.BasePreparedStatement.setBinaryStream
总之问题归结为有没有哪位兄弟用JDBC使用比较通用的代码实现过sql2000数据库二进制数据(image类型)的更新?
如有这方面经验的请谈谈,非常谢谢!
参考代码://打开输入文件
File file = new File(infilePath);
fis = new FileInputStream(file);
byte[] buf=new byte[(int)f.length();
fis.read(buf);//图片数据
psm = con.prepareStatement("update users set photo=? where id='001'");
psm.setBytes(1, buf);//插入图片
con.commit();
fis.close();
.....
//打开输入文件
File file = new File(infilePath);
fis = new FileInputStream(file);
byte[] buf=new byte[(int)f.length();
fis.read(buf);//图片数据
psm = con.prepareStatement("update users set photo=? where id='001'");
psm.setBytes(1, buf);//插入图片
psm.executeUpdate();
con.commit();
fis.close(); 另外我把读出的代码也贴出来吧,未经测试,请自己调试。^_^
String driverName="com.microsoft.jdbc.sqlserver.SQLServerDriver";
String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=studydata";
String user="newmoon";
String password="zw1974";
Connection conn = null;
PreparedStatement psm = null;
FileInputStream fis = null;
FileOutputStream fos = null;
InputStream is = null;
try{
Class.forName(driverName);
}catch(ClassNotFoundException e){
}
try{
conn = DriverManager.getConnection(url, user, password);
conn.setAutoCommit(false);
}catch(SQLException e){
}
try{
//打开输入文件
File file = new File("c:\\***.jpg");
fis = new FileInputStream(file);
byte[] buf = new byte[(int)file.length()];
fis.read(buf);
fis.close();
psm = conn.prepareStatement("update users set photo=? where id='001'");
psm.setBytes(1, buf);
psm.executeUpdate();
conn.commit();
psm.close();
conn.close();
conn = DriverManager.getConnection(url, user, password);
psm = conn.prepareStatement("SELECT * FROM users where id = '001'");
ResultSet rs = psm.executeQuery();
if (rs.next()) {
is = rs.getBinaryStream("photo");
byte[] buffer = new byte[4096];
int size;
file = new File("c:\\temp\\outfile.jpg");
try {
fos = new FileOutputStream(file);
while ((size = is.read(buffer)) != -1){
fos.write(buffer, 0, size);
}
} catch (IOException eee) {
eee.printStackTrace();
}
}
rs.close();
}catch(SQLException e){
conn.rollback();
System.out.println("无法更新");
e.printStackTrace();
}catch(FileNotFoundException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}finally {
// 关闭数据库连接
try{
fis.close();
}catch(IOException e){
e.printStackTrace();
}
if(conn != null){
conn.commit();
}
if(psm != null){
psm.close();
}
if(conn != null){
conn.close();
}
System.out.println("over");
}
psm.setBinaryStream(1, fis, (int)file.length());