| public static Object createOracleLob(Connection conn, String lobClassName) throws Exception { Class lobClass = conn.getClass().getClassLoader().loadClass( lobClassName); final Integer DURATION_SESSION = new Integer(lobClass.getField( ”DURATION_SESSION”).getInt(null)); final Integer MODE_READWRITE = new Integer(lobClass.getField( ”MODE_READWRITE”).getInt(null)); Method createTemporary = lobClass.getMethod(”createTemporary”, new Class[] { Connection.class, boolean.class, int.class }); Object lob = createTemporary.invoke(null, new Object[] { conn, false, DURATION_SESSION }); Method open = lobClass.getMethod(”open”, new Class[] { int.class }); open.invoke(lob, new Object[] { MODE_READWRITE }); return lob; } public static String oracleClob2Str(Clob clob) throws Exception { return (clob != null ? clob.getSubString(1, (int) clob.length()) : null); } public static Clob oracleStr2Clob(String str, Clob lob) throws Exception { Method methodToInvoke = lob.getClass().getMethod( ”getCharacterOutputStream”, (Class[]) null); Writer writer = (Writer) methodToInvoke.invoke(lob, (Object[]) null); writer.write(str); writer.close(); return lob; } public static void main(String[] args) throws Exception { //创建数据源略 Connection conn = SqlUtil.getConnection(); Clob clob = (Clob) createOracleLob(conn, ”oracle.sql.CLOB”);// BLOB的话传oracle.sql.BLOB // create table testTb (TheClob Clob); PreparedStatement pstmt = conn .prepareStatement(”insert into testTb (TheClob) values (?)”); pstmt.setClob(1, oracleStr2Clob(”test”, clob)); pstmt.execute(); SqlUtil.closeStmt(pstmt); Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery(”select * from testTb”); while (rs.next()) { String str = oracleClob2Str(rs.getClob(1)); System.out.println(str); } SqlUtil.closeRs(rs); SqlUtil.closeStmt(stmt); SqlUtil.closeConn(conn); } |
|
|