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 4 of 4

Thread: Autocommit

  1. #1
    Junior Member
    Join Date
    Oct 2012
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Autocommit

    Hello everyone, I'm finishing my first project that involves database and I have problem with one of the sql. I'll try to explain it, please bear with me, my English is not the best.
    Basically, I have db with movie titles and i want to display every movie that was released after the date that is entered by the user. For some reason it is showing me only the first one and following message: "ResultSet not open. Verify that autocommit is OFF."
    Could someone point me to what I'm doing wrong. Thanks a lot.
     
    public static void New_Release () {
        PreparedStatement query7;
     
        String ReleaseDate =  JOptionPane.showInputDialog(null, "Please enter the Date!);
     
        try{
     
        String host = ".......";
        String uName = "";
        String uPass = "";
     
        Connection con = DriverManager.getConnection(host);
     
     
     
        String sql = "SELECT TITLE from DVD WHERE RELEASEDATE > ? ";
            query7 = con.prepareStatement(sql);
     
            query7.setString(1,ReleaseDate);
            ResultSet rs1 = query7.executeQuery();
     
            while (rs1.next()){
            String TITLE = rs1.getString(1);
            query7.executeQuery();
     
     
     
            } // end of while
        } // end of try
     
        catch (SQLException err) {
     
          System.out.println(err.getMessage());
          } // end of catch
     
        } // end of new_release method


  2. #2
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    609
    My Mood
    Sleepy
    Thanks
    10
    Thanked 93 Times in 86 Posts

    Default Re: Autocommit

    That query7.executeQuery(); inside the while loop is nonsense. Is RELEASEDATE a CHAR/VARCHAR or a DATE column? You fetch the titles into the TITLE String which is defined inside the loop, so you can only have one title. If the query returns 10 titles you only see the last one, briefly until the while loop is closed.
    You never clean up resources there. The preparedStatement and ResultSet are never closed.

    I suggest you read the JDBC tutorial: Trail: JDBC(TM) Database Access (The Java™ Tutorials)


    Get that straight first and then see whether you still have a problem.

  3. #3
    Junior Member
    Join Date
    Oct 2012
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Autocommit

    RELEASEDATE is a DATE column. I got rid of 'query7.executeQuery();' and it works. It is showing all the movie titles. Now, I will read those tutorials. Thanks

  4. #4
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    609
    My Mood
    Sleepy
    Thanks
    10
    Thanked 93 Times in 86 Posts

    Default Re: Autocommit

    Quote Originally Posted by kave2 View Post
    RELEASEDATE is a DATE column.
    Then this is wrong/ error prone:
    query7.setString(1,ReleaseDate);

Similar Threads

  1. Ask about autocommit etc.
    By Yoga Herawan in forum JDBC & Databases
    Replies: 2
    Last Post: October 6th, 2011, 09:34 AM