0
I have an html uploading request
for a servlet
which answers with error but when I have a class rotated testaAdicionaContato
.
mysql 5 table:
create table contatos (
id BIGINT NOT NULL AUTO_INCREMENT,
nome VARCHAR(255),
email VARCHAR(255),
endereco VARCHAR(255),
dataNascimento DATE,
primary key (id)
);
class Testaaddicontact:
package br.com.caelum.servlet;
import java.util.Calendar;
public class TestaAdicionaContato {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Contato contato = new Contato();
contato.setNome("nome");
contato.setEndereco("endereco");
contato.setEmail("email");
contato.setDataNascimento(Calendar.getInstance());
ContatoDAO dao = new ContatoDAO();
dao.adiciona(contato);
}
}
Servlet Add contact call when I fill out the form in localhost:8080/agenda/add-contact-html
package br.com.caelum.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/adicionaContato")
@SuppressWarnings("serial")
public class AdicionaContato extends HttpServlet {
protected void service(HttpServletRequest request,
HttpServletResponse response) throws IOException, ServletException {
// busca o writer
PrintWriter out = response.getWriter();
// buscando os parâmetros no request
String nome = request.getParameter("nome");
String endereco = request.getParameter("endereco");
String email = request.getParameter("email");
String dataEmTexto = request.getParameter("dataNascimento");
Calendar dataNascimento = null;
// fazendo a conversão da data
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 da data");
return; // para a execução do método
}
// monta um objeto contato
Contato contato = new Contato();
contato.setNome(nome);
contato.setEndereco(endereco);
contato.setEmail(email);
contato.setDataNascimento(dataNascimento);
// salva o contato
ContatoDAO dao = new ContatoDAO();
dao.adiciona(contato);
// imprime o nome do contato que foi adicionado
out.println("<html>");
out.println("<body>");
out.println("Contato " + contato.getNome() + " adicionado com sucesso");
out.println("</body>");
out.println("</html>");
}
}
HTTP Status 500 -
type Exception report
message
Description The server encountered an Internal error that prevented it from fulfilling this request.
Exception
java.lang.Runtimeexception br.com.Caelum.servlet.Connectionfactory.getConnection(Connectionfactory.java:13) br.com.Caelum.servlet.Contactodao.(Contactodao.java:24) br.com.Caelum.servlet.Add contact.service(Add contact.java:49) javax.servlet.http.HttpServlet.service(Httpservlet.java:728) note The full stack trace of the root cause is available in the Apache Tomcat/7.0.41 logs.
Can you help me?
Its exception is in the Connectionfactory.java getConnection method. Most likely you are not using a connected driver, or are using it incorrectly (without including the jar in the classpath).
– cantoni
I managed to fix it. Thanks for your help!
– Diego Pereira