Problema com ResultSet

O código abaixo quando eu insiro, ou atualizo no banco tenho q chamar o metodo executeQuery() novamente para quando clicar nos botoes jb_ant e jb_prox não dar o seguinte erro “ResultSet is closed”. alguem sabe pq isto está acontecendo.

E o Statement é o seguinte:


stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);

Alguem poderia me explicar a difereça entre ResultSet.TYPE_SCROLL_SENSITIVE e ResultSet.TYPE_SCROLL_INSENSITIVE


package funcional;

import java.sql.*;
import janela.janela_cadastrar_nf.*;
import java.awt.event.*;
import javax.swing.*;
import conexao.*;

public class Funcional extends JanelaCadastrarNF{
		
	private String tabela, col_1, col_2;
	private Connection con;
	private Statement stmt;
	private ResultSet rs;
	private int n = 1;
				
	public Funcional(String titulo, String label, String tabela, String col_1, String col_2) {
		
		super(titulo, label);
		
		this.tabela = tabela;
		this.col_1 = col_1;
		this.col_2 = col_2;
								
		jb_cadastrar.addActionListener(new HandlerFuncional());
		jb_alterar.addActionListener(new HandlerFuncional());
		jb_excluir.addActionListener(new HandlerFuncional());
		jb_prox.addActionListener(new HandlerFuncional());
		jb_ant.addActionListener(new HandlerFuncional());
	  jb_sair.addActionListener(new HandlerFuncional());
										
		conexao();				
	}	
	
	private void conexao(){
		
		try{
			Conexao conex = new Conexao();
			con = conex.getConexao();
			stmt = conex.getStatement();
			rs = stmt.executeQuery("SELECT " + col_1 + ", " + col_2 + " FROM " + tabela);								
		}
		catch(Exception sqlex){
			System.out.println("rn");
		}		
	}
	
	private class HandlerFuncional implements ActionListener{
		
		public void actionPerformed(ActionEvent ae){
			
			int s;
									
			if(ae.getSource() == jb_cadastrar){
				String jtf = jtf_1.getText();
				if(!jtf.equals(""))
					try{
						s = stmt.executeUpdate("INSERT INTO " + tabela + " (" + col_2 + ") VALUES ('" + jtf + "')");
						rs = stmt.executeQuery("SELECT " + col_1 + ", " + col_2 + " FROM " + tabela);
						jtf_1.setText("");
						jtf_1.requestFocus();						
					}
					catch(Exception sqlex){
						System.out.println("erroooooooooooooooor");
						System.out.println(sqlex.getMessage());
					}					
				else{
					JOptionPane.showMessageDialog(null, "erro", "erro", JOptionPane.ERROR_MESSAGE);			
					jtf_1.requestFocus();					
				}
			}
					
			else if(ae.getSource() == jb_alterar){
				try{
						System.out.println("UPDATE " + tabela + " SET " + col_2 + "='" + jtf_1.getText() + "'");
						s = stmt.executeUpdate("UPDATE " + tabela + " SET " + col_2 + "='" + jtf_1.getText() + "' WHERE " +
																		col_1 + "=" + rs.getString(1));
						//rs = stmt.executeQuery("SELECT " + col_1 + ", " + col_2 + " FROM " + tabela);
						jtf_1.requestFocus();						
					}
					catch(Exception sqlex){
						System.out.println("errr");
						System.out.println(sqlex.getMessage());
					}
			}
			else if(ae.getSource() == jb_excluir){
				
			}
			else if(ae.getSource() == jb_prox){
				try{					
					if(rs.next())						
						jtf_1.setText(rs.getString(2));				
					else{
						rs.last();
						jtf_1.setText(rs.getString(2));																							
					}							
				}
				catch(Exception sqlex){
					System.out.println("er");
					System.out.println(sqlex.getMessage());					
				}					 
			}
			else if(ae.getSource() == jb_ant){
				try{
					if(rs.previous())
						jtf_1.setText(rs.getString(2));				
					else{
						rs.first();
						jtf_1.setText(rs.getString(2));					
					}
				}
				catch(Exception sqlex){
						System.out.println("erroor");
						System.out.println(sqlex.getMessage());						
				}
			}
			else if(ae.getSource() == jb_sair){
				try{
					System.out.println("maria clara");
					rs.close();
					stmt.close();
					con.close();			
					dispose();
				}
				catch(SQLException sqle){System.out.println("sp");}				
			}
			
		}
		
	}

}

Que meleca esse codigo hein?!..

A explicacao para os lances do ResultSet voce consegue na documentacao… Voce ja tinha olhado la? :wink:

http://java.sun.com/j2se/1.4.2/docs/api/java/sql/ResultSet.html

Rafael

eu ja fui la, mas não entendi direito, só consegui fazer esse código meleca.

valeus.

Bem, o result set is closed quando ele está fechado … hehe

admito não ter olhado o código, então certifique-se de que, antes de qualquer operação no banco, você inicializou os objetos necessários.

E quanto à outra dúvida:

TYPE_SCROLL_INSENSITIVE
The constant indicating the type for a ResultSet object that is scrollable but generally not sensitive to changes made by others.

TYPE_SCROLL_SENSITIVE
The constant indicating the type for a ResultSet object that is scrollable and generally sensitive to changes made by others.

“In a scrollable ResultSet object, the cursor can be moved backwards and forwards, to an absolute position, or to a position relative to the current row”

No primeiro você brinca, no segundo só vai pra frente :slight_smile:

Tah meio confusa esse tua duvida…
O codigo entao…

Dá uma simplificada nesse codigo, cria uma classe só pra acesso a banco e desacopla isso da interface…
usa um debug e marca ali no rs.close();

falow