2 Replies Latest reply: Jul 17, 2007 1:04 AM by 807600 RSS

    jdbc displaying result problem

    807600
      First of all is resultset better or rowset?.

      Secondly i created a program in which i am trying to execute a simple query using rowset. My problem is i am able to display only authorid and firstname only but with no lastname. what am i doing wrong?
      import java.sql.ResultSetMetaData;
        import java.sql.SQLException;
        import javax.sql.rowset.JdbcRowSet;  
        import com.sun.rowset.JdbcRowSetImpl;
      
      public class JdbcRowSetTest {
           static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
           static final String DATABASE_URL = "jdbc:mysql://localhost/books";
           static final String USERNAME = "jhtp6";
           static final String PASSWORD = "jhtp6";
           
           public JdbcRowSetTest() {
                try {
                     Class.forName(JDBC_DRIVER);
                     System.out.println("Connection Established");
                     JdbcRowSet rowSet = new JdbcRowSetImpl();
                     rowSet.setUrl(DATABASE_URL);
                     rowSet.setUsername(USERNAME);
                     rowSet.setPassword(PASSWORD);
                     rowSet.setCommand("Select * FROM authors");
                     rowSet.execute();
                     ResultSetMetaData metaData = rowSet.getMetaData();
                     int number = metaData.getColumnCount();
                     System.out.println("Author table for Books");
                for(int i=1; i<number; i++) 
                           System.out.printf( "%-8s\t", metaData.getColumnName( i ) );
                          System.out.println();
                     while(rowSet.next()) {
                     for(int i=1; i<number; i++) 
                               System.out.printf("%-8s\t", rowSet.getObject(i));
                               System.out.println();
                          
                     }
                }
                
                catch (SQLException e) {
                     e.printStackTrace();
                     System.exit(1);
                }
                catch (ClassNotFoundException e) {
                     e.printStackTrace();
                     System.exit(1);
                }
           }
           
           public static void main(String[] args) {
                JdbcRowSetTest window = new JdbcRowSetTest();
           }
           
      }
      Connection Established
      Author table for Books
      authorID     firstName     
      1            Harvey       
      2            Paul         
      3            Tem          
      4            Sean   
      and here is my sql query results in mysql command prompt
      mysql> select * from authors;
      +----------+-----------+----------+
      | authorID | firstName | lastName |
      +----------+-----------+----------+
      |        1 | Harvey    | John  |
      |        2 | Paul      | Paula   |
      |        3 | Tem       | Nieto    |
      |        4 | Sean      | Santry   |
      +----------+-----------+----------+
      4 rows in set (0.00 sec)