public static void main(String[] args) throws SQLException { try { JdbcRowSet rowSet = RowSetProvider.newFactory().createJdbcRowSet(); rowSet.setUrl(URL + DB_NAME); rowSet.setPassword(PASSWORD); rowSet.setUsername(USER_NAME); rowSet.setCommand("SELECT * FROM customers WHERE sex = ?"); rowSet.setString(1, "male"); rowSet.execute(); while (rowSet.next()) { System.out.println(rowSet.getRow() + " " + rowSet.getString(2)); } } catch (SQLException e) { e.printStackTrace(); } }
public void testExecute_SelectCmd() throws Exception { JdbcRowSet jrs = newJdbcRowSet(); jrs.setCommand("SELECT * FROM USER_INFO WHERE ID > ?"); jrs.setUrl(DERBY_URL); jrs.setInt(1, 1); jrs.execute(); assertEquals(ResultSet.TYPE_SCROLL_INSENSITIVE, jrs.getType()); assertEquals(ResultSet.CONCUR_UPDATABLE, jrs.getConcurrency()); assertTrue(jrs.getAutoCommit()); assertTrue(jrs.first()); assertEquals(2, jrs.getInt(1)); assertTrue(jrs.absolute(2)); assertEquals(3, jrs.getInt(1)); assertTrue(jrs.last()); assertEquals(4, jrs.getInt(1)); assertTrue(jrs.previous()); assertEquals(3, jrs.getInt(1)); assertTrue(jrs.relative(-1)); assertEquals(2, jrs.getInt(1)); jrs.close(); }
public void testUpdateRow_Normal() throws Exception { JdbcRowSet jrs = newJdbcRowSet(); jrs.setCommand("SELECT * FROM USER_INFO"); jrs.setUrl(DERBY_URL); jrs.execute(); assertTrue(jrs.absolute(3)); assertEquals(3, jrs.getInt(1)); assertEquals("test3", jrs.getString(2)); jrs.updateString(2, "update3"); assertFalse(jrs.rowUpdated()); jrs.updateRow(); assertTrue(jrs.rowUpdated()); assertEquals("update3", jrs.getString(2)); jrs.close(); // check db rs = st .executeQuery("SELECT COUNT(*) FROM USER_INFO WHERE NAME = 'update3'"); assertTrue(rs.next()); assertEquals(1, rs.getInt(1)); }
public void testDeleteRow() throws Exception { insertMoreData(6); JdbcRowSet jrs = newJdbcRowSet(); jrs.setCommand("SELECT * FROM USER_INFO"); jrs.setUrl(DERBY_URL); jrs.execute(); assertTrue(jrs.first()); jrs.deleteRow(); try { jrs.getInt(1); fail("should throw SQLException"); } catch (SQLException e) { // expected } jrs.close(); /* * Check database */ rs = st.executeQuery("SELECT COUNT(*) FROM USER_INFO WHERE ID = 1"); assertTrue(rs.next()); assertEquals(0, rs.getInt(1)); }
public void testGetStatement() throws Exception { JdbcRowSet jrs = newJdbcRowSet(); assertNull(jrs.getStatement()); jrs.setCommand("SELECT * FROM USER_INFO"); jrs.setUrl(DERBY_URL); jrs.execute(); assertTrue(jrs.getStatement() instanceof PreparedStatement); jrs.close(); try { assertNull(jrs.getStatement()); fail("Should throw SQLException"); } catch (SQLException e) { // expected } }
public void testGetRowSetWarnings() throws Exception { JdbcRowSet jrs = newJdbcRowSet(); jrs.setCommand("SELECT * FROM USER_INFO"); jrs.setUrl(DERBY_URL); jrs.execute(); /* * TODO not sure when and how will cause RowSetWarning */ assertNull(jrs.getRowSetWarnings()); jrs.close(); jrs = newJdbcRowSet(); RowSetWarning warning = jrs.getRowSetWarnings(); super.assertNull(warning); // TODO Try to produce warnings. jrs.close(); // Checks for Exception after closing jrs. assertNull(rs.getWarnings()); assertNull(jrs.getRowSetWarnings()); }
public void testSetMatchColumn_Initial() throws Exception { JdbcRowSet noInitalJrs = noInitalJdbcRowSet(); JdbcRowSet jrs = newJdbcRowSet(); String[] names = { "1", "2", "3" }; jrs.setMatchColumn(names); names = jrs.getMatchColumnNames(); assertEquals(13, names.length); assertEquals("1", names[0]); assertEquals("2", names[1]); assertEquals("3", names[2]); try { jrs.unsetMatchColumn(new String[] { "3", "2", "1" }); fail("Should throw SQLException"); } catch (SQLException e) { // expected, Columns being unset are not the same as set } }
public static void main(String[] args) throws SQLException { Connection conn = CreateH2Database.createSimpleDatabase("~/test", true, true); RowSetFactory rowSetFactory = RowSetProvider.newFactory(); JdbcRowSet dataModel = rowSetFactory.createJdbcRowSet(); dataModel.setUrl(conn.getMetaData().getURL()); dataModel.setUsername(conn.getMetaData().getUserName()); dataModel.setPassword(""); dataModel.setCommand("select * from test_table"); dataModel.execute(); // now update the database dataModel.absolute(1); dataModel.setString(2, "Hello world 2"); // This throws an exception but according to // http://docs.oracle.com/javase/tutorial/jdbc/basics/jdbcrowset.html#updating-column-value // it shouldn't try { dataModel.updateRow(); } catch (Exception ex) { ex.printStackTrace(); } conn.close(); }
public static void main(String args[]) { // connect to database books and query database try (JdbcRowSet rowSet = RowSetProvider.newFactory().createJdbcRowSet()) { // specify JdbcRowSet properties rowSet.setUrl(DATABASE_URL); rowSet.setUsername(USERNAME); rowSet.setPassword(PASSWORD); rowSet.setCommand("SELECT * FROM authors"); // set query rowSet.execute(); // execute query // process query results ResultSetMetaData metaData = rowSet.getMetaData(); int numberOfColumns = metaData.getColumnCount(); System.out.println("Authors Table of Books Database:\n"); // display rowset header for (int i = 1; i <= numberOfColumns; i++) System.out.printf("%-8s\t", metaData.getColumnName(i)); System.out.println(); // display each row while (rowSet.next()) { for (int i = 1; i <= numberOfColumns; i++) System.out.printf("%-8s\t", rowSet.getObject(i)); System.out.println(); } } catch (SQLException sqlException) { sqlException.printStackTrace(); System.exit(1); } }
private JdbcRowSet newJdbcRowSet() throws Exception { if ("true".equals(System.getProperty("Testing Harmony"))) { return (JdbcRowSet) Class.forName( "org.apache.harmony.sql.internal.rowset.JdbcRowSetImpl") .newInstance(); } return (JdbcRowSet) Class.forName("com.sun.rowset.JdbcRowSetImpl") .newInstance(); }
public void testCursorMove() throws Exception { insertMoreData(6); JdbcRowSet jrs = newJdbcRowSet(); jrs.setUrl(DERBY_URL); jrs.setCommand("SELECT * FROM USER_INFO"); jrs.execute(); jrs.beforeFirst(); assertTrue(jrs.isBeforeFirst()); assertTrue(jrs.next()); assertTrue(jrs.first()); assertFalse(jrs.previous()); assertTrue(jrs.isBeforeFirst()); assertTrue(jrs.absolute(1)); assertTrue(jrs.first()); assertEquals(1, jrs.getInt(1)); assertTrue(jrs.relative(3)); assertEquals(4, jrs.getInt(1)); assertTrue(jrs.previous()); assertEquals(3, jrs.getInt(1)); assertTrue(jrs.relative(-2)); assertTrue(jrs.first()); assertTrue(jrs.absolute(10)); assertEquals(10, jrs.getInt(1)); assertTrue(jrs.last()); assertFalse(jrs.next()); assertTrue(jrs.isAfterLast()); assertTrue(jrs.relative(-2)); assertEquals(9, jrs.getInt(1)); jrs.afterLast(); assertTrue(jrs.isAfterLast()); assertTrue(jrs.absolute(-3)); assertEquals(8, jrs.getInt(1)); jrs.close(); }
public void testExecute_UpdateCmd() throws Exception { JdbcRowSet jrs = newJdbcRowSet(); jrs.setCommand("Update User_INFO set Name= ? Where ID= ? "); jrs.setUrl(DERBY_URL); jrs.setString(1, "update"); jrs.setInt(2, 3); try { jrs.execute(); fail("should throw SQLException"); } catch (SQLException e) { // expected } jrs.close(); }
public void testExecute_DeleteCmd() throws Exception { JdbcRowSet jrs = newJdbcRowSet(); jrs.setCommand("DELETE FROM USER_INFO"); jrs.setUrl(DERBY_URL); try { jrs.execute(); fail("should throw SQLException"); } catch (SQLException e) { // expected } jrs.close(); }
public void testExecute_InsertCmd() throws Exception { JdbcRowSet jrs = newJdbcRowSet(); jrs.setCommand("insert into USER_INFO(ID,NAME) values (6,'insert6')"); jrs.setUrl(DERBY_URL); try { jrs.execute(); fail("should throw SQLException"); } catch (SQLException e) { // expected } jrs.close(); }
public void testCancelRowUpdates() throws Exception { JdbcRowSet jrs = newJdbcRowSet(); jrs.setCommand("SELECT * FROM USER_INFO"); jrs.setUrl(DERBY_URL); jrs.execute(); jrs.moveToInsertRow(); try { jrs.cancelRowUpdates(); fail("should throw SQLException"); } catch (SQLException e) { // expected } jrs.moveToCurrentRow(); assertTrue(jrs.absolute(3)); jrs.updateString(2, "update3"); jrs.updateRow(); assertTrue(jrs.rowUpdated()); assertEquals("update3", jrs.getString(2)); jrs.cancelRowUpdates(); assertEquals("update3", jrs.getString(2)); assertTrue(jrs.next()); assertEquals(4, jrs.getInt(1)); jrs.updateString(2, "update4"); assertFalse(jrs.rowUpdated()); assertEquals("update4", jrs.getString(2)); jrs.cancelRowUpdates(); assertEquals("test4", jrs.getString(2)); jrs.close(); /* * Check database */ rs = st.executeQuery("SELECT * FROM USER_INFO WHERE ID = 3"); assertTrue(rs.next()); assertEquals("update3", rs.getString(2)); }
public void testAutoCommit_False() throws Exception { JdbcRowSet jrs = newJdbcRowSet(); jrs.setCommand("SELECT * FROM USER_INFO"); jrs.setUrl(DERBY_URL); jrs.execute(); jrs.setAutoCommit(false); assertFalse(jrs.getAutoCommit()); assertTrue(jrs.absolute(3)); jrs.updateString(2, "update3"); jrs.updateRow(); jrs.commit(); jrs.rollback(); /* * TODO why throw NullPointerException after call rollback()? */ try { jrs.absolute(1); fail("should throw exception"); } catch (NullPointerException e) { // expected } jrs.close(); /* * Check database */ rs = st .executeQuery("SELECT COUNT(*) FROM USER_INFO WHERE NAME = 'update3'"); assertTrue(rs.next()); assertEquals(1, rs.getInt(1)); }
/** * @throws SQLException * @tests java.sql.rowset.joinRowSet#getRowSets() */ public void testGetRowSets_SingleJdbcRowSet() throws Exception { // Creates jdbc rowset. JdbcRowSet jdbcRs; jdbcRs = newJdbcRowSet(); jdbcRs.setCommand("SELECT * FROM USER_INFO"); jdbcRs.setUrl(DERBY_URL); jdbcRs.execute(); jdbcRs.setMatchColumn(1); jrs.addRowSet(jdbcRs); CachedRowSet returnRowset = (CachedRowSet) jrs.getRowSets().iterator() .next(); assertNotSame(returnRowset, jrs); jrs.absolute(4); jrs.updateString(2, "Updated 4"); jrs.updateRow(); jrs.absolute(4); assertEquals("Updated 4", jrs.getString(2)); // TODO It is Strange. According to spec, the returned rowset should // maintain // the update occured in the joinrowset. returnRowset.absolute(4); assertEquals("test4", returnRowset.getString(2)); jdbcRs.absolute(3); jdbcRs.updateString(2, "Updated 3"); jdbcRs.updateRow(); returnRowset.absolute(3); assertEquals("test3", returnRowset.getString(2)); jdbcRs.close(); }
/** * @throws Exception * @tests java.sql.rowset.joinRowSet#getWhereClause() */ public void testGetWhereClause_SingleJdbcRowSet() throws Exception { String whereClause; // Creates jdbc rowset. JdbcRowSet jdbcRs; jdbcRs = newJdbcRowSet(); jdbcRs.setCommand("SELECT * FROM USER_INFO"); jdbcRs.setUrl(DERBY_URL); jdbcRs.execute(); jdbcRs.setMatchColumn("ID"); jrs.addRowSet(jdbcRs); if (System.getProperty("Testing Harmony") == "true") { whereClause = jrs.getWhereClause(); assertNotNull(whereClause); } else { try { whereClause = jrs.getWhereClause(); fail("Should throw NullPointerException."); } catch (NullPointerException e) { // Expected. } } CachedRowSet rowSetInJrs = (CachedRowSet) jrs.getRowSets().iterator() .next(); rowSetInJrs.setTableName("Table1"); whereClause = jrs.getWhereClause(); assertNotNull(whereClause); }
protected JdbcRowSet newJdbcRowSet() throws Exception { try { return (JdbcRowSet) Class.forName("com.sun.rowset.JdbcRowSetImpl") .newInstance(); } catch (ClassNotFoundException e) { return (JdbcRowSet) Class.forName( "org.apache.harmony.sql.internal.rowset.JdbcRowSetImpl") .newInstance(); } }
public void testUnSetMatchColumn() throws Exception { JdbcRowSet noInitalJrs = noInitalJdbcRowSet(); JdbcRowSet jrs = newJdbcRowSet(); int[] indexs = { 1, 2, 3 }; jrs.setMatchColumn(indexs); try { jrs.unsetMatchColumn(new int[] { 3, 2, 1 }); fail("Should throw SQLException"); } catch (SQLException e) { // expected, Columns being unset are not the same as set } }
protected JdbcRowSet noInitalJdbcRowSet() throws Exception { try { return (JdbcRowSet) Class.forName("com.sun.rowset.JdbcRowSetImpl") .newInstance(); } catch (ClassNotFoundException e) { return (JdbcRowSet) Class.forName( "org.apache.harmony.sql.internal.rowset.JdbcRowSetImpl") .newInstance(); } }
protected JdbcRowSet newJdbcRowSet() throws Exception { JdbcRowSet noInitalJrs = noInitalJdbcRowSet(); noInitalJrs.setUrl(DERBY_URL); noInitalJrs.setCommand("SELECT * FROM USER_INFO"); noInitalJrs.execute(); return noInitalJrs; }
public void testIsClosed() throws Exception { JdbcRowSet jrs = newJdbcRowSet(); try { jrs.isClosed(); fail("Should throw UnsupportedOperationException"); } catch (UnsupportedOperationException e) { // expected } }