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 in Database by Kithuzzz
• 11,640 points
56 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 in Database by Kithuzzz
• 11,640 points
237 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 in Database by Kithuzzz
• 11,640 points
59 views
0 votes
0 answers

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

An ORA-01000 SQL exception is occurring for ...READ MORE

Sep 10 in Database by Kithuzzz
• 11,640 points
19 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 in Database by narikkadan
• 19,680 points
38 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,780 points
455 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,055 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 in Java by Kichu
• 19,040 points
2,602 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 in Database by Kithuzzz
• 11,640 points
92 views
0 votes
0 answers
webinar REGISTER FOR FREE WEBINAR X
Send OTP
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP