estou estudando e lendo uma postila de java WEB, o meu exemplo usando JDBC e um exemplo simples de servlet funcionou normal,
mas agora que estou na parte de cadastrar as informações digitadas em um form no banco usando uma servlet esta me retornando esse erro.
message java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost/olivro
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/olivro
br.com.livro.dao.ConnectionFactory.getConnection(ConnectionFactory.java:13)
br.com.livro.dao.LivroDAO.<init>(LivroDAO.java:13)
br.com.livro.servlet.AdicionaContatoServlet.service(AdicionaContatoServlet.java:39)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
root cause
java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost/olivro
java.sql.DriverManager.getConnection(Unknown Source)
java.sql.DriverManager.getConnection(Unknown Source)
br.com.livro.dao.ConnectionFactory.getConnection(ConnectionFactory.java:10)
br.com.livro.dao.LivroDAO.<init>(LivroDAO.java:13)
br.com.livro.servlet.AdicionaContatoServlet.service(AdicionaContatoServlet.java:39)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
note The full stack trace of the root cause is available in the Apache Tomcat/8.0.26 logs.
public class ConnectionFactory {
public Connection getConnection() {
try {
return DriverManager.getConnection("jdbc:mysql://localhost/olivro",
"root", "root");
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
public class LivroDAO {
private Connection connection;
public LivroDAO() {
this.connection = new ConnectionFactory().getConnection();
}
public void adiciona(Livro livro) {
String sql = "insert into livro" + "(titulo, autor, editora)"
+ "values (?,?,?)";
try {
// estancia o statment
PreparedStatement stmt = connection.prepareStatement(sql);
// pega os valores
stmt.setString(1, livro.getTitulo());
stmt.setString(2, livro.getAutor());
stmt.setString(3, livro.getEditora());
// executa
stmt.execute();
stmt.close();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}