Java(Netbeans) - 如何编码"访问被拒绝"在if语句中,我尝试了最好,但它没有工作..?

时间:2017-09-21 03:20:31

标签: java sql

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

 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    | 
-----------------------------------------------

3 个答案:

答案 0 :(得分:1)

最简单的答案为else。在else if之前添加cashier,然后在“默认”访问被拒绝的情况下再添加一个else。像,

if (name.equals("admin")) {
    JOptionPane.showMessageDialog(null, "Access Permitted");
    ManageUI ah = new ManageUI();
    ah.setVisible(true);
} else if (name.equals("cashier")) { // <-- add an else
    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 { // <-- another else
    JOptionPane.showMessageDialog(null, "Access Denied");
}

答案 1 :(得分:1)

如果在以下代码中没有任何其他内容匹配,那么您忘记了该怎么做,如果包含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);
    }

答案 2 :(得分:0)

  results = [{'06079467037ACD8AMV': ['US', 'IT', 3065, 'NOT_FOUND']}, {'A557288EDE635FD6MV': ['US', 'Cars 3', 2604, 'NOT_FOUND']}, {'0367F463382D27FBMV': ['US', 'Kingsman: The Golden Circle', 1580, 'NOT_FOUND']}]

}