Problem in populating jtable from database

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • PreethiGowri
    New Member
    • Oct 2012
    • 126

    Problem in populating jtable from database

    Jtable does not display the 1st row of my DB table,
    below is my code to populate the jtable -

    Code:
    try {
    Class.forName("com.mysql.jdbc.Driver");
                        String conURL = "jdbc:mysql://localhost/biometric";
     Connection con = DriverManager.getConnection(conURL, "root", "root");
    Statement st = con.createStatement();
    String query = "select sl,enrolladdr,employee_name from employee_details ";
    ResultSet rs = st.executeQuery(query);
    System.out.println(query);
    if (rs.next()) {
    Mymodel model = (Mymodel) MyDbUtils.resultSetToTableModel(rs);
    jTable1.setModel(model);
    }
    
     } catch (ClassNotFoundException | SQLException e) {
      e.printStackTrace();
      }
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    1.) You must close all connections in a finally block or use the try with resources construct.
    2.) if (rs.next()) moves to the first record in your resultset. So if you are doing while(rs.next() ) in the method resultSetToTabl eModel then you are skipping the first row because you are calling rs.next twice before accessing the data. Remove that if(rs.next()) check.

    Comment

    • PreethiGowri
      New Member
      • Oct 2012
      • 126

      #3
      Removing if(rs.next()) worked for me, Thanks:)

      Comment

      Working...