Hi, I'm learning to do a CRUD with java and jsp servlets using MVC. I have a table where all the elements of a database table are listed. For each of them, there is a link that says UPDATE. The idea is that when pressing that button, the JSP where the link is located sends the price of the selected item (which is a primary field) to the Controller and this sends it to the Model who finally uses a PreparedStatement with a SELECT * WHERE statement (price) ... where it should locate in the DB the only item that corresponds to that price. However, it throws me the following error java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax; check the manual for the right syntax to use near 'VALUES (2.0)' / 2.0 refers to the price I chose to update. And the block inside the Catch is executed.
Model method that gives an error: (important: this method receives a float. There is no error here. The float it receives from the controller method is correct. In addition, the price field of the database is EFFECTIVELY a float).
public Articulos actualizar(float precio) {
Connection miconexion;
PreparedStatement mistatement ;
ResultSet miresultset;
String descripcion = null;
int codigo = 0;
try {
miconexion = pool.getConnection(); //pool es una variable DataSource
String sql = "SELECT * FROM articulos WHERE (precio) VALUES (?)";
mistatement = miconexion.prepareStatement(sql);
mistatement.setFloat(1, precio);
miresultset = mistatement.executeQuery();
if(miresultset.next()) {
codigo = miresultset.getInt("codigo");
descripcion = miresultset.getString("descripcion");
}
Articulos objetoactualizable = new Articulos(descripcion, codigo);
return objetoactualizable;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("algo falló");
Articulos falla = new Articulos("lala",344);
return falla;
}
Method that receives the price and links with the DB of the Model class
Your SQL query is wrong, VALUES only makes sense on inserts. The error tells you that you have a syntax error in the SQL, not the Java code: