0

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)
sathya
  • 1,982
  • 1
  • 20
  • 37
damianm
  • 121
  • 1
  • 8
  • I think the single quotes around `?` are the reason, you don't need them. Also, you should close your connection. – tima Jul 19 '17 at 18:25
  • Plase see the JDBC tutorial - p repared statements – Akash Jul 19 '17 at 18:31
  • thanks but there is still error which looks like example below: please enter user id: please enter password: 123456 how much you want to put 400. I mean that Scanner doesn't ask me about user id – damianm Jul 19 '17 at 18:32
  • Don't put single quotes around your question marks. – Rabbit Guy Jul 19 '17 at 18:49
  • can you help me guys about Scanner problem? he just skips password – damianm Jul 19 '17 at 20:46

0 Answers0