求Oracle CLOB 和 BLOB 读写 例子 能用 立即给分
------解决方案--------------------
/**
* Oralce CLOB 字段更新
*
* @param sql
* String : sql 语句
* @param bdata
* byte[] : 大字段值
* @return int
* @throws Exception
*/
public int ExecuteUpdateOracleClob(String tableName,
String clobField, String pkField, String pkValue, String clobContent)
throws Exception {
int iret = 0;
PreparedStatement pst = null;
Connection Conn = null;
try {
Conn = GetConnection();
Conn.setAutoCommit(false);
pst = Conn.prepareStatement("update " + tableName + " set "
+ clobField + "=? where " + pkField + "=?");
pst.setClob(1, oracle.sql.CLOB.empty_lob());
pst.setObject(2, pkValue);
pst.executeUpdate();
pst = Conn.prepareStatement("select " + clobField + " from "
+ tableName + " where " + pkField + "=?");
pst.setObject(1, pkValue);
ResultSet rset = pst.executeQuery();
oracle.sql.CLOB resClob = null;
if (rset.next())
resClob = (oracle.sql.CLOB) rset.getClob(1);
rset.close();
resClob.putString(1, clobContent);
pst = Conn.prepareStatement("update " + tableName + " set "
+ clobField + "=? where " + pkField + "=?");
pst.setClob(1, resClob);
pst.setObject(2, pkValue);
iret = pst.executeUpdate();
Conn.commit();
} catch (Exception ex) {
try {
Conn.rollback();
CloseConn(Conn, pst);
} catch (SQLException sqlex) {
}
System.out.println(ex.toString());
throw new Exception("ExecuteUpdateOracleBlob:" + ex.toString());
} finally {
CloseConn(Conn, pst);
}
return iret;
}
/**
* 执行大字段查询
*
* @param sql
* String : sql 语句
* @return byte[] : 返回大字段内容
* @throws Exception
*/
public String ExecuteQueryClob(String sql) throws Exception {
Statement st = null;
ResultSet rs = null;
Connection Conn = null;
char retdata[] = null;
String result = "";
try {
Conn = GetConnection();
st = Conn.createStatement();
rs = st.executeQuery(sql);
if (rs.next()) {
Clob cdata = rs.getClob(1);
if (cdata != null) {
retdata = new char[(int) cdata.length()];
cdata.getCharacterStream().read(retdata);
result = String.valueOf(retdata);
}
}
} catch (Exception ex) {
try {
Conn.rollback();
CloseRs(rs);
CloseConn(Conn, st);
} catch (SQLException sqlex) {
}
throw new Exception(ex.toString());
} finally {
CloseRs(rs);
CloseConn(Conn, st);
}
return result;
}