[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();

Facing issues? Have Questions? Post them here! I am happy to answer!

Author Info:

Rakesh (He/Him) has over 14+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.

Follow him on: X

You can also reach out to him via e-mail: rakesh@code2care.org



















Copyright © Code2care 2024 | Privacy Policy | About Us | Contact Us | Sitemap