• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Store login record in database

 
Smiley31
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
im currently working on a project on jsp. i am required to keep track of the user's details when they log in successfully. for example, when the user log in to the system, i have to keep record of the user's login name and time (system's time) and store the record into a table (table's name: log) in the database. i've tried to store the record into the database but it was unsuccessful. below is my source code.
Login.jsp
<%@ page contentType="text/html; charset=iso-8859-1" language="java" import="java.io.*,java.util.*,javax.servlet.*,javax.servlet.http.*,java.sql.*" errorPage="" %>
<%@ include file="Connections/test.jsp" %>
<%
String userName = request.getParameter("staffid");
String pword = request.getParameter("password");
Driver DriverRecordset4 = (Driver)Class.forName(MM_test_DRIVER).newInstance();
Connection ConnRecordset4 = DriverManager.getConnection (MM_test_STRING,MM_test_USERNAME,MM_test_PASSWORD);
PreparedStatement StatementRecordset4 = ConnRecordset4.prepareStatement("SELECT STAFFID, PASSWORD FROM Staff WHERE StaffID = '"+ userName + "' "+ "AND Password = '"+ pword + "' ");
ResultSet Recordset4 = StatementRecordset4.executeQuery();
boolean Recordset4_isEmpty = !Recordset4.next();
boolean Recordset4_hasData = !Recordset4_isEmpty;
Object Recordset4_data;
int Recordset4_numRows = 0;
%>
<%
Date date = new Date();
String date = request.getParameter("date");
Class.forName ( MM_test_DRIVER ) ;
Connection ConnRecordset6 = DriverManager.getConnection ( MM_test_STRING, MM_test_USERNAME, MM_test_PASSWORD ) ;
PreparedStatement StatementRecordset6 = ConnRecordset6.prepareStatement ( "INSERT INTO Log (StaffID,[DATE]) VALUES (<%=request.getParameter("staffid")%>,<%request.getParameter("date")%> " ) ;
StatementRecordset6.setString ( 1, userName ) ;
StatementRecordset6.executeUpdate () ;
%>
<%
if (Recordset4_hasData)
{
%>
{
%>
<jsp:forward page="mainpage.jsp"/>
<%
}
else{
out.println("<HTML>");
out.println("<HEAD>");
out.println("<TITLE>Login Unsuccessful</TITLE>");
out.println("</HEAD>");
out.println("<body background=images/background.jpg style=background-image: url(images/background.jpg)><h1>");
Can anyone help me with this???
thanxs a million!
 
Ali Gohar
Ranch Hand
Posts: 572
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think there is some problem in your code, Consider the following Lines of Code:
PreparedStatement StatementRecordset4 = ConnRecordset4.prepareStatement("SELECT STAFFID, PASSWORD FROM Staff WHERE StaffID = '"+ userName + "' "+ "AND Password = '"+ pword + "' ");
ResultSet Recordset4 = StatementRecordset4.executeQuery();
Either use Statement instead of PreparedStatement or do following:
PreapredStatement StatementRecordset4 = ConnRecordset4.prepareStatement("SELECT STAFFID, PASSWORD FROM Staff WHERE StaffID = ? AND Password = ?")
StatementRecordset4.setString(1,userName);
StatementRecordset4.setString(2,pword);
ResultSet Recordset4 = StatementRecordset4.executeQuery();
Now consider Following Lines,Same problem is with these lines of code :
PreparedStatement StatementRecordset6 = ConnRecordset6.prepareStatement ( "INSERT INTO Log (StaffID,[DATE]) VALUES (<%=request.getParameter("staffid")%>,<%request.getParameter("date")%> " ) ;
StatementRecordset6.setString ( 1, userName ) ; //Why use this???
StatementRecordset6.executeUpdate () ;
Now consider following Lines:
<%
if (Recordset4_hasData)
{
%>
{ //Why thise bracket???
%>
<jsp:forward page="mainpage.jsp"/>
<%
}
else{
out.println("<HTML>");
out.println("<HEAD>");
out.println("<TITLE>Login Unsuccessful</TITLE>");
out.println("</HEAD>");
out.println("<body background=images/background.jpg style=background-image: url(images/background.jpg)><h1>");
Try to do the suggested things. If still the code doesn't work then post the error message you get.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64716
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Smiley31",
We're really glad that you are here with us in the JSP forum, and there aren't many rules you'll have to worry about, but one is that proper names are required. Please take a look at the JavaRanch Naming Policy and change your display name to match it.
In particular, your display name must be a first and a last name separated by a space character, and must not be obviously fictitious.
Thanks!
bear
JSP Forum Bartender
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64716
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since this is primarily concerned with database access, I'm moving this to the JDBC forum.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic