Reputation: 171
I have a problem regarding my JTable, I don't know how can I store my fetch records which is of type String from my database to the multidimensional array which is let's say Object[][] data. What I want to do is show my database records to the JTable, I already fetch the records in dtabase and store it in my String variables, The question is how can I store the fetch records to the multidimensional array of Object and use it on my JTable.
Here are my code for fetching records:
static class TableData{
Object[][] data;
int count = 0;
Statement sql = null;
String query, user = "JEROME", pass = "Perbert101", driver = "oracle.jdbc.OracleDriver", conString = "jdbc:oracle:thin:@127.0.0.1:1521:XE";
Connection con = null;
ResultSet rs = null;
TableData(){
try{
Class.forName(driver);
}
catch(ClassNotFoundException e){
JOptionPane.showMessageDialog(null, "Problem Loading Driver");
}
try{
con = DriverManager.getConnection(conString, user, pass);
sql = con.createStatement();
sql.executeQuery("SELECT * FROM INVENTORY");
rs = sql.getResultSet();
int key = 0;
String val = null, val1 = null, val2 = null, val3 = null, val4 = null, val5 = null;
System.out.println("Results: ");
while(rs.next()){
key = rs.getInt(1);
if(rs.wasNull()){
key = -1;
}
val = rs.getString(2);
if(rs.wasNull()){
val = null;
}
val1 = rs.getString(3);
if(rs.wasNull()){
val = null;
}
val2 = rs.getString(4);
if(rs.wasNull()){
val = null;
}
val3 = rs.getString(5);
if(rs.wasNull()){
val = null;
}
val4 = rs.getString(6);
if(rs.wasNull()){
val = null;
}
val5 = rs.getString(7);
if(rs.wasNull()){
val = null;
}
System.out.println("Key = " + key);
System.out.println("value = " + val);
System.out.println("value = " + val1);
System.out.println("value = " + val2);
System.out.println("value = " + val3);
System.out.println("value = " + val4);
System.out.println("value = " + val5);
}
sql.close();
con.close();
}
catch(SQLException e){
JOptionPane.showMessageDialog(null, "Error Loading Database Data");
}
}
}
//----------END------------
public static void main(String[] args){
POSModel.TableData data = new POSModel.TableData();
}
Upvotes: 0
Views: 899
Reputation: 347184
I'd suggest that the data you're pulling from the database needs to be stored in an array (by columns) first...
Object[] rowData = new Object[7];
rowData[0] = key;
rowData[1] = val;
rowData[2] = val1;
rowData[3] = val2;
rowData[4] = val3;
rowData[5] = val4;
rowData[6] = val5;
This then needs to be stored in some kind of row structure, I'd personally use a List. The main reason for this choice is that you probably don't know in advance the number of rows you will be reading...
List<Object[]> rowList = new ArrayList<Object[]>(25);
// Process the resultset...
// Create the column array from above...
rowList.add(rowData);
Once you've completed reading all the rows, you need to convert the list it an array...
data = rowList.toArray(new Object[](rowList.size())); // I like to provide my own array
Equally, you could do...
data = new Object[rowList.size()][7];
rowList.toArray(data);
Which ever is more convenient...
Now you should have a 2D array...
Upvotes: 1