Search code examples
javamysqlswingjtableresultset

Reading database data from more than two tables into a Swing table but the frame hangs indefinitely


Am trying to read database data into a Swing table from two database tables but the frame hands indefinitely.

 JDBCConnectionPigs c11 = new JDBCConnectionPigs();

  try {
   c11.createConnection().setAutoCommit(false);
   PreparedStatement ps = c11.createConnection().prepareStatement("SELECT * FROM sow_info");
   ResultSet rs=ps.executeQuery();
   int i1=0, a =0,b=1, c1=2,  d1 =3, e= 4,fs=5;      
   while(rs.next()){
   data4 =new ArrayList<>();
   data4.add(a, rs.getString("sow_info_id"));
   data4.add(b, rs.getString("sow_name"));
    data4.add(c1,rs.getString("sow_colour"));
    data4.add(d1,rs.getString("sow_breed"));
    data4.add(e,rs.getString("sow_date_of_birth"));

   PreparedStatement ps1 = c11.createConnection().prepareStatement("SELECT staff_id FROM staff");
    ResultSet rst=ps1.executeQuery();
    while(rst.last() ){
 stafid= rst.getString("staff_id");
    }
  data4.add(e, stafid);
   data5.add(i1, data4); 
  }
   model = new MyTableModel(data5, column1);
  jTable2.setModel(model);
  c11.createConnection().setAutoCommit(true);
  } catch (SQLException ex) {
  Logger.getLogger( SowSow.class.getName()).log(Level.SEVERE, null, ex);
   }
    if(c11!=null){
   c11.closeConnection();
   }

I need help to know the cause and how I can implement it differently.


Solution

  • try this code.I hope this working.

    PreparedStatement ps1 = c11.createConnection().prepareStatement("SELECT staff_id FROM staff");
    ResultSet rst=ps1.executeQuery();
    rst.last()
    {
     stafid= rst.getString("staff_id");
    }while(rst.previous());