简体   繁体   中英

jtable returns only one row from the database

i want the table to return the data from database without specifying the number of rows, i used the autoincrement in the default tablemodel but it doesnnot display all the rows, i have to define the table outside the actionlistener so as to add it to a JPanel,so the problem is my table returns only one row fro the database

int row = 1;
     Object [] colnames = {"year","term","balance"};
     JTable table4 = new JTable(new DefaultTableModel(colnames,++row));
     JScrollPane pane  = new JScrollPane(table4);





 saachs.addActionListener(new ActionListener(){

       public void actionPerformed(ActionEvent e) {
             String adm = adms.getText();
             int row = 0;

            Connection con = null;
            try {
                Class.forName("com.mysql.jdbc.Driver");
            } catch (ClassNotFoundException ex) {
                try {
                    throw new Exception ("driver not found");
                } catch (Exception e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
            }
            String url = "jdbc:mysql://localhost/test";
              try {
                con = DriverManager.getConnection(url,"abda","abda");
            } catch (SQLException e1) {
                e1.printStackTrace();
            }if(con!=null){
                Statement st = null;
                ResultSet rs = null;
                String query = "select a.year,a.term,a.payable - b.total_paid balance from seet a join ( select adm, extract(year from date) tyear,class, term, sum(paid) total_paid from fees group by adm,class,term) b on a.class = b.class and b.adm = '"+adms.getText()+"' and a.term = b.term and a.year = b.tyear";

                try {
                    st = con.createStatement();
                } catch (SQLException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
                try {
                    rs = st.executeQuery(query);
                } catch (SQLException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }

                try {
                    while(rs.next()){
                        table4.setValueAt(rs.getString(1),row,0);
                        table4.setValueAt(rs.getString(2),row,1);
                        table4.setValueAt(rs.getString(3),row,2);

                        row++;
                    }
                } catch (SQLException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
            }


        }

    });

Don't directly set your JTable's values with the setValueAt(...) method but instead create a DefaultTableModel object within your actionPerformed(...) method, and inside your while (rs.next()) method create a new Vector<String> that you fill with the data from the current row of the ResultSet, and at the end of the while loop, pass this Vector into your model by calling its addRow(Vector v) method. Then pass this model into the JTable by calling the table's setModel(TableModel model) method.

Something like:

   try {
      Object[] columnNames = { "year", "term", "balance" };
      DefaultTableModel model = new DefaultTableModel(columnNames, 0);
      while (rs.next()) {
         Vector<String> rowData = new Vector<>();
         rowData.add(rs.getString(1));
         rowData.add(rs.getString(2));
         rowData.add(rs.getString(3));
         row++;
         model.addRow(rowData);
      }
      table4.setModel(model);
   } catch (SQLException e1) {
      e1.printStackTrace();
   }

Warning: code has not beentested

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM