[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.

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

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

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

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

Author Info:

Rakesh is a seasoned developer with over 10 years of experience in web and app development, and a deep knowledge of operating systems. Author of insightful How-To articles for Code2care.

Follow him on: X

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