在android studio中使用JDBC登录级别

时间:2018-03-06 10:38:04

标签: android sql sql-server jdbc

我正忙于创建一个连接到数据库以检查凭据的登录页面。现在我正在尝试创建一行代码,如果您是员工职位1,则转到Main2Activity,如果您是员工职位2,则转到Main3Activity。

这是我的代码:

String query = "select * from cc_employee where employee_username= '" + usernam.toString() + "' and employee_password = '"+ passwordd.toString() +"'  ";
                        Statement stmt = con.createStatement();
                        ResultSet rs = stmt.executeQuery(query);
                        if(rs.next())
                        {
                            z = "Login successful";
                            type = rs.getString("employee_position");
                            isSuccess=true;
                            con.close();
                            if (type = "1"){
                                Intent i=new Intent(login.this,Main2Activity.class);
                                startActivity(i);
                            }
                            else {
                                Intent i=new Intent(login.this,Main3Activity.class);
                                startActivity(i);
                            }

                        }
                        else
                        {
                            z = "Invalid Credentials!";
                            isSuccess = false;
                        }

以下是我的数据库Database

的图片

1 个答案:

答案 0 :(得分:0)

以下是对我有用的答案

String query = "select * from cc_employee where employee_username= '" + usernam.toString() + "' and employee_password = '"+ passwordd.toString() +"'  ";                            
Statement stmt = con.createStatement();
                    ResultSet rs = stmt.executeQuery(query);
                    if(rs.next())
                    {
                        z = "Login successful";
                        type = rs.getInt("employee_position");
                        isSuccess=true;
                        con.close();

                        switch (type){
                            case 1:
                                intent = new Intent(login.this,Main2Activity.class);
                                break;
                            case 2:

                            intent = new Intent(login.this,Main3Activity.class);
                            break;

                            case 3:
                                intent = new Intent(login.this,Main4Activity.class);
                                break;
                            case 4:
                                intent = new Intent(login.this,Main5Activity.class);
                                break;
                        }
                        startActivity(intent);

                    }
                    else
                    {
                        z = "Invalid Credentials!";
                        isSuccess = false;
                    }
                }