user3013767
user3013767

Reputation: 137

SQLException: Operation not allowed after ResultSet closed error

Im getting this error and I don´t know why: SQLException: Operation not allowed after ResultSet closed . The error appears at line while(rs1.next()){

private void tablaPeliculasMouseClicked(java.awt.event.MouseEvent evt) {                                            
    int codigo=Integer.parseInt(tablaPeliculas.getValueAt(tablaPeliculas.getSelectedRow(),0).toString());
    System.out.println("codigo: "+codigo);
    String nombre="";

    ResultSet rs1=DBVideoteca.consultaPortada(codigo);
    try {
        while(rs1.next()){
            nombre=rs1.getObject("imagen").toString();
        }
    } catch (SQLException ex) {
        Logger.getLogger(Videoteca.class.getName()).log(Level.SEVERE, null, ex);
    }

and consultaPortada method:

static ResultSet consultaPortada(int cod){
 conn=enlace(conn);
    try{
        stt=conn.prepareStatement("select * from peliculas where codigo=?");
        stt.setInt(1, cod);
    }
    catch (SQLException ex)
   {
    System.out.println("excepcion del try 1º del metodo consulta portada");
   }
   try 
   {
    stt.executeQuery();
   }
   catch (SQLException ex)
   {
    System.out.println("error en la query " + ex);
   }

   //cerrarConexion();
   return rs;
}

Upvotes: 0

Views: 153

Answers (5)

Bhavik Shah
Bhavik Shah

Reputation: 5183

I have many concerns over your code First the answer to your question is that you are not assigning values to rs So, change

stt.executeQuery();

To:

rs = stt.executeQuery();

Secondly, your function is badly written

static ResultSet consultaPortada(int cod){
 conn=enlace(conn);
    try{
        stt=conn.prepareStatement("select * from peliculas where codigo=?");
        stt.setInt(1, cod);
    }
    catch (SQLException ex)
   {
    System.out.println("excepcion del try 1º del metodo consulta portada");//caught an exception here
   }
   try 
   {
    stt.executeQuery(); // this should not be executed if there is an exception in above try block
   }
   catch (SQLException ex)
   {
    System.out.println("error en la query " + ex);
   }

   //cerrarConexion();
   return rs;
}

you stt.executeQuery(); statement should be also in previous block something like this

 try{
stt=conn.prepareStatement("select * from peliculas where codigo=?");
stt.setInt(1, cod);
stt.executeQuery();
}
catch (SQLException ex)
{
System.out.println("excepcion del try 1º del metodo consulta portada");
}

Third, Always close connection in finally block

Upvotes: 1

Thirumalai Parthasarathi
Thirumalai Parthasarathi

Reputation: 4671

it seems before calling consultaPortada() you had previously assigned some value to rs and you had closed that ResultSet object. and since it is a closed resultset it is throwing this error.

assuming you are trying to assign stt.executeQuery(); to the result set try this

 rs = stt.executeQuery();

Upvotes: 0

dev2d
dev2d

Reputation: 4262

you can simply check if !rs.isClosed() in while loop condition itself

Upvotes: 0

subash
subash

Reputation: 3140

try this..

    try{
       rs = stt.executeQuery();
   }
   catch (SQLException ex) {
    System.out.println("error en la query " + ex);
   }

Upvotes: 0

Grambot
Grambot

Reputation: 4514

From consultaPortada you return rs which is never actually assigned.

Change this line:

stt.executeQuery();

To:

rs = stt.executeQuery();

And your issue should go away.

Upvotes: 1

Related Questions