I'm having problems with Tomcat using Eclipse. First it did not find the MySQL Driver, then I used Class.forName
in class ConnectionFactory
and now it is returning line 52 of AdicionaContatoServlet
as null
.
Can anyone help me? Here it is:
ConnectionFactory.java
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","***********");
Contact.java
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;
}
}
Contact your.java
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);
}
}
}
AddContactServlet.java
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); //linha 52
out.println("<html>");
out.println("<body>");
out.println("Contato" + contato.getNome() + "adicionado com sucesso!");
out.println("</body>");
out.println("</html>");
}
}
and the Tomcat8 error:
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)