Getting java sql SQLException Operation not allowed after ResultSet closed

0 votes

The following code throws an exception when I run it. Because I'm preparing a new statement with the same connection object, I believe that is the problem. How should I change this so that I can use RS2 AND make a prepared statement? Even though the connection is to the same database, do I still need to construct a new connection object?

    try 
    {
        //Get some stuff
        String name = "";
        String sql = "SELECT `name` FROM `user` WHERE `id` = " + userId + " LIMIT 1;";
        ResultSet rs = statement.executeQuery(sql);
        if(rs.next())
        {
            name = rs.getString("name");
        }

        String sql2 = "SELECT `id` FROM  `profiles` WHERE `id` =" + profId + ";";
        ResultSet rs2 = statement.executeQuery(sql2);
        String updateSql = "INSERT INTO `blah`............"; 
        PreparedStatement pst = (PreparedStatement)connection.prepareStatement(updateSql);    

        while(rs2.next()) 
        { 
            int id = rs2.getInt("id");
            int stuff = getStuff(id);

            pst.setInt(1, stuff);
            pst.addBatch();

        }

        pst.executeBatch();

    } 
    catch (Exception e) 
    {
        e.printStackTrace();
    }

private int getStuff(int id)
{

    try
    {   

            String sql = "SELECT ......;";
            ResultSet rs = statement.executeQuery(sql);

            if(rs.next())
            {
                return rs.getInt("something");

            }
            return -1;
    }//code continues

Can someone please help me with this?

Aug 22, 2022 in Database by Kithuzzz
• 38,010 points
1,629 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.

Related Questions In Database

0 votes
1 answer
0 votes
0 answers

java.sql.SQLException: Missing IN or OUT parameter at index:: 1

Using OJDBC 6, I created some Java ...READ MORE

Aug 15, 2022 in Database by Kithuzzz
• 38,010 points
5,560 views
0 votes
0 answers

The java.sql.SQLException: Fail to convert to internal representation

I want to do this query: String query ...READ MORE

Aug 20, 2022 in Database by Kithuzzz
• 38,010 points
1,642 views
0 votes
1 answer

java.sql.SQLException: - ORA-01000: maximum open cursors exceeded

The maximum-open-cursors error, also known as ORA-01000, ...READ MORE

answered Sep 11, 2022 in Database by narikkadan
• 63,420 points

edited Jul 5, 2023 by Khan Sarfaraz 2,819 views
0 votes
1 answer

java.sql.SQLException: Column count doesn't match value count at row 1

I think PreparedStatement would be a better choice. I ...READ MORE

answered Sep 20, 2022 in Database by narikkadan
• 63,420 points
1,695 views
0 votes
1 answer

How to detect a SQL table's existence in Java?

Hello, Depending on the DB, you can do ...READ MORE

answered May 13, 2020 in Database by Niroj
• 82,880 points
750 views
0 votes
1 answer

Accessing connection from different class in Java / MySQL?

You should just instantiate DoComms with every ...READ MORE

answered Nov 14, 2018 in Database by nirvana
• 3,130 points
1,666 views
0 votes
0 answers

java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES)

This is the exception on getConnection(): java.sql.SQLException: Access denied ...READ MORE

May 16, 2022 in Java by Kichu
• 19,050 points
6,625 views
0 votes
0 answers

java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/dbname [duplicate]

I have this Java program: MySQLConnectExample.java import java.sql.*; import java.util.Properties; public ...READ MORE

Aug 21, 2022 in Database by Kithuzzz
• 38,010 points
1,913 views
0 votes
0 answers
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP