Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: java.sql.SQLException: [Microsoft][ODBC Driver Manager] Invalid cursor state

  1. #1
    Junior Member
    Join Date
    Aug 2009
    Posts
    8
    Thanks
    1
    Thanked 1 Time in 1 Post

    Question java.sql.SQLException: [Microsoft][ODBC Driver Manager] Invalid cursor state

    I am getting an exception (java.sql.SQLException: [Microsoft][ODBC Driver Manager] Invalid cursor state)
    while running this program given below. Please help.

    import java.sql.*;
    import java.io.*;
     
    class Dbconnect1 
    {
        public static void main(String[] args) 
        {
            String str[]={"Yahoo","Yah"};
            try{
                Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                Connection con=DriverManager.getConnection("jdbc;dbc:db");
     
                String query="Select distinct id from employee";
                Statement st=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
                ResultSet rs = st.executeQuery(query);
     
                rs.beforeFirst();
                Boolean b;
                while(rs.next())
                {
                        System.out.println(rs.getInt(1));
                        if(rs.isLast())
                            break;
                }
                st.close();
                con.close();
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }
            System.out.println("Hello World!");
        }
    }
    Last edited by JavaPF; August 28th, 2009 at 07:41 AM. Reason: Please use [code] ... [/code] tags


  2. #2
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: java.sql.SQLException: [Microsoft][ODBC Driver Manager] Invalid cursor state

    On what line?

    // Json

  3. #3
    Junior Member
    Join Date
    Aug 2009
    Posts
    8
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: java.sql.SQLException: [Microsoft][ODBC Driver Manager] Invalid cursor state

    System.out.println(rs.getInt(1));

  4. #4
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: java.sql.SQLException: [Microsoft][ODBC Driver Manager] Invalid cursor state

    Well here is a little rewrite for you.

    import java.sql.*;
    import java.io.*;
     
    class Dbconnect1 
    {
        public static void main(String[] args) 
        {
            String str[]={"Yahoo","Yah"};
            try{
                Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                Connection con=DriverManager.getConnection("jdbc;dbc:db");
     
                String query="Select distinct id from employee";
                Statement st=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
                ResultSet rs = st.executeQuery(query);
     
                rs.beforeFirst();
                boolean b = true;
     
                while(b)
                {
                        System.out.println(rs.getInt(1));
     
                       b = rs.next();
                }
                st.close();
                con.close();
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }
            System.out.println("Hello World!");
        }
    }

    Notice what I did to the while statement. rs.next actually moves the cursor forward so you are skipping the first row and if you only have one row you will end up in big problem.

    Now I set b to true and loop while b is true and set b to rs.next(), if rs.next returns false the loop will break.

    Give it a shot!

    // Json

  5. The Following User Says Thank You to Json For This Useful Post:

    guptapr (August 29th, 2009)

  6. #5
    Junior Member
    Join Date
    Aug 2009
    Posts
    8
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: java.sql.SQLException: [Microsoft][ODBC Driver Manager] Invalid cursor state

    Hi Json
    i tried it. But its giving same exception again. Is there any problem with the query i am firing? Query:
    String query="Select distinct id from employee";
    I am using Microsoft Access as my Database.

    Thanks,
    guptapr

  7. #6
    Junior Member
    Join Date
    Aug 2009
    Posts
    8
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: java.sql.SQLException: [Microsoft][ODBC Driver Manager] Invalid cursor state

    Thanks for help Json. But i think i was able to rectify my problem. It sounds funny but i just changed the line:
    Statement st=con.createStatement(ResultSet.TYPE_SCROLL_SENSI TIVE,ResultSet.CONCUR_UPDATABLE);
    to
    Statement st=con.createStatement();
    i dont know why? But it works.

    Regards,
    guptapr

  8. #7
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: java.sql.SQLException: [Microsoft][ODBC Driver Manager] Invalid cursor state

    I see, glad you managed to work it out

    // Json