Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

reading values into JTable  RSS feed

 
kate damond
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I'm trying to read data from an Access database and use it to populate the cells in a JTable. I have a class, TestTableModel which extends javax.swing.table.AbstractTableModel, and my JTable connects to this data model. So far so good.
The constructor for TestTableModel just calls the method dbStuff(). Here is the code for dbStuff():
----------------------------------------------------
void dbStuff() {
try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection("jdbc dbc:Molly", "admin", "");
Statement stmt = con.createStatement();
ResultSet MyResults = stmt.executeQuery("select ID, Current, Parent, atLevel, Expanded from UCNData");
ResultSetMetaData MyMetaResults = MyResults.getMetaData();
int cols = MyMetaResults.getColumnCount();
data = new String[20][cols];
int i = 0;
while (MyResults.next()) {
for (int j = 1; j < cols; j++) {
System.out.println("MyResults.getString(j) is " + MyResults.getString(j) + " so there");
//data[i][j] = MyResults.getString(j);
}
++i;
}
MyResults.close();
stmt.close();
con.close();
} catch (Exception E) {System.err.println("GOOD GRIEF !: " + E.getMessage());
E.printStackTrace(System.err);
}
}
---------------------------------------------------------------
It starts to work OK - I can see that the data has been read from the database, cos the system.out.println works giving me each successive value of MyResults.getString(j), which are the individual cells of data from the database. However, I can't do a thing with MyResults.getString(j) apart from system.out.println it !!! I've tried putting it in its place in the array 'data', (the commented out line above), I've tried assigning to a temporary variable. Whatever I do with it I get the error ... "GOOD GRIEF !: No data found" Yet we no it has a value at that point, because I just saw it on the console window !
What do you think it means by this ?
Many Thanks,
Kate
 
Adrian Yan
Ranch Hand
Posts: 688
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You mean you can't display them in a JTable?
If you want Jtable, you need to implement AbstactTable class.
 
kate damond
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, I mean before I even try to put them in the JTable, I can't do anything with the string which results from MyResults.getString(j). I mean, I can't write
String s = MyResults.getString(j), or Data[i][j] = MyResults.getString(j) or anything. Any attempt to do this results in data not found error.
Yes, there are other parts to this program, in particular, I have a class testModelClass, which extends AbstractTableModel. I did not include all the code for all the parts of this, cos the error occurs before this stage.
Cheers,
Kate
 
kate damond
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's Ok, it's cos I was println - ing it out first. If I just assign it to it's place in the array to start with it works fine.
Cheers,
Kate
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!