Monday, December 31, 2012

Oracle的blob更新问题

测试代码(下附)表明:对于Oracle而言,在insert时使用PreparedStatement.setBinaryStream和PreparedStatement.setBytes,
如果二进制(字节数组)长度小于4000,正常,否则插入的数据长度为零。
在update时使用PreparedStatement.setBinaryStream和PreparedStatement.setBytes,
如果二进制(字节数组)长度小于4000,正常,否则插入的为null。

以下摘自LDBC之Oracle.java
 * http://otn.oracle.com/doc/oracle8i_816/mix.816/a76991/toc.htm
 * Oracle Thin driver CLOB/BLOB problem
 * http://castor.exolab.org/list-archive/msg06802.html

 * PreparedStatement.setBinaryStream works only with up to 4000 bytes.
 * This is true for both Oracle 8i and 9i, using the Thin driver.
 *
 * Oracle doesn't support ResultSet.getString on a CLOB.
 * getString returns null.
 
     try
    {
     //写入数据库的BLOB字段pic
     java.io.FileInputStream in = new java.io.FileInputStream("D:/tmp/a.jpg");
     int maxFileSize = 3990;// oracle不能超过4000
     Class.forName("oracle.jdbc.driver.OracleDriver");

     Connection con =
     DriverManager.getConnection(
     "jdbc:oracle:thin:@192.168.10.19:1521:czserver",
     "zheng",
     "zheng");

     PreparedStatement pstmt =
     //con.prepareStatement("INSERT INTO aj_xyr_zp (jz,ajbh,ryxh,zpxh, zp) " + " VALUES (?,?,?,?,?)");
                        con.prepareStatement("update aj_xyr_zp set zp=? where jz='05' and ajbh ='A4101050142001080010' and  ryxh='2'  and zpxh='1'  ") ;
//     pstmt.setString(1, "05");
//     pstmt.setString(2, "A4101050142001080010");
//     pstmt.setString(3, "2");
//     pstmt.setString(4, "1");
     //pstmt.setBinaryStream(5, in, maxFileSize);
                byte b[] = new byte[in.available()];
                System.out.println("size="+in.available());
                in.read(b);
                pstmt.setBytes(1,b);
     pstmt.executeUpdate();
     pstmt.close();
                con.close();
    }
    catch (Exception e)
    {
     e.printStackTrace();
    }
 

No comments:

Post a Comment