// testClob.java // // This class demonstrates inserting a new Clob value into an Oracle 8 database and // updating an existing Clob value in the database. // // The test() method is a simple test that compares Clob values written to the database // to values read back. import java.sql.*; import java.io.*; class testClob { testClob(java.sql.Connection conn) { m_conn = conn; } // final int m_testlength = 0x100000; // one meg int m_testlength = 1024* 3; // 3k public void addData() throws SQLException { try { String idx; byte[] val= new byte[m_testlength]; idx = String.valueOf(m_testlength); generateData(idx, val); // generate test data // insert small (less then 4k string) java.sql.PreparedStatement stmt = m_conn.prepareStatement("insert into TESTCLOB values(?, ?)"); ByteArrayInputStream instr = new ByteArrayInputStream(val); stmt.setString(1, idx); stmt.setAsciiStream(2, instr, val.length); stmt.executeUpdate(); // add large clob value m_testlength = 0x100000; // one meg ( 1048576) val= new byte[m_testlength]; idx = String.valueOf(m_testlength); generateData(idx, val); // generate test data instr = new ByteArrayInputStream(val); stmt = m_conn.prepareStatement("insert into TESTCLOB values(?, Empty_Clob())"); stmt.setString(1, idx); stmt.executeUpdate(); m_conn.commit(); stmt.close(); System.out.println("simulate an update on the new row"); // simulate an update on the new row to see if the resulting lock is // strong enough for clob update stmt = m_conn.prepareStatement("update TESTCLOB set ind = '1048576' where ind = '1048576'"); stmt.executeUpdate(); System.out.println("row updated"); // reselect the same row so we can update it java.sql.Statement select = m_conn.createStatement(); // java.sql.ResultSet rs = select.executeQuery("select val from testclob where ind = '1048576' for update"); java.sql.ResultSet rs = select.executeQuery("select val from testclob where ind = '1048576'"); if(rs.next()) // should find the row { String strVal = new String(val); char[] arr = strVal.toCharArray(); CharArrayReader rdr = new CharArrayReader(arr) ; rs.updateCharacterStream(1,rdr , m_testlength); System.out.println("about to call rs.updateRow()"); rs.updateRow(); System.out.println("rs.updateRow() returned"); m_conn.commit(); select.close(); stmt.close(); } } catch (SQLException sch_ex) { System.err.print ("SQLException: "); System.err.print (sch_ex.getMessage()); } catch (Exception e) { e.printStackTrace(); } } public void createTable() throws SQLException { java.sql.Statement stmt = m_conn.createStatement(); try { stmt.executeUpdate("drop table TESTCLOB"); } catch (SQLException sch_ex) { System.err.print ("SQLException: "); System.err.print (sch_ex.getMessage()); } catch (Exception e) { e.printStackTrace(); } try { stmt.executeUpdate("create table TESTCLOB(ind varchar2(30), val clob)"); } catch (SQLException sch_ex) { System.err.print ("SQLException: "); System.err.print (sch_ex.getMessage()); } catch (Exception e) { e.printStackTrace(); } } // generate test data private void generateData(String indexkey, byte[] val) { int idx = 0; while(idx < m_testlength - 100) { val[idx++] = (byte)'0'; // add 10 characters values val[idx++] = (byte)'1'; val[idx++] = (byte)'2'; val[idx++] = (byte)'3'; val[idx++] = (byte)'4'; val[idx++] = (byte)'5'; val[idx++] = (byte)'6'; val[idx++] = (byte)'7'; val[idx++] = (byte)'8'; val[idx++] = (byte)'9'; val[idx++] = (byte)' '; // space } while(idx < m_testlength) val[idx++] = (byte)'x'; } public void simple() { try { java.sql.Statement select = m_conn.createStatement(); java.sql.ResultSet rs = select.executeQuery("select ind, val from testclob"); // Class1.printHeaders(rs); while(rs.next()) { String str = rs.getString(1); java.sql.Clob clb = rs.getClob(2); long len = clb.length(); String strSub = clb.getSubString(1, (int)len); System.out.println(str + ", len= " + len + ", value= " + strSub); } } catch (SQLException sch_ex) { System.err.print ("SQLException: "); System.err.print (sch_ex.getMessage()); } catch (Exception e) { e.printStackTrace(); } } public void test() { try { java.sql.Statement select = m_conn.createStatement(); java.sql.ResultSet rs = select.executeQuery("select ind, val from testclob"); // Class1.printHeaders(rs); while(rs.next()) { String str = rs.getString(1); Integer len_val = new Integer(str); int len_shouldbe = len_val.intValue(); java.sql.Clob clb = rs.getClob(2); long len = clb.length(); if(len != len_shouldbe) { System.out.println("clob test failure, len should of been " + str + " but was " + len); } String strSub = clb.getSubString(1, (int)len); m_testlength = len_shouldbe; byte[] testval= new byte[m_testlength]; generateData(str, testval); // generate test data String strTestVal = new String(testval); if(strTestVal.length() != strSub.length()) { System.out.println("clob test failure, actual length compare should of been " + strTestVal.length() + " but was " + strSub.length()); } int icmp = strSub.compareTo(strTestVal); if( icmp == 0) { System.out.println("clob test succeeded for length = " + str); } else { System.out.println("clob test failed for length = " + str); } } } catch (SQLException sch_ex) { System.err.print ("SQLException: "); System.err.print (sch_ex.getMessage()); } catch (Exception e) { e.printStackTrace(); } } java.sql.Connection m_conn; }