'java.sql.SQLException: Parameter index out of range (1 > number of parameters, which is 0) Problem
Can anyone teach me where I went wrong?
private void Submit_ButtonActionPerformed(java.awt.event.ActionEvent evt) {
if (First_Name.getText().isEmpty() || Last_Name.getText().isEmpty() || Contact_Number.getText().isEmpty() ){
JOptionPane.showMessageDialog(null, "Please Fill Up The Forms");
clear();
}
//First_Name.getText();
//Last_Name.getText();
//Contact_Number.getText();
else{
try{
String query = "INSERT INTO `user2`(`First Name`, `Last Name`, `Contact Number`) VALUES (?,?,?)";
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/registration?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC","root","");
pst = con.prepareStatement(query);
//pst.setString
pst.setString(0, First_Name.getText());
pst.setString(1, Last_Name.getText());
pst.setString(2,Contact_Number.getText());
pst.setInt(3, Integer.parseInt(Contact_Number.getText()));
pst.executeUpdate();
JOptionPane.showMessageDialog(null, "Successfully Registered");
clear();
}
catch (HeadlessException | SQLException e){
System.out.println(e);
}
}
Solution 1:[1]
You are setting 4 parameters when the sql-string only contains three "?".
Also the parameters are set starting from 1 and not 0.
See this answer for more detailed info: other answer
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | lemoncactus |
