import java.sql.*; import javax.sql.*; import sun.jdbc.rowset.*; public class MyJdbcRowSet { Connection conn = null; Statement stmt = null; JdbcRowSet jrs = null; public static void main(String Args[]) { try { MyJdbcRowSet rs = new MyJdbcRowSet(); rs.startup(); rs.proceed(); }catch(Exception mex) { System.out.println(mex); } } public void proceed() { try { EchoListener echo = new EchoListener(); jrs = new JdbcRowSet(); jrs.addRowSetListener(echo); jrs.setUrl("jdbc:odbc:mydsn"); jrs.setUsername(""); jrs.setPassword(""); jrs.setCommand("select name,id from employee"); jrs.execute(); scrollData(jrs); jrs.close(); } catch (SQLException ex) { System.err.println("SQLException: " + 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 (‘Sam’, 1)"); smt.execute("insert into employee values (‘John’, 2)"); smt.execute("insert into employee values (‘David’, 5)"); smt.execute("insert into employee values (null,null)"); smt.execute("insert into employee values (‘Susan’, 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; } public void scrollData(JdbcRowSet crs) throws SQLException { System.out.println("Displaying Records..."); String name; int Id; try { while (crs.next()) { try { name = crs.getString(1); if (crs.wasNull() == false) System.out.println("name is " + name); else System.out.println("name is null"); } catch (SQLException ex) { System.err.println("Unable to get: " + ex.getMessage()); } Id = crs.getInt(2); if (crs.wasNull() == false) System.out.println("Id is " + Id); else System.out.println("Id is null"); } } catch (SQLException ex) { ex.printStackTrace(); } } }