Results 1 to 3 of 3
  1. #1
    DC1
    DC1 is offline Member
    Join Date
    May 2008
    Posts
    4
    Rep Power
    0

    Default 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;
    		}		
    	}

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  3. #3
    DC1
    DC1 is offline Member
    Join Date
    May 2008
    Posts
    4
    Rep Power
    0

    Default

    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

  1. [SOLVED] An annoying warning everytime I compile
    By Leprechaun in forum New To Java
    Replies: 8
    Last Post: 04-23-2008, 08:11 AM
  2. problem in connecting to mysql database
    By nancyv in forum Java Servlet
    Replies: 6
    Last Post: 04-02-2008, 11:33 AM
  3. Replies: 0
    Last Post: 03-29-2008, 03:36 PM
  4. Problem connecting to my Database using Java
    By javaneed in forum New To Java
    Replies: 3
    Last Post: 08-13-2007, 09:35 AM
  5. Problem to connect Database
    By Swamipsn in forum NetBeans
    Replies: 1
    Last Post: 08-07-2007, 07:13 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •