简体   繁体   English

如何通过JDBC连接使用IO

[英]How to use IO with JDBC connection

this is my method for writing db query. 这是我编写数据库查询的方法。

        public static void post() throws Exception{

        int clientMPNumber = Parcel.typeClientNumber();
        int orderPassword = Parcel.generatePass();

        try{
            Connection con = ConnectionDB.getConnection();
            PreparedStatement posted = con.prepareStatement("UPDATE `BankDB`.`Info` SET `Money`='77777' WHERE `ClientID`='77' AND `ClientPass`='1111';");

            posted.executeUpdate();
        }catch(Exception e){System.out.println(e);}
    finally{    
        System.out.println("Insert completed");
    }
    }

I'm trying to do something like ATM machine. 我正在尝试做类似ATM机的事情。 So I expect that user types his ID and password, and then the user can withdraw money or deposit money. 因此,我希望用户键入自己的ID和密码,然后该用户便可以取款或存款。 So I want to check login data correctness. 所以我想检查登录数据的正确性。 User needs to type correct ID/password [logins/passwords are placed in MySQL DB]. 用户需要输入正确的ID /密码[登录名/密码放置在MySQL DB中]。

PreparedStatement posted = con.prepareStatement("UPDATE `BankDB`.`Info` SET `Money`='77777' WHERE `ClientID`='USER TYPES IT' AND `ClientPass`='USER TYPES IT';");

There is a sentence: "USER TYPES IT", this is my problem. 有句话:“用户类型”,这是我的问题。 I want to use here a Scanner or something like this. 我想在这里使用扫描仪或类似的东西。 How can I do it? 我该怎么做?

A prototype for you (just an example, you should split up the part get userid, password, outside of this function for better practice): 为您准备的原型(仅作为示例,您应在此功能之外拆分获取用户名,密码的部分,以进行更好的练习):

public void post (){
    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();
    Connection con;
    PreparedStatement posted;
    try {
        con = ConnectionDB.getConnection();
        String sql = "UPDATE `BankDB`.`Info` SET `Money`='77777' WHERE `ClientID`=? AND `ClientPass`=?";
        posted = con.prepareStatement(sql);
        posted.setString(1, userId);
        posted.setString(2, pass);
        posted.executeUpdate();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        posted.close();
        con.close();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM