如下代码,想通过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);
}
}

解决方案 »

  1.   

    1,你在插入一个blob类型时,首先要update这个自段为空,update users set photo=empty_blob() where id='001';
    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,要注意,要使用事务提交。
      

  2.   

    AbstractMethodError:当应用程序试图调用一个抽象方法时,抛出此错误。通常由编译器捕获此错误;如果某个类的定义自当前执行方法最后一次编译以后作了不兼容的更改,则此错误只可能在运行时发生
      

  3.   

    一般是用4楼的用法,每种数据库都有自己的BLOB类,用Blob实例向数据库写入blob数据,楼主可以看看你的数据库驱动包:
      

  4.   

    不知道楼主用这种方式来对BLOB操作有没有问题,我们做BLOB一般都用4楼的那种方式去做,不管是INSERT,还是UPDATE,都需要先填个空的进去,在SELECT...FOR UPDATE出来填充,然后按事务提交
      

  5.   

    各位说的是oracle的操作方法,有没有通用一点的。
    而且看的也不是太明白。是不是一定要先insert再update?
      

  6.   

    建议使用对应的数据库方法去设置blob字段,mysql可以使用:
    UPDATE t
                SET blob_col=LOAD_FILE('/tmp/picture')
                WHERE id=1;
    我想SQLSERVER应该也有对应的函数可以使用
      

  7.   

    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()); 这里应该先套一个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()); 
      

  8.   

    一模一样的代码(就是一楼贴出的代码),今天用mysql测试通过了,只不过mysql数据库中存储大二进制字段类型使用的是longblob而已。这说明并不是一定用什么所谓的数据库函数先置值。
    现在的问题很可能是我现在用的微软提供的驱动程序很可能不支持这种代码的更新方法。、
    也就是说微软sql2000的JDBC驱动类实现setBinaryStream接口方法时有问题(bug?)或者说我的驱动有问题(不是最新的?)就是这个实现类:com.microsoft.jdbc.base.BasePreparedStatement.setBinaryStream 
    总之问题归结为有没有哪位兄弟用JDBC使用比较通用的代码实现过sql2000数据库二进制数据(image类型)的更新?
    如有这方面经验的请谈谈,非常谢谢!
      

  9.   

    一模一样的代码(就是一楼贴出的代码),今天用mysql测试通过了,只不过mysql数据库中存储大二进制字段类型使用的是longblob而已。这说明并不是一定用什么所谓的数据库函数先置值。
    现在的问题很可能是我现在用的微软提供的驱动程序很可能不支持这种代码的更新方法。、
    也就是说微软sql2000的JDBC驱动类实现setBinaryStream接口方法时有问题(bug?)或者说我的驱动有问题(不是最新的?)就是这个实现类:com.microsoft.jdbc.base.BasePreparedStatement.setBinaryStream 
    总之问题归结为有没有哪位兄弟用JDBC使用比较通用的代码实现过sql2000数据库二进制数据(image类型)的更新?
    如有这方面经验的请谈谈,非常谢谢!
      

  10.   

    ding
      

  11.   

    答:我教楼主一个简单些的方法,肯定能成功:
    参考代码://打开输入文件 
    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(); 
    .....
      

  12.   

    楼上的漏了一句^_^
    //打开输入文件 
    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");
            }
      

  13.   


    psm.setBinaryStream(1, fis, (int)file.length());