I am doing an insertion in a mysql database through a jdbc connection, which connects perfectly, that if when I do the PreparedStatement, Tomcat throws me a sql error that says the following:
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
The question is that when I do the query through a statement, it performs the query without any problem, which makes me suspect that the error is actually in the PreparedSatement. Here I leave the code.
@Override
public void save(Planet planet) throws SQLException {
String namePlanet = planet.getName();
float massPlanet = planet.getMass();
boolean habitablePlanet = planet.isHabitable();
sql = "INSERT INTO planeta (nom,massa,habitable) VALUES (?,?,?)";
PreparedStatement preparedStatement = conn.prepareStatement(sql);
preparedStatement.setString(1, namePlanet);
preparedStatement.setFloat(2, massPlanet);
preparedStatement.setBoolean(3, habitablePlanet);
preparedStatement.executeUpdate(sql);
}
I will also leave the code with the preparedstatement, so you can see that this does work.
@Override
public void save(Planet planet) throws SQLException {
String namePlanet = planet.getName();
float massPlanet = planet.getMass();
boolean habitablePlanet = planet.isHabitable();
sql = "insert into planeta(nom,massa,habitable) value('" + namePlanet + "'," + massPlanet + "," + habitablePlanet + ")";
stmt.executeUpdate(sql);
}
Please be patient with me, I'm a student and thanks in advance.
Like @Jens told in the comments i have to call
executeUpdate()without parameters.Thx again @Jens!!
Thats the correct code:
}