Tomcat:服务器遇到内部错误,导致无法完成此请求

时间:2015-01-26 23:41:47

标签: java eclipse tomcat java-ee

好吧,当我尝试将数据保存在数据库中时,我的Java Aplicattion正在回复我,但为什么呢?根据完整的错误,AdicionaContatoServlet的第52行返回null,但是如何呢?变量是正确的,名称是正确的,我不知道还能做什么。请帮我...代码:

    package br.com.caelum.servlet;

    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.SQLException;

    public class ConnectionFactory{
        public Connection getConnection () throws ClassNotFoundException{
            try{
                Class.forName("com.mysql.jdbc.Driver");
                return DriverManager.getConnection(
                        "jdbc:mysql://localhost/agenda", "gabriel","******");
            } catch(SQLException e){
                throw new RuntimeException(e);
            }
        }
    }

//"jdbc:mysql://localhost/agenda", "gabriel","***********“);

package br.com.caelum.servlet;

import java.util.Calendar;

public class Contato {
    private Long id;
    private String nome;
    private String email;
    private String endereco;
    private Calendar dataNascimento;

    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }
    public String getNome() {
        return nome;
    }
    public void setNome(String nome) {
        this.nome = nome;
    }
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
    public String getEndereco() {
        return endereco;
    }
    public void setEndereco(String endereco) {
        this.endereco = endereco;
    }
    public Calendar getDataNascimento() {
        return dataNascimento;
    }
    public void setDataNascimento(Calendar dataNascimento) {
        this.dataNascimento = dataNascimento;
    }
}

package br.com.caelum.servlet;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import br.com.caelum.servlet.ConnectionFactory;
import br.com.caelum.servlet.Contato;

public class ContatoDao {

    private Connection connection;

    public ContatoDao() throws ClassNotFoundException{
        this.connection = new ConnectionFactory().getConnection();
    }

    public void adiciona(Contato contato){
        String sql = "insert into contatos" +
                    "(nome, email, endereco, dataNascimento)"+
                    "values (?,?,?,?)";

        try{
            PreparedStatement stmt = connection.prepareStatement(sql);

            stmt.setString(1,contato.getNome());
            stmt.setString(2,contato.getEmail());
            stmt.setString(3,contato.getEndereco());
            stmt.setDate(4, new Date(
                    contato.getDataNascimento().getTimeInMillis()));

            stmt.execute();
            stmt.close();
        } catch(SQLException e){
            throw new RuntimeException(e);
        }
    }

    public void altera(Contato contato){
        String sql = "update contatos set nome=?, email=?,"+
                    "endereco=?, dataNascimento=? where id=?";

        try{
            PreparedStatement stmt = connection.prepareStatement(sql);
            stmt.setString(1, contato.getNome());
            stmt.setString(2, contato.getEmail());
            stmt.setString(3, contato.getEndereco());
            stmt.setDate(4, new Date(contato.getDataNascimento().
                            getTimeInMillis()));
            stmt.setLong(5, contato.getId());
            stmt.execute();
            stmt.close();
        } catch(SQLException e){
            throw new RuntimeException(e);
        }
    }
    public void remove(Contato contato){
        String sql = "delete from contatos where id=?";

        try{
            PreparedStatement stmt = connection.prepareStatement(sql);

            stmt.setLong(1, contato.getId());
            stmt.execute();
            stmt.close();
        }catch(SQLException e){
            throw new RuntimeException(e);
        }
    }

    public List <Contato> getLista(){
        try{
            List<Contato> contatos = new ArrayList<Contato>();
            PreparedStatement stmt = this.connection.
                    prepareStatement("select * from contatos");
            ResultSet rs = stmt.executeQuery();

            while(rs.next()){
                Contato contato = new Contato();
                contato.setId(rs.getLong("id"));
                contato.setNome(rs.getString("nome"));
                contato.setEmail(rs.getString("email"));
                contato.setEndereco(rs.getString("endereco"));

                Calendar data = Calendar.getInstance();
                data.setTime(rs.getDate("dataNascimento"));
                contato.setDataNascimento(data);

                contatos.add(contato);
            }
            rs.close();
            stmt.close();
            return contatos;
        } catch(SQLException e){
            throw new RuntimeException(e);
        }
    }
}

package br.com.caelum.servlet;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletException;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Calendar;
import java.util.Date;
import java.text.SimpleDateFormat;
import java.text.ParseException;

@WebServlet("/adicionaContato")
public class AdicionaContatoServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    protected void service (HttpServletRequest request,
                            HttpServletResponse response)
                            throws IOException, ServletException{

        PrintWriter out = response.getWriter();

        String nome = request.getParameter("nome");
        String email = request.getParameter("email");
        String endereco = request.getParameter("endereco");
        String dataEmTexto = request.getParameter("dataNascimento");
        Calendar dataNascimento = null;

        try {
            Date date = new SimpleDateFormat("dd/MM/yyyy").parse(dataEmTexto);
            dataNascimento = Calendar.getInstance();
            dataNascimento.setTime(date);
        } catch (ParseException e){
            out.println("Erro de conversão de data");
            return;
        }

        Contato contato = new Contato();
        contato.setNome(nome);
        contato.setEmail(email);
        contato.setEndereco(endereco);
        contato.setDataNascimento(dataNascimento);

        ContatoDao dao = null;
        try{
            dao = new ContatoDao();
        }catch(ClassNotFoundException e){
            e.printStackTrace();
        }
        dao.adiciona(contato);

        out.println("<html>");
        out.println("<body>");
        out.println("Contato" + contato.getNome() + "adicionado com sucesso!");
        out.println("</body>");
        out.println("</html>");
    }
}

和tomcat的错误:

显示java.lang.NullPointerException     br.com.caelum.servlet.AdicionaContatoServlet.service(AdicionaContatoServlet.java:52)     javax.servlet.http.HttpServlet.service(HttpServlet.java:725)     org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

0 个答案:

没有答案