Sorry if this is a standard problem. But I'm stumped.
I'm learning to program with the MySQL Connector for Java (5.1.36). While learning I stumbled upon a MySQLSyntaxError Exception. And, as beginner, I can't find the reason.
public void create(Project project) throws IllegalArgumentException, DAOException {
if (project.getProjectId() != 0) {
throw new IllegalArgumentException("Project is already created, the project ID is not null.");
}
Object[] values = { project.getProjectNumber(), project.getProjectName(),
project.getActivity(), project.getPerNumber(),
toSqlDate(project.getDate()), project.getTime()};
try (Connection connection = daoFactory.getConnection();
PreparedStatement statement = prepareStatement(connection,
SQL_INSERT_PROJECT, true, values);) {
int affectedRows = statement.executeUpdate();
if (affectedRows == 0) {
throw new DAOException("Creating user failed, no rows affected.");
}
try (ResultSet generatedKeys = statement.getGeneratedKeys()) {
if (generatedKeys.next()) {
project.setProjectId(generatedKeys.getLong(1));
} else {
throw new DAOException("Creating user failed, no generated key obtained.");
}
}
} catch (SQLException e) {
throw new DAOException(e);
}
}
SQL_INSERT_PROJECT is
private static final String SQL_INSERT_PROJECT =
"INSERT INTO projects (projectnumber, project, activity, per_number, date, time "
+ "VALUES (?, ?, ?, ?, ?, ?)";
And finally, toSqlDate is
public static Date toSqlDate(java.util.Date date) {
return (date != null) ? new Date(date.getTime()) : null;
}
The error I got is:
class nl.pruttel.tk.dao.DAOException - DAOException constructor, com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax;
check the manual that corresponds to your MySQL server version for the right syntax to use near
'VALUES ('p10001', 'TimeKeeper', 'DAOTest', '100101', '2015-08-05', '05:30:00')' at line 1
Exception in thread "main" java.lang.NullPointerException
at nl.pruttel.tk.dao.DAOException.logState(DAOException.java:76)
at nl.pruttel.tk.dao.DAOException.<init>(DAOException.java:51)
at nl.pruttel.tk.dao.ProjectDAOImplementation.create(ProjectDAOImplementation.java:210)
at nl.pruttel.tk.dao.DAOTest.main(DAOTest.java:68)