对oracle的blob字段的操作(插入,更新,删除)

CREATE TABLE txjblob
(
x NUMBER(5) NOT NULL,
b BLOB DEFAULT EMPTY_BLOB() NOT NULL
)

插入:

   stmt.executeUpdate("insert into txjblob values(1,EMPTY_BLOB())");
   ResultSet rs = stmt.executeQuery("SELECT b FROM fff WHERE x=1 FOR UPDATE NOWAIT");
   
   FileInputStream fin = new FileInputStream("c:chart.gif");
   byte[] blobBuf = new byte[(int)fin.available()];
   fin.read(blobBuf);
   fin.close();

   if(rs.next()) {
    System.out.println(blobBuf.length);
    BLOB blob = (oracle.sql.BLOB)rs.getBlob(1);
    OutputStream out = blob.getBinaryOutputStream();
    out.write(blobBuf);
    out.close();
    conn.commit();
   }

更新:

   stmt.executeUpdate("UPDATE txjblob SET b=EMPTY_BLOB() WHERE x=1");
   ResultSet rs = stmt.executeQuery("SELECT b FROM fff WHERE x=1 FOR UPDATE NOWAIT");
  
   FileInputStream fin = new FileInputStream("c:chart.gif");
   byte[] blobBuf = new byte[(int)fin.available()];
   fin.read(blobBuf);
   fin.close();

   if(rs.next()) {
    System.out.println(blobBuf.length);
    BLOB blob = (oracle.sql.BLOB)rs.getBlob(1);
    OutputStream out = blob.getBinaryOutputStream();
    out.write(blobBuf);
    out.close();
    conn.commit();
   }

删除:

stmt.executeUpdate("DELETE txjblob WHERE x=1");

读取:

   ResultSet rs = stmt.executeQuery("select b from fff where x=1");
   if(rs.next()){
    BLOB blob = (BLOB)rs.getBlob(1);
    InputStream is = blob.getBinaryStream();
 //   InputStream is = rs.getBinaryStream(1);
    FileOutputStream os = new FileOutputStream("c:1.gif");
    int b;
    byte[] buffer = new byte[1024];
    while( (b=is.read(buffer)) != -1){
     os.write(buffer,0,b);
    }
    is.close();
    os.close();
   }


你可能感兴趣的:(oracle技术)