I'm trying to create very simple app like ATM machine basing on MySQL DB. In my method I want to ask user for ClientID and ClientPass (which are saved in my DB). If ClientID and Client password are correct, then I want to add money or take money from account. My code sample is,
public static void post () throws Exception{
Scanner sc = new Scanner(System.in);
System.out.println ("please enter user id:");
String userId = sc.nextLine();
System.out.println("please enter password:");
String pass = sc.nextLine();
System.out.println("how much you want to put");
int money = sc.nextInt();
try {
Connection con = ConnectionDB.getConnection();
String sql = "UPDATE `BankDB`.`Info` SET `Money`= '?' WHERE `ClientID`= '?' AND `ClientPass`='?'";
PreparedStatement posted = con.prepareStatement(sql);
posted.setString(1, userId);
posted.setString(2, pass);
posted.setInt(3, money);
posted.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
}
}
Scanner works now 2 times, not 3 times. And I am getting the following error
Parameter index out of range (1 > number of parameters, which is 0).
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:964)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:897)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:886)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:860)
at com.mysql.jdbc.PreparedStatement.checkBounds(PreparedStatement.java:3327)
at com.mysql.jdbc.PreparedStatement.setInternal(PreparedStatement.java:3312)
at com.mysql.jdbc.PreparedStatement.setString(PreparedStatement.java:4027)
at CurrentValue.post(CurrentValue.java:25)
at Helper.showMainMenu(Helper.java:15)
at Main.main(Main.java:9)