java - jtable returns only one row from the database -


i want table return data database without specifying number of rows, used autoincrement in default tablemodel doesnnot display rows, have define table outside actionlistener add jpanel,so problem table returns 1 row fro 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 seet join ( select adm, extract(year date) tyear,class, term, sum(paid) total_paid fees group adm,class,term) b on a.class = b.class , b.adm = '"+adms.gettext()+"' , a.term = b.term , 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 jtable's values setvalueat(...) method instead create defaulttablemodel object within actionperformed(...) method, , inside while (rs.next()) method create new vector<string> fill data current row of resultset, , @ end of while loop, pass vector model calling addrow(vector v) method. pass model jtable calling 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


Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -