Variável de sessão em uma classe

1 resposta
E

Pessoal,

Tenho algumas variaveis de sessão instanciadas em uma servlet.
Uma classe precisa ler essas variaveis de sessão.

Fiz assim: (não deu certo)

package model;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

public class FiltroProjetos {
	private ConexaoMysql conexaoMysql;
	
	public String FiltroProjetos2(String status){
		
		conexaoMysql = new ConexaoMysql(); 
	    Statement state = null;   
	    ResultSet rs = null; 
	       
	    String nome = "";
	    
	    System.out.println(session.getAttribute("perfil")); // tento exibir a variavel de sessao para saber se ela foi pega corretamente
	          		
	    try {  
	     	state = conexaoMysql.getConnection().createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);   
	        rs = state.executeQuery("Select nome from projeto where statusProjeto = '" + status + "'");
	               
            while (rs.next()) {
            	nome += rs.getString("nome") + "#";
	            //System.out.println("Nome:" + nome);
            }
	    } catch(Exception erro) {   
	        System.out.println("Erro ocorrido na classe FiltroProjetos: n" + erro);   
	    } finally {   
	    	if(state != null){   //fechar os statments
				try {
					state.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}  
	    	}
		}
		return nome;
	}
}

Ai tentei fazer assim:

package model;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

public class FiltroProjetos {
	private ConexaoMysql conexaoMysql;
	
	public void Filtro (HttpServletRequest request){
        HttpSession session = request.getSession(); 
        //String matricula = (String)session.getAttribute("login"); 
        //session.getAttribute("perfil");
        System.out.println("Esse é o meu perfil: " + session.getAttribute("perfil"));
    	//String matricula = (String)session.getAttribute("login");
		
	}
	
	public String FiltroProjetos2(String status){
		
		conexaoMysql = new ConexaoMysql(); 
	    Statement state = null;   
	    ResultSet rs = null; 
	       
	    String nome = "";
	    
	    Filtro(); // Aki tem que passar um valor mas não sei ql valor eu tenho q passar para dar certo.
	          		
	    try {  
	     	state = conexaoMysql.getConnection().createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);   
	        rs = state.executeQuery("Select nome from projeto where statusProjeto = '" + status + "'");
	               
            while (rs.next()) {
            	nome += rs.getString("nome") + "#";
	            //System.out.println("Nome:" + nome);
            }
	    } catch(Exception erro) {   
	        System.out.println("Erro ocorrido na classe FiltroProjetos: n" + erro);   
	    } finally {   
	    	if(state != null){   //fechar os statments
				try {
					state.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}  
	    	}
		}
		return nome;
	}
}

Qual valor eu tenho que passar para isso dê certo? Tem outra solução?

???

1 Resposta

edmarr

ao invez disto

Statement state = null;

use isto

PreparedStatement pstmt = null;

entao faça assim

String sql = "Select nome from projeto where statusProjeto =  ?";
             pstmt = con.prepareStatement(sql);
             
             pstmt.setString(1, status);

      
             rs = pstmt .executeQuery();
Criado 28 de outubro de 2009
Ultima resposta 28 de out. de 2009
Respostas 1
Participantes 2