I have written some code which will retrieve data from database . If I have giving hard coded value then it is working fine but I want to do dynamic then it not fetching data from db.
Coding
UserLogin.java
PrintWriter out=response.getWriter();
String firstName = request.getParameter("firstName");
String password = request.getParameter("password");
Connection con= DBConnection.getConnection();
String sql = "select count(*) from hmssignupdetails where firstname=? and password=?";
PreparedStatement ps;
try {
ps = con.prepareStatement(sql);
ps.setString(1, firstName);
ps.setString(2, password);
ResultSet rs=ps.executeQuery();
if(rs.next()){
int i= rs.getInt(1);
if(i==1){
request.setAttribute("err",firstName );
System.out.println("Details");
/*System.out.println(rs.getInt(1));
System.out.println(rs.getString(2));*/
response.sendRedirect("CustomerDetail.jsp");
}
else{
System.out.println("Sorry UserName or Password Error!");
RequestDispatcher rd=request.getRequestDispatcher("UserLogin.jsp");
request.setAttribute("err", "Invalid login");
rd.include(request, response);
}
}
how to set the value of username and password and I need to get the value in below jsp page . Please suggest ?
CustomerDetail.jsp
<%
String firstName = request.getParameter("firstName");
String password = request.getParameter("password");
Connection con = DBConnection.getConnection();
//String sql = "select * from HMSSIGNUPDETAILS";
/* Statement stmt = con.createStatement();
ResultSet rset = stmt
.executeQuery("select * from HMSSIGNUPDETAILS where firstname='Malay' and password='123'"); */
System.out.println("fetching data");
String sql = "select * from HMSSIGNUPDETAILS where firstname=? and password=?";
PreparedStatement ps;
ps = con.prepareStatement(sql);
ps.setString(1, firstName);
ps.setString(2, password);
ResultSet rset = ps.executeQuery();
%>
Above comment line code is working fine but if dynamic want to do with the help of Prepared statement then it is not able to retrieve data from db ? what may be reason ?
To dsiplay data
<center>
<table border="1" bordercolor="red" style="width: 100%;">
<tr style="font-size: 16px; color: red;">
<td>USER ID</td>
<td>FIRSTNAME</td>
<td>LASTNAME</td>
<td>EMAIL</td>
<td>PASSWORD</td>
<td>REPASSWORD</td>
<td>IDENTIFICATION</td>
<td>PHONE NO</td>
<td>ADDRESS</td>
</tr>
</table>
</center>
<%
if(rset.next()) {
%>
<center>
<table border="1" bordercolor="red" style="width: 100%">
<tr style="font-size: 20px; color: white;">
<td><%=rset.getString("userid")%></td>
<td><%=rset.getString("firstname")%></td>
<td><%=rset.getString("lastname")%></td>
<td><%=rset.getString("email")%></td>
<td><%=rset.getString("password")%></td>
<td><%=rset.getString("repassword")%></td>
<td><%=rset.getString("identification")%></td>
<td><%=rset.getString("phoneno")%></td>
<td><%=rset.getString("address")%></td>
</tr>
</table>
</center>
<%
}
%>