Erro ao gravar no banco de dados

2 respostas
P

Essa é minha classe:

import java.sql.*;

public class AgendaDB {
    private String nome = blank;
    private String endereco = blank;
    private String cidade = blank;
    private String telefone = blank;
    private String result_busca = blank;
    private String result_inserir = blank;
    public static final String BUSCA_INVALIDA = "failure";
    public static final String BUSCA_VALIDA = "success";
    public static final String SUCESSO_INSERCAO = "success";
    public static final String FALHA_INSERCAO = "failure";
    static Connection con = null;
    static Statement stm = null;
    static ResultSet rs;
    static private String blank = "";
    public AgendaDB() {
        if (con==null) {
            try {
                Class.forName("org.postgresql.Driver");
                con =
                    DriverManager.getConnection("jdbc:postgresql://localhost:5435/postgres","postgres","eguru");
            } catch (SQLException e) {
                System.err.println ("Erro: "+e);
                con = null;
            } catch (ClassNotFoundException e) {
                System.out.println("ClassNotFound...");
                e.printStackTrace();
            }
        }
    }
    public String getNome() {
        return nome;
    }
    public void setNome(String nome) {
        this.nome = nome;
    }
    public String getCidade() {
        return cidade;
    }
    public void setCidade(String cidade) {
        this.cidade = cidade;
    }
    public String getEndereco() {
        return endereco;
    }
    public void setEndereco(String endereco) {
        this.endereco = endereco;
    }
    public String getTelefone() {
        return telefone;
    }
    public void setTelefone(String telefone) {
        this.telefone = telefone;
    }
    public String inserir() {
        String result_inserir = FALHA_INSERCAO;
        try {
            stm = con.createStatement();
            stm.execute("INSERT INTO teste(nome,endereco,cidade,telefone) VALUES ('" + nome + "','"+
                    endereco + "','" + cidade + "','"+ telefone + "')");
            stm.close();
            result_inserir = SUCESSO_INSERCAO;
        } catch (SQLException e) {
            System.err.println ("Erro: "+e);
            result_inserir = FALHA_INSERCAO;
        }
        return result_inserir;
    }
    public String buscar() throws SQLException {
        String result_busca = BUSCA_INVALIDA;
        try {
            stm = con.createStatement();
            rs = stm.executeQuery("SELECT * FROM teste WHERE nome = '" + nome + "'");
            if (rs.next()) {
                nome = rs.getString(1);
                endereco = rs.getString(2);
                cidade = rs.getString(3);
                telefone = rs.getString(4);
                result_busca = BUSCA_VALIDA;
            }
            else
                result_busca = BUSCA_INVALIDA;
            rs.close();
            stm.close();
        } catch (SQLException e) {
            System.err.println ("Erro: "+e);
        }
        return result_busca;
    }
}
essa é minha JSP que chama os parametros de inserção
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<html>
<body>
<f:view>
<h:form>
<center><h2> Inserção </h2></center>
<br>
&lt;h3&gt;Entre com os dados abaixo&lt;/h3&gt;
&lt;table&gt;
&lt;tr&gt;
&lt;td&gt;Nome:&lt;/td&gt;
&lt;td&gt;
&lt;h:inputText value="#{agenda.nome}"/&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Endereço:&lt;/td&gt;
&lt;td&gt;
&lt;h:inputText value="#{agenda.endereco}"/&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Cidade:&lt;/td&gt;
&lt;td&gt;
&lt;h:inputText value="#{agenda.cidade}"/&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Telefone:&lt;/td&gt;
&lt;td&gt;
&lt;h:inputText value="#{agenda.telefone}"/&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
<p>
&lt;h:commandButton value="Inserir" action="#{agenda.inserir}"/&gt;
</p>
&lt;/h:form&gt;
<br>
&lt;h:outputLink value="index.jsf"&gt;
&lt;f:verbatim&gt;voltar&lt;/f:verbatim&gt;
&lt;/h:outputLink&gt;
&lt;/f:view&gt;
&lt;/body&gt;
&lt;/html&gt;

e esse é o erro que dá, que eu não sei como resolver.

HTTP Status 500 -

type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

javax.servlet.ServletException: java.lang.NullPointerException
	javax.faces.webapp.FacesServlet.service(FacesServlet.java:321)

root cause

javax.faces.el.EvaluationException: java.lang.NullPointerException
	javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:98)
	com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:98)
	javax.faces.component.UICommand.broadcast(UICommand.java:311)
	javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:781)
	javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1246)
	com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:77)
	com.sun.faces.lifecycle.Phase.doPhase(Phase.java:97)
	com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:114)
	javax.faces.webapp.FacesServlet.service(FacesServlet.java:308)

root cause

java.lang.NullPointerException
	AgendaDB.inserir(AgendaDB.java:61)
	sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
	sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
	java.lang.reflect.Method.invoke(Unknown Source)
	org.apache.el.parser.AstValue.invoke(AstValue.java:196)
	org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:276)
	org.apache.jasper.el.JspMethodExpression.invoke(JspMethodExpression.java:68)
	javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:84)
	com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:98)
	javax.faces.component.UICommand.broadcast(UICommand.java:311)
	javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:781)
	javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1246)
	com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:77)
	com.sun.faces.lifecycle.Phase.doPhase(Phase.java:97)
	com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:114)
	javax.faces.webapp.FacesServlet.service(FacesServlet.java:308)

note The full stack trace of the root cause is available in the JBoss Web/3.0.0-CR1 logs.
JBoss Web/3.0.0-CR1

2 Respostas

Z111

AgendaDB estaria mapeado como um ManagedBean?

P

Sim, está no faces-config.xml

Criado 12 de abril de 2011
Ultima resposta 12 de abr. de 2011
Respostas 2
Participantes 2