1

I'm totally new to programming and not really good with it. This is my first question in stack overflow. I'm doing a project where I use java swing to develop a hotel management desktop application. I need help on displaying data from 2 sql tables to a single jtable.I have created a method for that.A positive response is appreciated :). The code is stopped in the while loop and gives fail as output.

The code will be as follows:

public void displayTable(){
    try{
        String sql =  "SELECT customer.Cid,customer.Cname,customer.CEmail,customer.CContact,Reservation.checkIn,Reservation.checkOut from customer,Reservation where customer.Cid = Reservation.CusID";

        System.out.println("done");
        
        PreparedStatement pst;
        
        pst = connection.prepareStatement(sql);
        ResultSet rs = pst.executeQuery();
        System.out.println(jTable1);
        
        DefaultTableModel model = (DefaultTableModel) jTable1.getModel();
        model.setRowCount(0);
        
        List<itp.reservation.model.ReservationDetails> arrList = new ArrayList<itp.reservation.model.ReservationDetails>();
        List<itp.reservation.model.CustomerDetails> arList = new ArrayList<itp.reservation.model.CustomerDetails>();
        
        System.out.println("new done");
        while(rs.next())
        { 
             System.out.println("lev");
          itp.reservation.model.CustomerDetails x = new   itp.reservation.model.CustomerDetails();
          //itp.reservation.model.ReservationDetails y = new   itp.reservation.model.ReservationDetails();
            System.out.println("qwe");
          x.setCid(rs.getString(1));
          x.setCname(rs.getString(2));
          x.setCemail(rs.getString(3));
          x.setCcontact(rs.getInt(4));
           System.out.println("new");
          //y.setCheckin(rs.getDate(5));
          //y.setCheckout(rs.getDate(6));
          
          System.out.println("restable");
            
          //arrList.add(y);
          arList.add(x);
         
          
        }
        jTable1.setModel(model);
        
    }catch(Exception e){
        System.out.println("Fail");
    }
}
  • So what is your problem? Are there values in the ResultSet? If so then use the `addRow(...)` method of the DefaultTableModel to add each row of data to the model. See: https://stackoverflow.com/a/55635012/131872 for a complete working example. – camickr Oct 12 '20 at 03:48

0 Answers0