I had a problem with a PreparedStatement insert.
I have this exception:
java.sql.SQLSyntaxErrorException: 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 '?, ?, ?, ?, ?, ?)' at line 1
I think the problem is with the SQL date but I'm not sure. I have an Event Id, but it is an auto_increment attribute, so I think I don't need to place it in the query.
connect();
try {
String sql = "INSERT INTO agenda(nombre_evento, fecha_evento, hora_inicio, hora_final, lugar, nota)VALUES(?, ?, ?, ?, ?, ?)";
PreparedStatement preparedStatement = con.prepareStatement(sql);
DateFormat parser = new SimpleDateFormat("yyyy-MM-dd");
Date fechaInicio = parser.parse(txtFecha.getText().substring(0, 10));
Date horaInicio = parser.parse(txtHoraInicio.getText().substring(0, 10));
Date horaFinal = parser.parse(txtHoraFinal.getText().substring(0, 10));
java.sql.Date fechaToSql = new java.sql.Date(fechaInicio.getTime());
java.sql.Date horaInicioToSql = new java.sql.Date(horaInicio.getTime());
java.sql.Date horaFinalToSql = new java.sql.Date(horaFinal.getTime());
preparedStatement.setString(1, txtNombre.getText());
preparedStatement.setDate(2, fechaToSql);
preparedStatement.setDate(3, horaInicioToSql);
preparedStatement.setDate(4, horaFinalToSql);
preparedStatement.setString(5, txtLugar.getText());
preparedStatement.setString(6, txtNotas.getText());
preparedStatement.execute(sql);
} catch (SQLException ex) {
System.out.print("NO");
System.out.print(ex);
}
disconnect();