/** * Tests the implementation of getCharacterStream(long pos, long length). * * @throws Exception */ public void testGetCharacterStreamLong() throws Exception { String str1 = "This is a test String. This is a test String"; Reader r1 = new java.io.StringReader(str1); PreparedStatement ps = prepareStatement("insert into BLOBCLOB(ID, CLOBDATA) values(?,?)"); int id = BlobClobTestSetup.getID(); ps.setInt(1, id); ps.setCharacterStream(2, r1); ps.execute(); ps.close(); Statement st = createStatement(); ResultSet rs = st.executeQuery("select CLOBDATA from " + "BLOBCLOB where ID=" + id); rs.next(); Clob clob = rs.getClob(1); Reader r_1 = clob.getCharacterStream(2L, 5L); String str2 = str1.substring(1, 6); Reader r_2 = new java.io.StringReader(str2); assertEquals(r_2, r_1); rs.close(); st.close(); }
/** private method which actually will do all of our work for the sample */ private void executeSample() { String query = "select anEmployee from staff2"; try { Statement stmt = _con.createStatement(); ; // Execute the query which will return an Employee object // We will cast this using the Person interface. Note the // Person interface class MUST be in your CLASSPATH. You // Do not need Employee in your CLASSPATH. ResultSet rs = stmt.executeQuery(query); output("***Using interface class\n"); while (rs.next()) { Person aPerson = (Person) rs.getObject(1); displayMethods(aPerson.getClass()); output( "The person is: " + aPerson.toString() + "\nFirst Name= " + aPerson.getFirstName() + "\nLast Name= " + aPerson.getLastName() + "\n"); } // Now execute the same query, but this time we will use // reflection to access the class. Again, only the interface // Person is required in the CLASSPATH rs = stmt.executeQuery(query); output("***Using reflection\n"); Object theObj = null; while (rs.next()) { theObj = rs.getObject(1); output("The person is: " + theObj.toString() + "\n"); Class theClass = theObj.getClass(); displayMethods(theClass); Method m1 = theClass.getMethod("toString", new Class[0]); Method m2 = theClass.getMethod("getFirstName", new Class[0]); Method m3 = theClass.getMethod("getLastName", new Class[0]); output( "The person is: " + (Object) m1.invoke(theObj, new Object[0]) + "\nFirst Name= " + (Object) m2.invoke(theObj, new Object[0]) + "\nLast Name= " + (Object) m3.invoke(theObj, new Object[0]) + "\n"); } rs.close(); stmt.close(); } catch (SQLException sqe) { displaySQLEx(sqe); } catch (Exception e) { error("Unexpected exception : " + e.toString() + "\n"); e.printStackTrace(); } }
/** * Test that <code>Clob.getCharacterStream(long,long)</code> works on CLOBs that are streamed from * store. (DERBY-2891) */ public void testGetCharacterStreamLongOnLargeClob() throws Exception { getConnection().setAutoCommit(false); // create large (>32k) clob that can be read from store final int size = 33000; StringBuilder sb = new StringBuilder(size); for (int i = 0; i < size; i += 10) { sb.append("1234567890"); } final int id = BlobClobTestSetup.getID(); PreparedStatement ps = prepareStatement("insert into blobclob(id, clobdata) values (?,cast(? as clob))"); ps.setInt(1, id); ps.setString(2, sb.toString()); ps.executeUpdate(); ps.close(); Statement s = createStatement(); ResultSet rs = s.executeQuery("select clobdata from blobclob where id = " + id); assertTrue(rs.next()); Clob c = rs.getClob(1); // request a small region of the clob BufferedReader r = new BufferedReader(c.getCharacterStream(4L, 3L)); assertEquals("456", r.readLine()); r.close(); c.free(); rs.close(); s.close(); rollback(); }
// // Examine BLOBs and CLOBs. // private void vetLargeObjects( Connection conn, HashSet<String> unsupportedList, HashSet<String> notUnderstoodList) throws Exception { Statement stmt = conn.createStatement(); stmt.execute("CREATE TABLE t (id INT PRIMARY KEY, " + "b BLOB(10), c CLOB(10))"); stmt.execute( "INSERT INTO t (id, b, c) VALUES (1, " + "CAST (" + TestUtil.stringToHexLiteral("101010001101") + "AS BLOB(10)), CAST ('hello' AS CLOB(10)))"); ResultSet rs = stmt.executeQuery("SELECT id, b, c FROM t"); rs.next(); Blob blob = rs.getBlob(2); Clob clob = rs.getClob(3); vetObject(blob, unsupportedList, notUnderstoodList); vetObject(clob, unsupportedList, notUnderstoodList); stmt.close(); conn.rollback(); }
/** * Inserts a Clob with the specified length, using a stream source, then fetches it from the * database and checks the length. * * @param length number of characters in the Clob * @throws IOException if reading from the source fails * @throws SQLException if something goes wrong */ private void insertAndFetchTest(long length) throws IOException, SQLException { PreparedStatement ps = prepareStatement("insert into BLOBCLOB(ID, CLOBDATA) values(?,?)"); int id = BlobClobTestSetup.getID(); ps.setInt(1, id); ps.setCharacterStream(2, new LoopingAlphabetReader(length), length); long tsStart = System.currentTimeMillis(); ps.execute(); println( "Inserted " + length + " chars (length specified) in " + (System.currentTimeMillis() - tsStart) + " ms"); Statement stmt = createStatement(); tsStart = System.currentTimeMillis(); ResultSet rs = stmt.executeQuery("select CLOBDATA from BLOBCLOB where id = " + id); assertTrue("Clob not inserted", rs.next()); Clob aClob = rs.getClob(1); assertEquals("Invalid length", length, aClob.length()); println("Fetched length (" + length + ") in " + (System.currentTimeMillis() - tsStart) + " ms"); rs.close(); // Insert same Clob again, using the lengthless override. id = BlobClobTestSetup.getID(); ps.setInt(1, id); ps.setCharacterStream(2, new LoopingAlphabetReader(length)); tsStart = System.currentTimeMillis(); ps.executeUpdate(); println( "Inserted " + length + " chars (length unspecified) in " + (System.currentTimeMillis() - tsStart) + " ms"); rs = stmt.executeQuery("select CLOBDATA from BLOBCLOB where id = " + id); assertTrue("Clob not inserted", rs.next()); aClob = rs.getClob(1); assertEquals("Invalid length", length, aClob.length()); println("Fetched length (" + length + ") in " + (System.currentTimeMillis() - tsStart) + " ms"); rs.close(); rollback(); }
/** * Preconditions: 1. flag must be one of TMSTARTRSCAN, TMENDRSCAN, TMNOFLAGS or TMSTARTTRSCAN | * TMENDRSCAN 2. if flag isn't TMSTARTRSCAN or TMSTARTRSCAN | TMENDRSCAN, a recovery scan must be * in progress * * <p>Postconditions: 1. list of prepared xids is returned */ public Xid[] recover(int flag) throws XAException { // Check preconditions if (flag != TMSTARTRSCAN && flag != TMENDRSCAN && flag != TMNOFLAGS && flag != (TMSTARTRSCAN | TMENDRSCAN)) throw new PGXAException(GT.tr("Invalid flag"), XAException.XAER_INVAL); // We don't check for precondition 2, because we would have to add some additional state in // this object to keep track of recovery scans. // All clear. We return all the xids in the first TMSTARTRSCAN call, and always return // an empty array otherwise. if ((flag & TMSTARTRSCAN) == 0) return new Xid[0]; else { try { Statement stmt = conn.createStatement(); try { // If this connection is simultaneously used for a transaction, // this query gets executed inside that transaction. It's OK, // except if the transaction is in abort-only state and the // backed refuses to process new queries. Hopefully not a problem // in practise. ResultSet rs = stmt.executeQuery("SELECT gid FROM pg_prepared_xacts"); LinkedList l = new LinkedList(); while (rs.next()) { Xid recoveredXid = RecoveredXid.stringToXid(rs.getString(1)); if (recoveredXid != null) l.add(recoveredXid); } rs.close(); return (Xid[]) l.toArray(new Xid[l.size()]); } finally { stmt.close(); } } catch (SQLException ex) { throw new PGXAException(GT.tr("Error during recover"), ex, XAException.XAER_RMERR); } } }
/** * Insert a row with a large clob into the test table. Read the row from the database and assign * the clob value to <code>clob</code>. * * @return The id of the row that was inserted * @throws java.sql.SQLException */ private int initializeLongClob() throws SQLException { // Clob needs to be larger than one page for locking to occur final int lobLength = 40000; // Insert a long Clob PreparedStatement ps = prepareStatement("insert into BLOBCLOB(ID, CLOBDATA) values(?,?)"); int id = BlobClobTestSetup.getID(); ps.setInt(1, id); ps.setCharacterStream(2, new LoopingAlphabetReader(lobLength), lobLength); ps.execute(); ps.close(); commit(); // Fetch the Clob object from the database Statement st = createStatement(); ResultSet rs = st.executeQuery("select CLOBDATA from BLOBCLOB where ID=" + id); rs.next(); clob = rs.getClob(1); rs.close(); st.close(); return id; }
/** * Tests the exceptions thrown by the getCharacterStream (long pos, long length) for the following * conditions a) pos <= 0 b) pos > (length of LOB) c) length < 0 d) pos + length > (length of * LOB). * * @throws SQLException */ public void testGetCharacterStreamLongExceptionConditions() throws SQLException { String str1 = "This is a test String. This is a test String"; Reader r1 = new java.io.StringReader(str1); PreparedStatement ps = prepareStatement("insert into BLOBCLOB(ID, CLOBDATA) values(?,?)"); int id = BlobClobTestSetup.getID(); ps.setInt(1, id); ps.setCharacterStream(2, r1); ps.execute(); ps.close(); Statement st = createStatement(); ResultSet rs = st.executeQuery("select CLOBDATA from " + "BLOBCLOB where ID=" + id); rs.next(); Clob clob = rs.getClob(1); // check the case where pos <= 0 try { // set pos as negative clob.getCharacterStream(-2L, 5L); // Should not come here. The exception has to be thrown. fail("FAIL: Expected SQLException for pos being negative " + "not thrown"); } catch (SQLException sqle) { // The SQLState for the exception thrown when pos <= 0 is XJ070 assertSQLState("XJ070", sqle); } // check for the case pos > length of clob try { // set the pos to any value greater than the Clob length clob.getCharacterStream(clob.length() + 1, 5L); // Should not come here. The exception has to be thrown. fail( "FAIL: Expected SQLException for position being greater than " + "length of LOB not thrown"); } catch (SQLException sqle) { // The SQLState for the exception thrown when pos > length of Clob // is XJ076 assertSQLState("XJ087", sqle); } // check for the case when length < 0 try { // set length as negative clob.getCharacterStream(2L, -5L); // Should not come here. The exception has to be thrown. fail("Fail: expected exception for the length being negative " + "not thrown"); } catch (SQLException sqle) { // The SQLState for the exception thrown when length < 0 of Clob // is XJ071 assertSQLState("XJ071", sqle); } // check for the case when pos + length > length of Clob try { // set pos + length > length of Clob clob.getCharacterStream((clob.length() - 4), 10L); // Should not come here. The exception has to be thrown. fail( "Fail: expected exception for the sum of position and length" + " being greater than the LOB size not thrown"); } catch (SQLException sqle) { // The SQLState for the exception thrown when length < 0 of Clob // is XJ087 assertSQLState("XJ087", sqle); } }
public String consultarImagen( Connection conn, String path, String pagina, String sucID, String vigID, String padreID, String secundarioID, String referenciaID, String documentoID, String consecutivoID) { Vector vRetornoImg = new Vector(1); Statement st = null; ResultSet rs = null; String sql = ""; try { if (consecutivoID.length() <= 0) consecutivoID = "%"; if (pagina.equalsIgnoreCase("prerequisitodet")) { sql = " select PREDI_IMAGEN img, PREDI_NOMBREIMAGEN nameimg" + " from COT_PREREQUISITODETIMG" + " where suc_id = '" + sucID + "' " + " and vig_id = '" + vigID + "'" + " and prec_id = '" + padreID + "'" + " and tpr_id = '" + secundarioID + "'" + " and tdo_referencia = '" + referenciaID + "'" + " and tdo_id = '" + documentoID + "'" + " and predi_consecutivo like '" + consecutivoID + "'"; } if (pagina.equalsIgnoreCase("requisitosdis")) { sql = " select REDII_IMAGEN img, REDII_NOMBREIMAGEN nameimg" + " from COT_REQHABILITANTEDISIMG " + " where suc_id = '" + sucID + "' " + " and vig_id = '" + vigID + "'" + " and disc_id = '" + padreID + "'" + " and tdo_referencia = '" + referenciaID + "'" + " and tdo_id = '" + documentoID + "'"; } if (pagina.equalsIgnoreCase("proceso")) { sql = " select PROEI_IMAGEN img, PROEI_NOMBREIMAGEN nameimg" + " from COT_PROCESOESTIMG " + " where suc_id = '" + sucID + "' " + " and vig_id = '" + vigID + "'" + " and proc_id = '" + padreID + "'" + " and epo_id = '" + secundarioID + "'" + " and tdo_referencia = '" + referenciaID + "'" + " and tdo_id = '" + documentoID + "'" + " and proei_consecutivo like '" + consecutivoID + "'"; } st = conn.createStatement(); rs = st.executeQuery(sql); if (rs.next()) { vRetornoImg.clear(); vRetornoImg.addElement(rs.getBlob("img")); vRetornoImg.addElement(rs.getString("nameimg")); return famhn.upload.central.FileUpload.materializarImagen(path + "/", conn, vRetornoImg); } return "Pendiente"; } catch (Exception e) { System.out.println("" + e.toString()); return e.toString(); } finally { try { rs.close(); rs = null; } catch (Exception er) {; } try { st.close(); st = null; } catch (Exception err) {; } } }