java - java和sql如何从同一列获取信息
问题描述
所以我正在尝试创建一个应用程序,对于我的登录,我让用户只输入一个 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*/}
推荐阅读
- c# - Asp.net 5.0 将 webapi 项目作为 windows 服务运行
- javascript - 我怎样才能让它在.map 上使用我的过滤器来反应原生?
- spring - 将 Elasticsearch 查询记录到 spring 数据 elasticsearch 的日志文件中
- java - 如果返回多个实例,则 Xpath 选择特定实例
- css - 边框图像:Safari(iOS)上的线性渐变行为不符合预期
- fastapi - 是否可以使用 fastapi 为请求正文的列表属性强加长度?
- c# - Azure Function App- 使用 dll 由另一个函数调用一个函数
- javascript - 刷新时用户在 Vue 和 Firebase 项目中消失
- javascript - 如何阅读或浏览 json 对象?
- python - 将两个 series/df 的每个元素与 pandas 中的自定义函数进行比较,无需 for 循环