public static void queryBookAuthor(Connection conn) { RowSetFactory factory; try { // Create a new RowSetFactory factory = RowSetProvider.newFactory(); // Create a CachedRowSet object using the factory bookAuthors = factory.createCachedRowSet(); // Alternatively opulate the CachedRowSet connection settings // crs.setUsername(createConn.getUsername()); // crs.setPassword(createConn.getPassword()); // crs.setUrl(createConn.getJdbcUrl()); // Populate a query that will obtain the data that will be used bookAuthors.setCommand("SELECT ID, LASTNAME, FIRSTNAME FROM BOOK_AUTHOR"); bookAuthors.execute(conn); // You can now work with the object contents in a disconnected state while (bookAuthors.next()) { System.out.println( bookAuthors.getString(1) + ": " + bookAuthors.getString(2) + ", " + bookAuthors.getString(3)); } } catch (SQLException ex) { ex.printStackTrace(); } }
protected void prepareStatement(String SQL) throws SQLException { // the default is TYPE_SCROLL_INSENSITIVE and CONCUR_READ_ONLY // which is good cachedResults = new CachedRowSetImpl(); cachedResults.setCommand(SQL); cachedResults.setFetchDirection(ResultSet.FETCH_UNKNOWN); // the BL list class will set the parameters of the query }
/** * @throws Exception * @tests java.sql.rowset.joinRowSet#getWhereClause() */ public void testGetWhereClause_MultipleCachedRowSet() throws Exception { String whereClause; // Creates another cached rowset. CachedRowSet crset2; crset2 = newNoInitialInstance(); crset2.setCommand("SELECT * FROM BOOKS"); crset2.setUrl(DERBY_URL); crset2.execute(); // Tests when there are on jdbcRowSet and one CachedRowSet. jrs.addRowSet(crset2, 1); jrs.addRowSet(crset, 1); if (System.getProperty("Testing Harmony") == "true") { whereClause = jrs.getWhereClause(); assertNotNull(whereClause); } else { try { whereClause = jrs.getWhereClause(); fail("Should throw NullPointerException."); } catch (NullPointerException e) { // Expected. } } crset.setTableName("Table1"); crset2.setTableName("Table2"); if (System.getProperty("Testing Harmony") == "true") { whereClause = jrs.getWhereClause(); assertNotNull(whereClause); } else { try { whereClause = jrs.getWhereClause(); fail("Should throw SQLException."); } catch (SQLException e) { // Expected. } } crset.setMatchColumn("ID"); crset2.setMatchColumn("AUTHORID"); whereClause = jrs.getWhereClause(); assertNotNull(whereClause); jrs = newJoinRowSet(); jrs.addRowSet(crset2, "AUTHORID"); jrs.addRowSet(crset, "ID"); whereClause = jrs.getWhereClause(); assertNotNull(whereClause); crset2.close(); }
/** * @throws Exception * @tests java.sql.rowset.joinRowSet#getWhereClause() */ public void testGetWhereClause_MoreRowSets() throws Exception { crset.setTableName("Table1"); jrs.addRowSet(crset, "ID"); // Creates another cached rowset. CachedRowSet crset2; crset2 = newNoInitialInstance(); crset2.setCommand("SELECT * FROM BOOKS"); crset2.setUrl(DERBY_URL); crset2.execute(); crset2.setTableName("Table2"); jrs.addRowSet(crset2, "AUTHORID"); crset2 = newNoInitialInstance(); crset2.setCommand("SELECT * FROM BOOKS"); crset2.setUrl(DERBY_URL); crset2.execute(); crset2.setTableName("Table3"); jrs.addRowSet(crset2, "AUTHORID"); String whereClause = jrs.getWhereClause(); assertNotNull(whereClause); }
@Test public void testGetInfoRowSet() { CachedRowSet rws; try { rws = new CachedRowSetImpl(); rws.setCommand(ALLEGATO5_FIND_BYPK); rws.setInt(1, 6); rws.execute(conn); rws.next(); allegato5 = Allegato5Assembler.getInfo(rws); rws.close(); db.closeConnection(conn); } catch (SQLException e) { fail(e.getMessage()); } catch (DBAccessException e) { fail(e.getMessage()); } }
public static void queryAuthorWork(Connection conn) { RowSetFactory factory; try { // Create a new RowSetFactory factory = RowSetProvider.newFactory(); // Create a CachedRowSet object using the factory authorWork = factory.createCachedRowSet(); // Alternatively opulate the CachedRowSet connection settings // crs.setUsername(createConn.getUsername()); // crs.setPassword(createConn.getPassword()); // crs.setUrl(createConn.getJdbcUrl()); // Populate a query that will obtain the data that will be used authorWork.setCommand("SELECT ID, AUTHOR_ID, BOOK_ID FROM AUTHOR_WORK"); authorWork.execute(conn); // You can now work with the object contents in a disconnected state while (authorWork.next()) { System.out.println( authorWork.getString(1) + ": " + authorWork.getInt(2) + " - " + authorWork.getInt(3)); } } catch (SQLException ex) { ex.printStackTrace(); } }