import java.sql.*; import javax.sql.*; import sun.jdbc.rowset.*; import java.io.*; public class MyWebRowSet { public static void main(String args[]) { MyWebRowSet rs = new MyWebRowSet(); try { rs.startup(); rs.proceed(); } catch (SQLException ex) { System.err.println("SQLException: " + ex.getMessage()); } } private void proceed() throws SQLException { WebRowSet wrs; wrs = new WebRowSet(); wrs.setUrl("jdbc:odbc:mydsn"); wrs.setUsername(""); wrs.setPassword(""); wrs.setCommand("select name, id from employee"); wrs.setTransactionIsolation(java.sql.Connection.TRANSACTION_READ_UNCOMMITTED); wrs.execute(); System.out.println("RowSet populated."); int c[] = {1,2}; wrs.setKeyColumns(c); try { java.io.FileWriter FW = new java.io.FileWriter("Employee.xml"); scrollData(wrs); wrs.writeXml(FW); } catch (Throwable ex) { System.out.println(ex.getMessage()); } } private void startup() throws SQLException { Connection con = getConnection(); con.setAutoCommit(false); con.setAutoCommit(false); Statement smt = con.createStatement(); try { smt.execute("drop table employee"); } catch (SQLException ex) { System.err.println("Caught drop table."); } // create the test table smt.execute("Create table employee (name char(10), id int)"); System.out.println("Table created."); // insert some test data smt.execute("insert into employee values ('John', 1)"); smt.execute("insert into employee values ('David', 2)"); smt.execute("insert into employee values ('Susan', 5)"); smt.execute("insert into employee values ('Rose', 7)"); System.out.println("Rows inserted"); // disconnect smt.close(); con.commit(); con.close(); } private Connection getConnection() { try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); } catch (ClassNotFoundException ex) { System.err.println("SQLException: " + ex.getMessage()); } try { return DriverManager.getConnection( "jdbc:odbc:mydsn", "",""); } catch (SQLException ex) { System.err.println("getConnection failed: " + ex.getMessage()); } return null; } private void scrollData(WebRowSet wrs) throws SQLException { System.out.println("Displaying Records..."); String name; int Id; wrs.beforeFirst(); while (wrs.next()) { name = wrs.getString(1); if (wrs.wasNull() == false) { System.out.println("name is " + name); } else { System.out.println("name is null"); } Id = wrs.getInt("id"); if (wrs.wasNull() == false) { System.out.println("Id is " + Id); if (Id == 2) { System.out.println("Updating!"); wrs.updateInt("id", 110); wrs.updateString("name", "Daniel"); wrs.updateRow(); wrs.deleteRow(); wrs.moveToInsertRow(); wrs.updateInt("id", 120); wrs.updateString("name", "Allen"); wrs.insertRow(); wrs.moveToCurrentRow(); wrs.next(); wrs.updateString("name", "Monica"); wrs.updateRow(); wrs.previous(); } } else { System.out.println("Id is null"); } } if (wrs.isAfterLast() == true) { System.out.println("We have reached the end"); System.out.println("This is row: " + wrs.getRow()); } System.out.println("And now backwards..."); while (wrs.previous()) { name = wrs.getString("name"); if (wrs.wasNull() == false) { System.out.println("name is " + name); } else { System.out.println("name is null"); } Id = wrs.getInt(2); if (wrs.wasNull() == false) { System.out.println("Id is " + Id); } else { System.out.println("Id is null"); } } if (wrs.isBeforeFirst() == true) { System.out.println("Cursor at the Start"); } wrs.first(); if (wrs.isFirst() == true) System.out.println("First Record"); System.out.println("Row Number: " + wrs.getRow()); if (wrs.isBeforeFirst() == false) System.out.println("Not Before First"); wrs.last(); if (wrs.isLast() == true) System.out.println("Reached Last Record"); System.out.println("Now the Record No is " + wrs.getRow()); if (wrs.isAfterLast() == false) System.out.println("Not after last"); } }