estou tentando persistir mais não estou conseguindo.
package br.com.triadworks.todoList.infra;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class ConnectionFactory {
private static Connection connection;
public static Connection getConnection() {
try {
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection("jdbc:mysql://localhost/todoList", "root", "root");
System.out.println("conectou");
} catch (SQLException e) {
e.printStackTrace();
}
return connection;
}
}
package br.com.triadworks.todoList.dao;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import br.com.triadworks.todoList.infra.ConnectionFactory;
import br.com.triadworks.todoList.model.Tarefa;
public class TarefaDAO {
private Connection connection;
public TarefaDAO() {
connection = ConnectionFactory.getConnection();
}
public void adiciona(Tarefa tarefa) {
String sql = "insert into tarefa(descricao,data) values(?,?)";
try (PreparedStatement prepareStatement = connection.prepareStatement(sql)) {
prepareStatement.setString(1, tarefa.getDescricao());
prepareStatement.setDate(2,new Date(tarefa.getData().getTimeInMillis()));
System.out.println("Salvou");
} catch (Exception e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
}
}
package br.com.triadworks.todoList.infra;
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;
import br.com.triadworks.todoList.dao.TarefaDAO;
import br.com.triadworks.todoList.model.Tarefa;
@WebServlet("/adicionarTarefa")
public class OiMundo extends HttpServlet {
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = response.getWriter();
String data = request.getParameter("data");
String descricao = request.getParameter("descricao");
Calendar dataTarefa = Calendar.getInstance();
try {
Date dataconvertida = new SimpleDateFormat("dd/MM/yyyy").parse(data);
dataTarefa.setTime(dataconvertida);
} catch (ParseException e) {
System.out.println(e.getMessage());
e.printStackTrace();
return;
}
Tarefa tarefa = new Tarefa();
tarefa.setDescricao(descricao);
tarefa.setData(dataTarefa);
TarefaDAO dao = new TarefaDAO();
dao.adiciona(tarefa);
out.println("<html>");
out.println("<body>");
out.println("Descrição: " + tarefa.getDescricao() +" adicionado com sucesso");
out.println("</body>");
out.println("</html>");
}
}
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Adicionar Tarefa</title>
</head>
<body>
<header>
<img alt="logomarca" src="imagens/logotipo.jpeg" height="250" width="700">
</header>
<nav>Home | Cadastrar | Consultar</nav>
<article>
<fieldset>
<legend>Cadastro de Tarefa</legend>
<form action="adicionarTarefa">
Data <input type="date" name="data"> <br/>
Descrição<textarea rows="4" cols="10" name="descricao"></textarea> <br />
<input type="submit" value="Gravar">
</form>
</fieldset>
</article>
</body>
</html>
Ao Adicionar, estou com esse erro.
OBS: Já adicionei o .jar no ClassPath
java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost/todoList
at java.sql.DriverManager.getConnection(DriverManager.java:689)
at java.sql.DriverManager.getConnection(DriverManager.java:247)
at br.com.triadworks.todoList.infra.ConnectionFactory.getConnection(ConnectionFactory.java:13)
at br.com.triadworks.todoList.dao.TarefaDAO.<init>(TarefaDAO.java:15)
at br.com.triadworks.todoList.infra.OiMundo.service(OiMundo.java:45)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:475)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:80)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:651)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:498)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:796)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1374)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:748)
java.lang.NullPointerException
at br.com.triadworks.todoList.dao.TarefaDAO.adiciona(TarefaDAO.java:3)
at br.com.triadworks.todoList.infra.OiMundo.service(OiMundo.java:46)
null
at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:475)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:80)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:651)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:498)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:796)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1374)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:748)