This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes JDBC and the fly likes Code works in main() but not in a handler Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Code works in main() but not in a handler " Watch "Code works in main() but not in a handler " New topic
Author

Code works in main() but not in a handler

Joe Kaz
Greenhorn

Joined: Aug 13, 2001
Posts: 14
I was am successfully retrieving DB table names from a local MS Access 2000 database with the code below in main(). However when I simply paste the exact same code into an actionPerformed() handler method, it generates this SQLException at the getString() statement:
[Microsoft] {ODBC Driver Manager] Invalid cursor state
The driver name is odbcjt32.dll v 2.0001 <04.00.4403>
I have tried playing around with repositioning the cursor first,
creating another connection first, and have tried other suggestions posted on various ODBC discussion boards for this error, but you really can't do much when using DatabaseMetaData. Again, I get all the way to the first getString() successfully and have determined that rs.next() is returning 'true' OK.
Microsoft has posted some admitted odbc bugs for this type of error regarding threading possibilities, which I think may be coming into play here once I move the code out of main()... I don't know... The most relevant MS notes on this may be at this URL: http://support.microsoft.com/default.aspx?scid=kb;EN-US;q253010
Thanks all, for any insights here.
Joe Kaz
Here's the code:
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection connection = DriverManager.getConnection("jdbc dbc:MyDSN");
DatabaseMetaData dbmd = connection.getMetaData();
ResultSet rs = dbmd.getTables( n,n , "%", m );
while (rs.next()) {
System.out.println("Table = " + rs.getString(3) );
}
Joe Kaz
Greenhorn

Joined: Aug 13, 2001
Posts: 14
Another point on that main() code:
I can successfully move from main() to the handler method any code creating ResultSets from regular Statements. This problem is just with the result set created from the DatabaseMetaData object.
Thanks
Joe
 
Don't get me started about those stupid light bulbs.
 
subject: Code works in main() but not in a handler