Boa noite!
Tenho um formulário com um cadastro simples de contatos, ao clicar no botão Gravar, estou recebendo a seguinte mensagem:
"HTTP Status 500 - java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost/fj21
type Exception report message java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost/fj21
description The server encountered an internal error that prevented it from fulfilling this request.
exception
java.lang.RuntimeException: java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost/fj21
br.com.caelum.agenda.ConnectionFactory.getConnection(ConnectionFactory.java:12)
br.com.caelum.agenda.DAO.ContatoDAO.<init>(ContatoDAO.java:19)
br.com.caelum.agenda.servlet.AdicionaContatoServlet.service(AdicionaContatoServlet.java:46)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)"
Aparentemente não está sendo possível encontrar uma conexão MySQL ao tentar gravar os dados no banco. Meu driver MySQL está incluso corretamente no projeto, não tenho ideia do que pode ser o problema. Estou usando o petern ConnectionFactory, segue abaixo minha classe:
package br.com.caelum.agenda;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class ConnectionFactory {
public Connection getConnection() {
try {
return DriverManager.getConnection("jdbc:mysql://localhost/fj21", "root", "root");
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
Segue também minha classe DAO:
package br.com.caelum.agenda.DAO;
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.agenda.ConnectionFactory;
import br.com.caelum.agenda.modelo.Contato;
public class ContatoDAO {
private Connection connection;
public ContatoDAO() {
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);
}
}
}
O que estou fazendo de errado?