Help Save Code2care! 😢

I've lost 99% of my revenue to AdBlockers & AI. Your support could be the lifeline that keeps this passion project alive!

Buy Code2care a Coffee QR Code

Scan to Buy Me A Coffee and help me continue coding for you!

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

You can download this article in various formats for your convenience. Choose from the options below:

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

Author Info:

Rakesh (He/Him) has a Masters Degree in Computer Science with over 15+ 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 | Search