如何将数据库表中一行的值与jsp中的字符串进行比较

时间:2016-02-14 10:09:25

标签: java mysql oracle jsp jdbc

我正在创建一个应用程序,它从数据库中获取特定属性行的值并将其与字符串进行比较。但在if条件下发生了零点异常。是否有任何其他方法可以从数据库中获取值并将其与特定字符串进行比较。

<%@ page import="java.sql.*"%>
<%@include file="faaltu.jsp"%>
<%@ page session="true" %>
<html>
<body>
<%
String id=request.getParameter("t1");
String pwd=request.getParameter("p1");


DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE","system","kajal");
PreparedStatement st1=con.prepareStatement("select * from kamp where userid=? and password=?");
st1.setString(1,id);
st1.setString(2,pwd);

ResultSet rs1=st1.executeQuery();

if(rs1.next()==true)
{

String k=(String)session.getAttribute("hey"); //hey is passed from other jsp file. it is an attribute of database table named gender
if(k.equals("female")) //here i get java.lang.NullPointerException 
//here female is string that user will select
{
response.sendRedirect("bhatia.html");
}
else
response.sendRedirect("a1.html");
}
else
{
response.sendRedirect("b.html");
}
con.close();
%>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

要检查使用此:

   if("female".equals(rs1.getString("gender")))