我的程序没有进入 "if" 部分,它总是进入 "else" 部分
My program is not going inside the "if" part and it is always going in the "else" part
我已经通过以下代码完成了与Oracle 10g Xe数据库的连接。
Context ctx=new InitialContext();
DataSource ds=(DataSource)ctx.lookup("java:OracleDS");
Connection con=ds.getConnection();
String mob=request.getParameter("mob_no");
String pass=request.getParameter("pass");
Statement stmt=con.createStatement();
//checking login details
String sql="select * from user1 where mob_no='"+mob+"' and password='"+pass+"'";
ResultSet rs=stmt.executeQuery(sql);
String sql2="select * from postpaid where mob_no='"+mob+"'";
ResultSet rs2=stmt.executeQuery(sql2);
if(rs.isBeforeFirst())
{
while(rs2.next())
{
if(rs2.getInt(2)!=0)
{
out.println("You have total due of "+rs2.getInt(2));
out.println("<a href=\"paybill.jsp\">Pay Bill</a>");
}
else {
out.println("You have no dues!!!!!");
}
}
}
else
{
out.println("Invalid login details.....<a href=\"#\">Back to home page</a>");
}
我的程序总是显示结果"Invalid login Details"
isBeforeFirst()
的文档说:
"Returns true
if the cursor is before the first row; false
if the cursor is at any other position or the result set contains no rows."
在这种情况下,false
很可能是指后者;即您的结果集为空,因为没有行与第一个查询中提供的用户和密码匹配。
更新
我的回答基本一样。最后的 else
分支被采用,因为第一个查询不匹配任何行。
为什么?
我可以猜到...但是,我建议您打印出您正在执行的查询 SQL 以确保语句是它们应该的样子。
另一件需要注意的事情是构建这样的查询:
String sql="select * from user1 where mob_no='"+mob+"' and password='"+pass+"'";
危险。假设有人发送了一个带有 mob_no
参数的请求,其值是这样的:
"foo' ; drop table user1 ;"
你应该像这样使用 PreparedStatement
和 SQL:
String sql = "select * from user1 where mob_no = ? and password = ?";
并使用 PreparedStatement.setString(...)
添加参数值。
我已经通过以下代码完成了与Oracle 10g Xe数据库的连接。
Context ctx=new InitialContext();
DataSource ds=(DataSource)ctx.lookup("java:OracleDS");
Connection con=ds.getConnection();
String mob=request.getParameter("mob_no");
String pass=request.getParameter("pass");
Statement stmt=con.createStatement();
//checking login details
String sql="select * from user1 where mob_no='"+mob+"' and password='"+pass+"'";
ResultSet rs=stmt.executeQuery(sql);
String sql2="select * from postpaid where mob_no='"+mob+"'";
ResultSet rs2=stmt.executeQuery(sql2);
if(rs.isBeforeFirst())
{
while(rs2.next())
{
if(rs2.getInt(2)!=0)
{
out.println("You have total due of "+rs2.getInt(2));
out.println("<a href=\"paybill.jsp\">Pay Bill</a>");
}
else {
out.println("You have no dues!!!!!");
}
}
}
else
{
out.println("Invalid login details.....<a href=\"#\">Back to home page</a>");
}
我的程序总是显示结果"Invalid login Details"
isBeforeFirst()
的文档说:
"Returns
true
if the cursor is before the first row;false
if the cursor is at any other position or the result set contains no rows."
在这种情况下,false
很可能是指后者;即您的结果集为空,因为没有行与第一个查询中提供的用户和密码匹配。
更新
我的回答基本一样。最后的 else
分支被采用,因为第一个查询不匹配任何行。
为什么?
我可以猜到...但是,我建议您打印出您正在执行的查询 SQL 以确保语句是它们应该的样子。
另一件需要注意的事情是构建这样的查询:
String sql="select * from user1 where mob_no='"+mob+"' and password='"+pass+"'";
危险。假设有人发送了一个带有 mob_no
参数的请求,其值是这样的:
"foo' ; drop table user1 ;"
你应该像这样使用 PreparedStatement
和 SQL:
String sql = "select * from user1 where mob_no = ? and password = ?";
并使用 PreparedStatement.setString(...)
添加参数值。