Results 1 to 6 of 6

Thread: java.sql.SQLException{resolved}

  1. #1

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418

    Question java.sql.SQLException{resolved}

    java.sql.SQLException: Before start of resultset. *** does this mean? :confuesd: I thought that my code might be positioning the pointer of the the bounds of the table stored in memory. Any ideas on what might be wrong?

    Thanks.

    Code:
    import java.sql.*; 
    
      public class SQL{
        public static void main(String[] agrs){  
    
        try{  
        int columns = 0; 
    
        Class.forName("com.mysql.jdbc.Driver").newInstance();
     
        Connection c = DriverManager.getConnection("jdbc:mysql://localhost/test");
     
       
        Statement s = c.createStatement();
        ResultSet rs = s.executeQuery("select * from ifilms"); 
        columns = (rs.getMetaData()).getColumnCount();
        for(int i = 0; i < columns; ++i){
          System.out.println(rs.getString(i));
        }
       /*
        boolean more = true; 
        int index = 0;  
        while(more){
          System.out.println(rs.getString(++index));
          more = rs.next();
        }
      */
        }catch(Exception e){
          System.out.println(e); 
       }
      }
     }

  2. #2
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    Code:
        for(int i = 0; i < columns; ++i){
          System.out.println(rs.getString(i));
        }
    Its been a while, but don't you have to do an initial read() (or next() or whatever it is in Java) before you can access the data?
    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  3. #3
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    You have to indeed.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  4. #4

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    Ok ok happy now! Just kidding. I had rs.next() in the for loop before and the exception was still thrown. If the while loop is uncommented and the for loop commented it's the same result.
    Code:
    import java.sql.*; 
    
      public class SQL{
        public static void main(String[] agrs){  
    
        try{  
        int columns = 0; 
    
        Class.forName("com.mysql.jdbc.Driver").newInstance();
     
        Connection c = DriverManager.getConnection("jdbc:mysql://localhost/test");
     
       
        Statement s = c.createStatement();
        ResultSet rs = s.executeQuery("select * from ifilms"); 
        columns = (rs.getMetaData()).getColumnCount();
        for(int i = 0; i < columns; ++i){
          System.out.println(rs.getString(i));
          rs.next();
        }
       /*
        boolean more = true; 
        int index = 0;  
        while(more){
          System.out.println(rs.getString(++index));
          more = rs.next();
        }
      */
        }catch(Exception e){
          System.out.println(e); 
       }
      }
     }

  5. #5
    Fanatic Member brown monkey's Avatar
    Join Date
    Jun 2004
    Location
    Cebu
    Posts
    552
    Code:
    import java.sql.*;
    public class SQL{
       public static void main(String[] args){
          try{
             String url="jdbc:mysql://localhost/northwind";
             Class.forName("com.mysql.jdbc.Driver");
             Connection cn=DriverManager.getConnection(url);
    
             Statement s=cn.createStatement();
             ResultSet rs=s.executeQuery("select * from territories");
             while(rs.next()){
                for(int i=0;i<rs.getMetaData().getColumnCount()-1;i++)
                   System.out.print(rs.getObject(i+1)+"     ");
                System.out.println();
             }
          }
          catch(Exception e){
             e.printStackTrace();
          }
       }
    }
    Last edited by brown monkey; Aug 4th, 2004 at 01:35 AM.

  6. #6

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    Sorry i locked the wrong thread. I just merged in your thread so everything should be fine. I see how it's done now. Thanks for the help.

Posting Permissions

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



Click Here to Expand Forum to Full Width