java常用方法

1.读取oracle blob类型

private byte[] readBlob(Object obj) {
  byte[] data = null;
  InputStream inStream = null;
  java.sql.Blob blob = (Blob) obj;
  try {
   inStream = blob.getBinaryStream();
   long nLen = blob.length();
   int nSize = (int) nLen;
   // System.out.println("img data size is :" + nSize);
   data = new byte[nSize];
   inStream.read(data);
   return data ;
  } catch (Exception e) {
   System.out.println(e.getMessage());
   return null;
  } finally {
   try {
    if (inStream != null) {
     inStream.close();
    }
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
  }
 }

2.写oracle blob列

public void updateBlobFile(String id, byte[] data) throws IOException,
   SQLException {
  Connection conn = null;
  PreparedStatement preparedStatement1 = null;
  PreparedStatement preparedStatement2 = null;
  ResultSet rs = null;
  oracle.sql.BLOB blob = null;
  OutputStream outStream=null;
  try {
   conn = db.getConnection();
   conn.setAutoCommit(false);

   String updateEmptySql = "update table1 t set t.attachFile=empty_blob() where t.id=?";
   preparedStatement1 = conn.prepareStatement(updateEmptySql);
   preparedStatement1.setString(1, id); 
   preparedStatement1.executeUpdate();
   // 获取空BLOB字段,并获取更新锁
   String getEmpty = "select t.attachFile from table1 t where t.id= ? for update ";
   preparedStatement2 = conn.prepareStatement(getEmpty);
   preparedStatement2.setString(1, id);
   rs = preparedStatement2.executeQuery();
   if (rs.next()) {
    blob = (oracle.sql.BLOB) rs.getBlob(1);
   }
   outStream = blob.getBinaryOutputStream();
   outStream.write(data, 0, data.length);
   outStream.flush();
   conn.commit();
  } catch (SQLException e) {
   conn.rollback();
   throw e;
  } finally {
   if (outStream != null) {
    outStream.close();
   }
   if (preparedStatement1 != null) {
    preparedStatement1.close();
   }
   if (preparedStatement2 != null) {
    preparedStatement2.close();
   }
   if (conn != null) {
    conn.close();
   }
  }
 }

3.读oracle clob 类型

private String readClob(Object obj) {
  Reader inStream = null;
  try {
   java.sql.Clob clob = (java.sql.Clob) obj;
   inStream = clob.getCharacterStream();
   char[] c = new char[(int) clob.length()];
   inStream.read(c);
   // data是读出并需要返回的数据,类型是String
   String data = new String(c);
   return data;
  } catch (Exception e) {
   System.out.println("获取Clob数据失败,原因:" + e.getMessage());
   return "";
  } finally {
   try {
    if (inStream != null) {
     inStream.close();
    }
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
  }
 }

 4.写oracle clob 类型

public void updateClob(String id, String data) throws IOException,
   SQLException {
  Connection conn = null;
  PreparedStatement preparedStatement1 = null;
  PreparedStatement preparedStatement2 = null;
  ResultSet rs = null;
  oracle.sql.CLOB clob = null;
  OutputStream outStream=null;
  try {
   conn = db.getConnection();
   conn.setAutoCommit(false);

   String updateEmptySql = "update table1 t set t.content_clob=empty_clob() where t.id=?";
   preparedStatement1 = conn.prepareStatement(updateEmptySql);
   preparedStatement1.setString(1, id); 
   preparedStatement1.executeUpdate();
   // 获取空BLOB字段,并获取更新锁
   String getEmpty = "select t.content_clob from table1 t where t.id= ? for update ";
   preparedStatement2 = conn.prepareStatement(getEmpty);
   preparedStatement2.setString(1, id);
   rs = preparedStatement2.executeQuery();
   if (rs.next()) {
    clob = (oracle.sql.CLOB) rs.getBlob(1);

    Writer outStream = clob.getCharacterOutputStream();
    char[] c = data.toCharArray();
    outStream.write(c, 0, c.length);
   }
   outStream.flush();
   conn.commit();
  } catch (SQLException e) {
   conn.rollback();
   throw e;
  } finally {
   if (outStream != null) {
    outStream.close();
   }
   if (preparedStatement1 != null) {
    preparedStatement1.close();
   }
   if (preparedStatement2 != null) {
    preparedStatement2.close();
   }
   if (conn != null) {
    conn.close();
   }
  }
 }

 

 

posted on 2010-01-14 12:06  david.chen  阅读(356)  评论(0)    收藏  举报