1

I am trying to make login time and logout time web application in jsp netbeans. While i try to save logout time into mysql database the date & time save correctly but user name and password both save as null. Please help me to save user name and password correctly to table. Here is my logout code:

`<%@ page import ="java.sql.*" %>
`<%String url="jdbc:mysql://localhost:3306/mysql";`
   ` String user="root";`
   ` String password="";`
    `Class.forName("com.mysql.jdbc.Driver");`
    `Connection con = DriverManager.getConnection(url, user, password);`
    `Statement st = con.createStatement();`
    `String uname1= request.getParameter("first_name");`  
    `String pwd = request.getParameter("pass");`
    `session.setAttribute("fname", uname1);`
    `session.setAttribute("pass", pwd);`
    `int i = st.executeUpdate("insert into logut values ('" + uname1 + "','" + pwd + "',now())");`
    `if (i > 0) `
    `{out.write("<script type='text/javascript'>\n");`
     `out.write("alert(' Logout Successfully!!! ');\n");`
     `out.write("setTimeout(function({window.location.href='index.jsp'},1000);");`
     `out.write("</script>\n");`
    `}`
%>`

My database save like this: id= null pass=null and date and time save correctly. help me out. Thank you advance.

Manwal
  • 23,450
  • 12
  • 63
  • 93

1 Answers1

1

There is a typo in your statement. I guess you mean the table logout

"insert into logut values ('" + uname1 + "','" + pwd + "',now())"

But aside this you really have to consider prepared statements.

String insertTableSQL = "INSERT INTO DBUSER"
    + "(USER_ID, USERNAME, CREATED_BY, CREATED_DATE) VALUES"
    + "(?,?,?,?)";
PreparedStatement preparedStatement = dbConnection.prepareStatement(insertTableSQL);
preparedStatement.setInt(1, 11);
preparedStatement.setString(2, "username");
preparedStatement.setString(3, "password");
preparedStatement.setTimestamp(4, getCurrentTimeStamp());
// execute insert SQL statement
preparedStatement .executeUpdate();

Why prepared Statements:

Community
  • 1
  • 1
SWiggels
  • 2,159
  • 1
  • 21
  • 35