因此,我正在尝试创建一个应用程序,并且对于我的登录,我让用户仅输入一个 PIN 码。我想根据他们的职位名称打开一个新的 JFrame。在我的数据库中,我在用户表中创建了一个名为“职业”的列。如何获取该列中的信息,以便根据其职位创建不同的 IF 语句?
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/restaurantdb", "root","");
String sql = "Select * from users where pin=?";
PreparedStatement pst = con.prepareStatement(sql);
pst.setString(1, PinField.getText());
ResultSet rs = pst.executeQuery();
if (rs.next()){
JOptionPane.showMessageDialog(null, "Login Succesful");
/*if (occupation = "Server") {
New ServerWindow
}
*/
}
else{
JOptionPane.showMessageDialog(null, "Login Failed");
PinField.setText("");
}
con.close();
}
catch(Exception e){
JOptionPane.showMessageDialog(null, e);
}
}
最佳答案
Preparedstatement job=con.prepareStatement("SELECT
Occupation FROM USERS WHERE PIN=?");
job.setString(1,PinField.getText());
try(ResultSet myJob=job.executeQuery())
{
if(myJob.next())
{
String theJob=myJob.getString("Occupation");
if(theJob.equals("Whatever u want");{/*open frame 1*/}
if(theJob.equals("Whatever u want 2");{/*open frame 2*/}
}
}
或者
由于您已经在 ResultSet rs 中拥有完整数据,因此请执行此操作
String theJob=rs.getString("Occupation");
if(theJob.equals("Whatever u want");{/*open frame 1*/}
if(theJob.equals("Whatever u want 2");{/*open frame 2*/}
关于java - java和sql如何获取同一列的信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61219174/