写个java下程序,用DBUtils操作数据库。代码如下:
@Test
public void blob()
{
    try {
        Connection connection=JDBCTools.getConnection();
         QueryRunner queryRunner =new QueryRunner();
         File file=new File("C:/1.png");
         byte[] buf=new byte[(int)file.length()];
         InputStream in=new FileInputStream(file);
         in.read(buf);
         SerialBlob  blob=new SerialBlob(buf);
         String sql="INSERT INTO table1 VALUES(?)";
         queryRunner.update(connection, sql,blob);
        System.out.println(connection);
    } catch (Exception e) {
        // TODO: handle exception
        e.printStackTrace();
    }
}为啥插入不了??

解决方案 »

  1.   

    buf有内容?
    是否提交了?
      

  2.   

    1.先在blob中插入empty_blob()  2.获得对刚刚插入记录的引用 BLOB blob = (BLOB) rs.getBlob("你的blob字段名称"); 3.写入 OutputStream out = blob.getBinaryOutputStream(); out.write(ENCYPWD);//注意这里
      

  3.   

    这个貌似是Java模块的问题,请移到Java板块~
      

  4.   

    @Test
    public void blob()
    {
        try {
            //写入Blob字段之前 需要保存好其他字段
            Connection connection=JDBCTools.getConnection();
             String sql = "确定update table set Blob字段 where 确定需要写入Blob字段的条件";
             File file=new File("C:/1.png");
             byte[] buf=new byte[(int)file.length()];
             PrepareStatement ps = connection.prepareStatement(sql);
             ResultSet rs = ps.executeQuery();
             while(rs.next()){
                 rs.updateBytes(columnIndex,buf);
              }
            System.out.println(connection);
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
    }