Bew
Bew

Reputation: 1

What is "java.sql.SQLException: No value specified for parameter 1 "?

I don't know what is wrong with my code below.

 try {
        String sql = "INSERT INTO  `myporject`.`selectnation` (`nations` ,`package` ,`persons`) "
                + "VALUES ('?',  ?,  ?)";
        PreparedStatement ps = connect.prepareStatement(sql);
        if (ps.executeUpdate() != -1) {
            ps.setString(1,"Japan" );
            ps.setInt(2, this.pack);
            ps.setString(3, jTextField1.getText());

And this is StackTrace

java.sql.SQLException: No value specified for parameter 1

Upvotes: 0

Views: 748

Answers (1)

Vasu
Vasu

Reputation: 22422

You are trying to execute the prepareStatement before setting the parameters, so you need to change your code as follows:

PreparedStatement ps = connect.prepareStatement(sql);

//set the parameters first
ps.setString(1,"Japan" );
ps.setInt(2, this.pack);
ps.setString(3, jTextField1.getText());

//now execute the prepared statement
if (ps.executeUpdate() != -1) {
  //add your code
}

I suggest you refer here to understand the jdbc concepts.

Upvotes: 3

Related Questions