0

I created a method to get the values from a database in java using SQL and store the information in a ResultSet and then use a while loop to store the information in a RentSendItem and store all those items in an ArrayList called sendList but when I try to run it, it gives me the error:

'ResultSet not open. Operation 'getString' not permitted. Verify that autocommit is off'

This is my class:

public void getDataFromDB() {
    System.out.println("Wordk");
    //connecting
    Connection connection = null;
    Statement statement = null;
    try {
        System.out.println("1");
        connection = DriverManager.getConnection(url, username, password);
        statement = connection.createStatement();
        ResultSet name = statement.executeQuery("SELECT firstname,surname FROM CUSTOMER");
        ResultSet titles = statement.executeQuery("Select Title,Category From ADDDVD ");
        System.out.println(name.getString("firstname"));
        System.out.println("2");
        while (name.next()) {
            String fullName = name.getString("firstname") + " " + name.getString("surname");
            RentSendItem item = new RentSendItem(name.getString("firstname") + name.getString("surname"), titles.getString("Category"), titles.getString("title"));
            sendList.add(item);
        }
        System.out.println("3");
    } catch (Exception e) {
        System.out.println("Error" + e.getMessage());
    }
}

So just want to know what am I doing wrong and will this class do what I want it to do. If you could maybe help me, I would be grateful.

2
  • comment below line and check System.out.println(name.getString("firstname")); Commented Nov 2, 2020 at 12:23
  • try once with the comment above the line and let me know what you will get. Commented Nov 2, 2020 at 12:27

1 Answer 1

1

There are several problems in your code.

  1. You can't call method getString() of interface java.sql.ResultSet before you call method next(). First call method next() and if that method returns "true", then you can call method getString().
  2. You also need to call method next() on titles.
  3. You can't call getString() twice on the same column on the same row.

Compare the below with your code.

public void getDataFromDB() {
    System.out.println("Wordk");
    // connecting
    try (Connection connection = DriverManager.getConnection(url, username, password);
         Statement statement = connection.createStatement()) {
        System.out.println("1");
        ResultSet name = statement.executeQuery("SELECT firstname,surname FROM CUSTOMER");
        ResultSet titles = statement.executeQuery("Select Title,Category From ADDDVD ");
        System.out.println("2");
        while (name.next()) {
            String firstname = name.getString("firstname");
            String surname = name.getString("surname");
            String fullName = firstname + " " + surname;
            if (titles.next()) {
                RentSendItem item = new RentSendItem(fullName,
                                                     titles.getString("Category"),
                                                     titles.getString("title"));
                sendList.add(item);
            }
        }
        System.out.println("3");
    }
    catch (Exception e) {
        e.printStackTrace();
    }
}

Also, the following are not problems but recommendations.

  1. In the catch block, it usually preferable to print the entire stack trace rather than just the error message.
  2. You should close the Connection and Statement once you no longer need them. In the code above, I have used try-with-resources
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.