如何读写Oracle数据库Blog字段(Java)

    技术2022-05-11  88

    数据库表结构

    CREATE TABLE LM_DATA_KEY(  KEY  BLOB,  ID   NUMBER) 

    1. 如何写入KEY字段 :

    // Remove old record with id=1 PreparedStatement pstmt  =  conn.prepareStatement( " delete from lm_data_key where id=1 " );pstmt.executeUpdate();     // Insert empty blob field         pstmt  =  conn.prepareStatement( " insert into lm_data_key (key, id) values(empty_Blob(), 1) " );pstmt.executeUpdate(); // Update blob field              pstmt  =  conn.prepareStatement( " select key from lm_data_key where id=1 for update " );rs  =  pstmt.executeQuery();              rs.next();java.sql.Blob es_Blob  =  rs.getBlob( 1 ); if  ( null   !=  es_Blob)  {  OutputStream outstream = ( (oracle.sql.BLOB) es_Blob).getBinaryOutputStream();  ObjectOutputStream oos = new java.io.ObjectOutputStream(outstream);  ArrayList b = new ArrayList();  b.add(new String("11111111"));  Object parameter = b;   oos.writeObject(parameter);  oos.flush();  oos.close();} else   {  System.out.println(" is null:");} pstmt.close();conn.commit();conn.setAutoCommit( false );

     

    2. 如何读出KEY字段:

    pstmt  =  conn.prepareStatement(sqlString);rs  =  pstmt.executeQuery();rs.next();Blob blob  =  rs.getBlob( 1 );InputStream instream  =  ( (Blob) blob).getBinaryStream();java.io.ObjectInputStream ois  =   new  java.io.ObjectInputStream(instream);Object parameter  =  ois.readObject();ois.close();ArrayList tarray  =  (ArrayList)parameter;System.out.println(tarray.get( 0 ).toString());conn.commit();conn.setAutoCommit( false );

     

    注:以上用的是java.io.ObjectInputStream来读写ArrayList对象的.


    最新回复(0)