[Solution] Java JDBC SQLException: No value specified for parameter 1


Post Banner

If you have used PreparedStatement with parameterized query (with question marks) and forget to set those parameters you will get an SQLException.

Example:
String insertQuery ="insert into employees values(?,?)";
Connection connection = DriverManager.getConnection(url,userName,password);
PreparedStatement preparedStatement = connection.prepareStatement(insertQuery);
preparedStatement.execute();

As you can see the PreparedStatement is executed without setting the query parameters.

Solution
PreparedStatement preparedStatement = connection.prepareStatement(insertQuery);
preparedStatement.setString(1, "Micheal"); //Name
preparedStatement.setString(2, "IT"); //Dept
preparedStatement.execute();
-


Have Questions? Post them here!

Top Hashtags:

Advertisements

Advertisements




Advertisements