gpt4 book ai didi

Java (Netbeans) - 如何在 if 语句中编写 "access denied"代码,我尽了最大努力,但没有成功..?

转载 作者:行者123 更新时间:2023-12-02 02:35:53 24 4
gpt4 key购买 nike

需要创建三个用户登录,每个登录用户登录后需要有不同的界面。我的操作按钮脚本如下:

 try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String url="jdbc:sqlserver://localhost:1433;databaseName=77OOP062;user=sa;password=hnd";
Connection conn= DriverManager.getConnection(url);
String sql ="Select Type from Users where Username=? and Password=?";
PreparedStatement pst =conn.prepareStatement(sql);
pst.setString(1, jTextFieldUserName.getText());
pst.setString(2, jTextFieldPassword.getText());
ResultSet rs = pst.executeQuery();
rs.next();
String name = rs.getString("Type");





if (name.equals("admin")) {
JOptionPane.showMessageDialog(null, "Access Permitted");
ManageUI ah = new ManageUI();
ah.setVisible(true);
}

if (name.equals("cashier")){
JOptionPane.showMessageDialog(null, "Access Permitted");
CashierUI eh = new CashierUI();
eh.setVisible(true);
}
else if (name.equals("stockkeeper")){
JOptionPane.showMessageDialog(null, "Access Permitted");
StockKeeperUI aq = new StockKeeperUI();
aq.setVisible(true);
}
conn.close();
}
catch (Exception e){
JOptionPane.showMessageDialog(null, e);
}


This is my Sql server database: User
-----------------------------------------------
Username | Type | Password |
-----------------------------------------------
mick | admin | 123 |
-----------------------------------------------
nisha | cashier | 456 |
-----------------------------------------------
sam | stockkeeper | 789 |
-----------------------------------------------

最佳答案

当没有任何内容与以下代码中的最后一个 else 匹配时,您忘记了该怎么做,同样在第二个 if include else 来完成 if else 链。

 try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String url="jdbc:sqlserver://localhost:1433;databaseName=77OOP062;user=sa;password=hnd";
Connection conn= DriverManager.getConnection(url);
String sql ="Select Type from Users where Username=? and Password=?";
PreparedStatement pst =conn.prepareStatement(sql);
pst.setString(1, jTextFieldUserName.getText());
pst.setString(2, jTextFieldPassword.getText());
ResultSet rs = pst.executeQuery();
if(rs.next()){ // Row Exists
String name = rs.getString("Type");

if (name.equals("admin")) {
JOptionPane.showMessageDialog(null, "Access Permitted");
ManageUI ah = new ManageUI();
ah.setVisible(true);
}

else if (name.equals("cashier")){
JOptionPane.showMessageDialog(null, "Access Permitted");
CashierUI eh = new CashierUI();
eh.setVisible(true);
}
else if (name.equals("stockkeeper")){
JOptionPane.showMessageDialog(null, "Access Permitted");
StockKeeperUI aq = new StockKeeperUI();
aq.setVisible(true);
}else {
JOptionPane.showMessageDialog(null, "Access Denied");
}
} else{
JOptionPane.showMessageDialog(null, "Access Denied"); // Row Doesnot exists
}
conn.close();
}
catch (Exception e){
JOptionPane.showMessageDialog(null, e);
}

关于Java (Netbeans) - 如何在 if 语句中编写 "access denied"代码,我尽了最大努力,但没有成功..?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46334940/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com