sanket karanjule
sanket karanjule

Reputation: 37

data can not save into mysql properly

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.

Upvotes: 1

Views: 128

Answers (1)

SWiggels
SWiggels

Reputation: 2296

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:

Upvotes: 1

Related Questions