[英]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
记录,用于记录username
和password
的组合。 So, you can better use an if
instead of while
. 因此,最好使用
if
代替while
。
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.