Problemas com Conexao MySQL

1 resposta
B
Bem, o problema que estou tendo é o seguinte.. tenho esse codigo:
public static int getId (String nome) {
        int aux = 0;
        Connection conexao = Conexao.getConnection ();
        try {
            String sql = "SELECT tag_id from ct_tag where nome = '" + nome+"'";

            
            PreparedStatement psta = conexao.prepareStatement (sql);
            
            ResultSet result = psta.executeQuery ();
            while (result.next ()) {
                aux = result.getInt (1);
                System.out.println (":::::" +aux+ "::::");
            }
            
            
        } catch (SQLException e) {
            System.out.println ("Erro: " + e.getMessage ());
        }
        return aux;
    }
Ele é chamado a partir dessa linha.. int aux3 = TagDAO.getId("Windows"); Era para ele buscar no banco de dados e retornar 1 mais ele da NullPointerException na linha: PreparedStatement psta = conexao.prepareStatement (sql); E eu nao sei mais o que fazer.. Alguem me da uma luz :~

1 Resposta

ramilani12

Na verdade sua variavel sql ,deveria ficar nessa linha:

Statement psta = conexao.createStatement();
ResultSet result = psta.executeQuery(sql);

ou assim:

PreparedStatement pstmt = conexao.prepareStatement("SELECT tag_id from ct_tag where nome = ?");
pstmt.setString(1, "+nome+");

Esta dando NullPointerException prq preparedStatement espera uma passagem de parametro …

Criado 18 de junho de 2006
Ultima resposta 18 de jun. de 2006
Respostas 1
Participantes 2