Estou desenvolvendo uma aplicação em um treinamento porém mesmo seguindo os passos meu código retorna uma exceção que havia atribuído caso ocorresse.
Sei onde está o erro porém não consegui entender como corrigi-lo.
classe modelo
package model;
import java.util.Date;
public class Project {
private int idProject;
private String name;
private String description;
private Date createdAt;
private Date updatedAt;
public Project(int idProject, String name, String description, Date createdAt, Date updatedAt) {
this.idProject = idProject;
this.name = name;
this.description = description;
this.createdAt = createdAt;
this.updatedAt = updatedAt;
}
//Construtor para setar data inicial ao createdAt
public Project(){
this.createdAt = new Date();
this.updatedAt = new Date();
}
public int getIdProject() {
return idProject;
}
public void setIdProject(int idProject) {
this.idProject = idProject;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public Date getCreatedAt() {
return createdAt;
}
public void setCreatedAt(Date createdAt) {
this.createdAt = createdAt;
}
public Date getUpdatedAt() {
return updatedAt;
}
public void setUpdatedAt(Date updatedAt) {
this.updatedAt = updatedAt;
}
@Override
public String toString() {
return "Project{" + "idProject=" + idProject + ", name=" + name + ", "
+ "description=" + description + ",createdAt=" + createdAt + ","
+ " updatedAt=" + updatedAt + '}';
}
}
classe de controle
package controller;
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.List;
import model.Project;
import util.ConnectionFactory;
public class ProjectController {
public void save(Project project){
String sql = "INSERT INTO project("
+ "name,"
+ "description,"
+ "createdAt,"
+ "updatedAt) VALUES (?,?,?,?,?)";
Connection connection = null;
PreparedStatement statement = null;
try{
//inicia a conexão
connection = ConnectionFactory.getConnection();
//prepara a query
statement = connection.prepareStatement(sql);
//insere valores ao atributos
statement.setString(1, project.getName());
statement.setString(2, project.getDescription());
statement.setDate(3,new Date(project.getCreatedAt().getTime()));
statement.setDate(4,new Date(project.getUpdatedAt().getTime()));
//Executa a query
statement.execute();
}catch(SQLException ex){
throw new RuntimeException("Erro ao salvar o projeto",ex);
}finally{
ConnectionFactory.closeConnection(connection, statement);
}
}
public void update(Project project){
String sql = "UPDATE project SET"
+ "name = ?,"
+ "description = ?,"
+ "createdAt = ?,"
+ "updatedAt = ?,"
+ "WHERE idProject = ?";
Connection connection = null;
PreparedStatement statement = null;
try{
connection = ConnectionFactory.getConnection();
statement = connection.prepareStatement(sql);
statement.setString(1,project.getName());
statement.setString(2, project.getDescription());
statement.setDate(4, new Date(project.getCreatedAt().getTime()));
statement.setDate(5, new Date(project.getUpdatedAt().getTime()));
statement.execute();
}catch(SQLException ex){
throw new RuntimeException("Erro ao alterar dados do projeto",ex);
}finally{
ConnectionFactory.closeConnection(connection, statement);
}
}
public void removeById( int idProject){
String sql = "REMOVE FROM project WHERE idProject = ?";
Connection connection = null;
PreparedStatement statement = null;
try{
connection = ConnectionFactory.getConnection();
statement = connection.prepareStatement(sql);
statement.setInt(1, idProject);
statement.execute();
}catch(SQLException ex){
throw new RuntimeException("Erro ao deletar o projeto",ex);
}finally{
ConnectionFactory.closeConnection(connection, statement);
}
}
public List<Project> getAll(){
String sql = "SELECT * FROM projects";
List<Project> projects = new ArrayList<>();
Connection connection = null;
PreparedStatement statement = null;
ResultSet resultSet = null;
try{
connection = ConnectionFactory.getConnection();
statement = connection.prepareStatement(sql);
resultSet = statement.executeQuery();
while(resultSet.next()){
Project project = new Project();
project.setIdProject(resultSet.getInt("idProject"));
project.setName(resultSet.getString("name"));
project.setDescription(resultSet.getString("description"));
project.setCreatedAt(resultSet.getDate("createdAt"));
project.setUpdatedAt(resultSet.getDate("updatedAt"));
projects.add(project);
}
}catch(SQLException ex){
throw new RuntimeException("Erro mostrar lista de projetos",ex);
}finally{
ConnectionFactory.closeConnection(connection, statement, resultSet);
}
return projects;
}
}
classe Main para teste
package TodoApp;
import controller.ProjectController;
import model.Project;
public class Main {
public static void main(String[] args) {
// TODO code application logic here
ProjectController projectController = new ProjectController();
Project project = new Project();
project.setName("Teste de controller 1");
project.setDescription("Teste para verificar validade do controller");
projectController.save(project);
}
}
Erro que retorna
> Task :run FAILED
Exception in thread "main" java.lang.RuntimeException: Erro ao salvar o projeto
at controller.ProjectController.save(ProjectController.java:38)
at TodoApp.Main.main(Main.java:16)
Caused by: java.sql.SQLException: No value specified for parameter 5
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1075)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:989)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:984)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:929)
at com.mysql.jdbc.PreparedStatement.checkAllParametersSet(PreparedStatement.java:2560)
at com.mysql.jdbc.PreparedStatement.fillSendPacket(PreparedStatement.java:2536)
at com.mysql.jdbc.PreparedStatement.fillSendPacket(PreparedStatement.java:2462)
at com.mysql.jdbc.PreparedStatement.execute(PreparedStatement.java:1300)
at controller.ProjectController.save(ProjectController.java:35)
... 1 more