I,ve set up a JTable and want to populate it from an Access database useing jdbc odbc.
The data returned is placed into an array but my problem is how to use the array results with...
jTable1.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{list[0], list[1], list[2], list[3], list[4]},
},
new String [] {
"Product", "Price", "Stock", "Code", "Amount Sold"
}
));
full code as follows...
private void testtableActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
table.setBounds(0,0,525,550);
table.setLocationRelativeTo(null);
table.setVisible(true);
int rowCount =getRow();
Object[][] list = new Object [rowCount][5];
jTable1 = new javax.swing.JTable();
// connect to database
String data = "jdbc:odbc:SNbase";
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection conn = DriverManager.getConnection(
data, "", "");
Statement st = conn.createStatement();
// set sql query
ResultSet rec = st.executeQuery(
"SELECT * " +
"FROM stock_items");
while(rec.next()) {
for (int i=0; i<rowCount; i++){
// place query result into array
list[i][0] = rec.getString(2);
double num = Double.parseDouble(rec.getString(3));
list[i][1] = "£ " + num;
list[i][2] = rec.getString(4);
list[i][3] = rec.getString(5);
list[i][4] = rec.getString(7);
}
}
st.close();
} catch (SQLException s) {
System.out.println("SQL Error: " + s.toString() + " "
+ s.getErrorCode() + " " + s.getSQLState());
} catch (Exception e) {
System.out.println("Error: " + e.toString()
+ e.getMessage());
}
jTable1.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{list[0], list[1], list[2], list[3], list[4]},
},
new String [] {
"Product", "Price", "Stock", "Code", "Amount Sold"
}
));
jScrollPane1.setViewportView(jTable1);
// end method
}