结果集jsp - 如何使用?
有人可以告诉我为什么这不起作用吗?我希望能够检查从数据库查询返回的用户名/密码是否等于某个值。
会话属性“loginSuccess”始终返回 false,即使值应该匹配也是如此。
<%@ page language="java" import="java.sql.*" %>
<%
String driver = "oracle.jdbc.driver.OracleDriver";
Class.forName(driver).newInstance();
Connection con = null;
ResultSet rst = null;
Statement stmt = null;
try {
String url = "jdbc:oracle:thin:username/password@url:port:SID";
con = DriverManager.getConnection(url);
stmt = con.createStatement();
} catch (Exception e) {
System.out.println(e.getMessage());
}
if (request.getParameter("action") != null) {
rst = stmt.executeQuery("select username, password"
+ " from table");
}
%>
<%
boolean loginCheck = false;
int count = 1;
while (rst.next()) {
if(request.getParameter("action")!=null){
if("username1"==rst.getString("username")){
if("password1"==rst.getString("password")){
loginCheck = true;
}
}
}
count++;
}
if(loginCheck==true){
session.setAttribute("loginSuccess", "true");
}else{
session.setAttribute("loginSuccess", "false");
}
rst.close();
stmt.close();
con.close();
%>
<jsp:forward page="login.jsp"/>
Can someone please tell me why this isn't working? I would like to be able to check if the username/password returned from the DB query is equal to a value.
Session attribute "loginSuccess" always returns false, even if the values should be matching.
<%@ page language="java" import="java.sql.*" %>
<%
String driver = "oracle.jdbc.driver.OracleDriver";
Class.forName(driver).newInstance();
Connection con = null;
ResultSet rst = null;
Statement stmt = null;
try {
String url = "jdbc:oracle:thin:username/password@url:port:SID";
con = DriverManager.getConnection(url);
stmt = con.createStatement();
} catch (Exception e) {
System.out.println(e.getMessage());
}
if (request.getParameter("action") != null) {
rst = stmt.executeQuery("select username, password"
+ " from table");
}
%>
<%
boolean loginCheck = false;
int count = 1;
while (rst.next()) {
if(request.getParameter("action")!=null){
if("username1"==rst.getString("username")){
if("password1"==rst.getString("password")){
loginCheck = true;
}
}
}
count++;
}
if(loginCheck==true){
session.setAttribute("loginSuccess", "true");
}else{
session.setAttribute("loginSuccess", "false");
}
rst.close();
stmt.close();
con.close();
%>
<jsp:forward page="login.jsp"/>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您正在使用 == 来比较字符串。尝试使用等于。
我建议不要以这种方式将 scriptlet 代码放入 JSP 中。您应该使用 JSTL 标记库来编写 JSP。
我还建议将该数据库代码放入 POJO 中,以便您可以离线测试它,与 servlet/JSP 引擎分开。然后让 servlet 实例化 POJO,与数据库交互,并将结果发送到 JSP。
这需要做更多的工作,但从长远来看,设计会更好地扩展和扩展。
You're using == to compare Strings. Try equals instead.
I'd recommend not putting scriptlet code in JSPs this way. You should write JSPs using the JSTL tag library.
I'd also recommend putting that database code in a POJO so that you can you test it off line, separate from your servlet/JSP engine. Then have a servlet instantiate the POJO, interact with the database, and send the result to the JSP.
It's a bit more work, but the design will extend and scale better in the long run.
我认为您在这行代码中犯了一个基本的语法错误。
应该是这样的
并且密码类似。
请检查这是否有帮助。
I think that you got a basic grammatical mistake in this line of code.
It should be like this
and the password is similar.
Please check if this can help.