这是我编写数据库查询的方法。
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");
}
}
我正在尝试做一些类似 ATM 机的事情。所以我希望用户输入他的ID和密码,然后用户可以取款或存款。 所以我想检查登录数据的正确性。用户需要输入正确的ID/密码[登录名/密码放置在MySQL DB中]。
PreparedStatement posted = con.prepareStatement("UPDATE `BankDB`.`Info` SET `Money`='77777' WHERE `ClientID`='USER TYPES IT' AND `ClientPass`='USER TYPES IT';");
有一句话:“USER TYPES IT”,这是我的问题。我想在这里使用扫描仪或类似的东西。我该怎么做?
最佳答案
为您提供的原型(prototype)(只是一个示例,您应该将获取用户名、密码的部分拆分到此函数之外,以便更好的实践):
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();
}
}
关于java - 如何通过 JDBC 连接使用 IO,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45177706/