开发者

result set jsp - how to use?

开发者 https://www.devze.com 2023-02-18 20:54 出处:网络
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.

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();
                开发者_如何学C            con.close();
                %>

<jsp:forward page="login.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.

"username1"==rst.getString("username")

It should be like this

rst.getString("username").equalsIgnoreCase("username1");

and the password is similar. Please check if this can help.

0

精彩评论

暂无评论...
验证码 换一张
取 消