Reputation: 51
Hi I have here a code that works but doesnt update my JTable.
try {
insertRows();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
invTable.setAutoCreateRowSorter(true);
invTable.setBorder(javax.swing.BorderFactory.createEtchedBorder());
invTable.setModel(modelx);
invTable.getTableHeader().setReorderingAllowed(false);
insertRows, is called when the form is launched and it populates the blank table with the rows from my database.
i tried adding this code to an Add button which adds a new item to my database.
modelx.fireTableDataChanged();
but it doesnt update my table. how can i update it properly?
here is my code for insertRows():
private void insertRows() throws SQLException, Exception{
modelx = new DefaultTableModel();
modelx.addColumn("Name");
modelx.addColumn("ID");
modelx.addColumn("Weight");
modelx.addColumn("Doctor");
modelx.addColumn("Supplier");
modelx.addColumn("Department");
modelx.addColumn("Cost");
modelx.addColumn("Price");
modelx.addColumn("Quantity");
modelx.addColumn("Threshold");
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
database = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=D:\\floresClinic.accdb;";
conn = DriverManager.getConnection(database, "", "");
s = conn.createStatement();
rs = s.executeQuery("SELECT * FROM medItem ");
int cou = 0;
while(rs.next())
{
s1 = rs.getString("productName");
s2 = rs.getString("productID");
s3 = rs.getString("productWeight");
s4 = rs.getString("doctor");
s5 = rs.getString("sName");
s6 = rs.getString("department");
s7 = rs.getString("unitCost");
s8 = rs.getString("unitPrice");
s9 = rs.getString("quantity");
s0 = rs.getString("threshold");
if(!s1.equals("1")){
modelx.insertRow(0, new Object [] {s1, s2, s3, s4, s5, s6, s7, s8, s9, s0});
//String[] itemize ={s1,s2,s3,s4,s5,s5,s7,s8,s9,s0};
//System.out.println(s1+s2);
//modelx.addRow(itemize);
}
cou++;
}
}
Upvotes: 0
Views: 65
Reputation: 19158
As you mentioned,your modelx.fireTableDataChanged();
adds a new item to database,but there is no further code to retrieve those values from database which is why your JTable is not getting populated with any values!
You need to write a method to retrieve values in JTable from the database.
Also,you didn't show the codes for insertRows();
method!
A helpful link for you to get help using JTable and Database linking is
Upvotes: 1