简体   繁体   English

如何从选择查询中获取另一个字符串?

[英]How can i get another string from select query?

I want to get string from column no. 我想从列号获取字符串。 4 from my database to check user privileges. 4从我的数据库中检查用户权限。

Can I use rs.getString(index) to get data from column no.4? 我可以使用rs.getString(index)从第4列获取数据吗? I want to check user´s privileges...so if the column data is equal 4, the page will be redirected to AdminControlPanel.jsp BUT, this code doesn´t work :( 我想检查用户的权限...因此,如果列数据等于4,则页面将被重定向到AdminControlPanel.jsp BUT,此代码不起作用:(

String user=request.getParameter("login");
String pass=request.getParameter("password");
 Class.forName("com.mysql.jdbc.Driver").newInstance();
    Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/android","root","root");  
           Statement st=con.createStatement();
           ResultSet rs=st.executeQuery("select * from users where login='"+user+"' and password='"+pass+"'");
           String p = rs.getString(4);                   
           int count=0;
           while(rs.next()){
           count++;
          }
      if(count>0 && p == "4"){
             // out.println(rs);
                           response.sendRedirect("AdminControlPanel.jsp");
                      }
          else{
               out.println("aaa");
               response.sendRedirect("#");
           }
        }
                 catch(Exception e){
    System.out.println(e);
}

You want 你要

while (rs.next()) {
   String val = rs.getString(4);
   ....

Note that iterating through a ResultSet iterates through the rows . 请注意,遍历ResultSet遍历 For each row, the column indexing starts from '1'. 对于每一行,列索引均从“ 1”开始。

However it's safer to get by column name, since your SQL query doesn't specify neither the columns nor the order in which they're returned: 但是,通过列名获取更安全,因为您的SQL查询既未指定列也未指定返回顺序:

String val = rs.getString("COLUMN_NAME");

I see from the below that you need an integer. 我从下面看到,您需要一个整数。 Check out the doc for ResultSet for more info, but: 查看ResultSet的文档以获取更多信息,但是:

int val = rs.getInt("COLUMN_NAME");

As an aside, I don't see you closing your ResultSet / Statement / Connection in the above. 顺便说一句,我看不到您在上面关闭了ResultSet / Statement / Connection If you're not, then you'll need to! 如果不是,那么就需要!

String p = rs.getString(4);   // This should be inside your while                  
int count=0;
while(rs.next()){
    count++;
}
  • You should move your first line inside your while loop. 您应该在while循环内移动第一行。 You can't fetch the columns of a row, until you move your cursor to that row using res.next() . 您无法获取一行的列,直到使用res.next()将光标移动到该行。

  • Also, since your database should ideally have only one record for a combination of username and password . 此外,由于理想情况下,数据库应该只包含one记录,用于记录usernamepassword的组合。 So, you can better use an if instead of while . 因此,最好使用if代替while

  • And you don't really need a count variable there. 而且您真的不需要那里的count变量。

So, your code should be: - 因此,您的代码应为:-

ResultSet rs=st.executeQuery("select * from users where login='"+user+"' " +
                              "and password='"+pass+"'");
if (rs.next()) {
    String p = rs.getString(4);  // Note that using Column name is a better idea

    // or rs.getInt(4) if the column type is `int`

    if(p.equals("4")) {   // Use equals method to compare string content
         response.sendRedirect("AdminControlPanel.jsp");

    } else{
         out.println("aaa");
         response.sendRedirect("#");
    }
}

Also, note that you should compare your string using equals method. 另外,请注意,您应该使用equals方法比较字符串。 if (p == "4") will give you false result. if (p == "4")将给您错误的结果。 == operator does not compare the content of the string, rather the content of the reference used in comparison. ==运算符不比较字符串的内容,而是比较中使用的引用的内容。

you are comparing two String objects rather than checking the values in the String. 您正在比较两个String对象,而不是检查String中的值。

just change the code to p.equals("4") and try. 只需将代码更改为p.equals(“ 4”)并尝试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何用另一个表达式(字符串)替换字符串中的特定单词,另一个表达式必须获取特定单词的某些部分 - How can i replace particular word from a string with another expression(string) and the another expression has to get some part of particular word 如何使用Apache Jena获得“选择计数(*)”查询的结果? - How can I get the result of a “Select count(*)” query with Apache Jena? 如何获得一个类,以从位于另一个类中的对象的数组列表中打印出字符串变量? - How can I get a class to print out a string variable from an arraylist of objects located in another class? 我如何从Java集获取返回值 <String> 在java的另一个类中 - how i can get the return value from java set<String> in another class in java 如何从多行SQL查询中获取非字符串数组字段 - How can i get non String Array field from SQL query with multiple rows 如何从String获取HWND? - How can I get HWND from String? 如何从hashCode获取字符串 - how can I get the String from hashCode 如何从字符串中获取特定的字符串(子字符串) - how can I get particular string(sub string ) from a string 如何在Android的另一个类中获取String? - How Can I Get String for another class in Android? 我如何才能将最终字符串转换为功能以用于其他功能? - How can i get final string into function for use to another function?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM