English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

I am trying to redirect to a diff page depending on a certain condition,only one condition is working.the code is below.
<%@page import="java.sql.*;"%><%--import the sql pacakage of JAVA.--%>
<%Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");//Database Driver.
//Getting Data from HTML page.
String User=request.getParameter("UserName");
String Pwd=request.getParameter("Pwd");
Connection conn=DriverManager.getConnection("jdbc:odbc:File");
//Create a mysql odbc connection.
Statement st=conn.createStatement();//create a connection statement.
ResultSet res = st.executeQuery("SELECT * FROM users WHERE User_Name='"+User+"' AND Pwd='"+Pwd+"'");//create resultset to access data from database.
if(res!=null)
{
String redirectURL = "DeptInfo.html";//only this works
response.sendRedirect(redirectURL);
}
else//not working
{
String redirectURL = "Login.html";
response.sendRedirect(redirectURL);
}
res.close();//close resultset.
conn.close();//close connection.
%>
can any one tell me y this happens?

2006-09-01 20:30:14 · 1 answers · asked by Niru 1 in Computers & Internet Programming & Design

1 answers

"SELECT * FROM users WHERE User_Name='"+User+"' AND Pwd='"+Pwd+"'";
i guess problem is here. Because if no row has matched the condition it does not return null. Try the following query:
SELECT COUNT(*) FROM users WHERE User_Name ='"+User+"' AND Pwd='"+Pwd+"'";
If the condition matches it return the no of selected rows. If no row is there to match the condition it will return 0(zero). And depending on if it is returning 0 or not you may redirect the page to desired location. I hope this may solve your problem.

2006-09-01 20:51:13 · answer #1 · answered by Reality Bites! 2 · 0 0

fedest.com, questions and answers