Results 1 to 3 of 3
Thread: Annoying database problem!
- 05-29-2008, 12:54 AM #1
Member
- Join Date
- May 2008
- Posts
- 4
- Rep Power
- 0
Annoying database problem!
Hi,
I was wondering if anyone could help me. After running the below code, I get a SQL Exception "ResultSet is closed" as I leave the while(c.nextPage()) loop - can someone tell me why and also how to stop the exception and instead just get out of the while loop as it's meant to and run the next lot of code?
Thanks.
Java Code:public JTable printData (CachedRowSet c){ try{ RowSetMetaData rsmd = (RowSetMetaData)c.getMetaData(); int cols = rsmd.getColumnCount(); String [] columns = new String[cols]; for (int i = 0; i<cols; i++){ columns[i] = rsmd.getColumnName(i+1); } c.last(); int rows = c.getRow(); c.first(); String [][] data = new String [rows][cols]; int i = 0; c.beforeFirst(); while(c.nextPage()){ // ERROR AS LEAVE // Inner code works fine - populates data as required while(c.next()){ for (int j = 0; j<cols; j++){ data[i][j] = c.getString(j+1); } i++; System.out.println("c.next"); } } DefaultTableModel dm = new DefaultTableModel(); dm.setDataVector(data, columns); JTable table = new JTable(dm); return table; } catch (SQLException e) { System.err.println("Could not connect: " + e + "\n" + e.getMessage() ); e.printStackTrace(); JTable table = new JTable(); return table; } }
- 05-29-2008, 06:42 AM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Did you get at least one entry to the table?
- 05-29-2008, 09:43 AM #3
Member
- Join Date
- May 2008
- Posts
- 4
- Rep Power
- 0
Yes, the inner code populates data with the 4 rows that resulted from the query. The only problem is when I exit the outer while (c.nextPage()) loop - instead of going to the code below it, it goes straight to the SQL Exception catch block.
When I run this simple test code, the while(c.nextPage()) loop works fine - just exits as expected (so get hello, hello, hello, hello, hi as predicted).
Java Code:try { try { Class.forName("org.postgresql.Driver"); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } CachedRowSet c = new CachedRowSetImpl(); Connection conn = DriverManager.getConnection ("jdbc:postgresql://db.doc.ic.ac.uk/g0516315_u","g0516315_u","ncSCbCon9E"); PreparedStatement stmt = conn.prepareStatement("SELECT * FROM ppl", ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); ResultSet r = stmt.executeQuery(); c.setPageSize(1); c.populate(r, 1); while (c.nextPage()){ System.out.println("hello"); } System.out.print("hi"); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }
Similar Threads
-
[SOLVED] An annoying warning everytime I compile
By Leprechaun in forum New To JavaReplies: 8Last Post: 04-23-2008, 08:11 AM -
problem in connecting to mysql database
By nancyv in forum Java ServletReplies: 6Last Post: 04-02-2008, 11:33 AM -
Problem with jTable that is binded with a table in MySQL Database
By rajkenneth in forum NetBeansReplies: 0Last Post: 03-29-2008, 03:36 PM -
Problem connecting to my Database using Java
By javaneed in forum New To JavaReplies: 3Last Post: 08-13-2007, 09:35 AM -
Problem to connect Database
By Swamipsn in forum NetBeansReplies: 1Last Post: 08-07-2007, 07:13 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks