Note that there are some explanatory texts on larger screens.

plurals
  1. POCannot store Date variable in database in Java
    primarykey
    data
    text
    <p>I am trying to keep the last login date of a user in the database. Here are the necessary parts:</p> <p>In my login page, I create a Date object and put it into my session, I import <em>java.util.Date</em>:</p> <pre><code>&lt;% Calendar c = Calendar.getInstance(); Date l = new Date(c.get(Calendar.YEAR),c.get(Calendar.MONTH),c.get(Calendar.DAY_OF_MONTH),c.get(Calendar.HOUR),c.get(Calendar.MINUTE)); //the midnight, that's the first second of the day. //Globals.customer.lastlogin=l; session.setAttribute("lastlogin", l); %&gt; </code></pre> <p>Then when login is successful, I update my database in my servlet, this servlet is called when login button is clicked:</p> <pre><code>try { int result=-1; PreparedStatement checkDB = (PreparedStatement) con.prepareStatement( "UPDATE users set lastlogin='"+(Date)session.getAttribute("lastlogin")+"' where username='"+session.getAttribute("username")+"'"); result= checkDB.executeUpdate(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } </code></pre> <p>I keep lastlogin as a Date variable in my Database. But I get the following error when trying to update last login:</p> <pre><code>com.mysql.jdbc.MysqlDataTruncation: Data truncation: Incorrect date value: 'Wed Jul 02 05:49:00 VET 3913' for column 'lastlogin' at row 1 at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2936) at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1601) at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1710) at com.mysql.jdbc.Connection.execSQL(Connection.java:2436) at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1402) at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1694) at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1608) at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1593) at classes.LoginServlet.service(LoginServlet.java:94) at javax.servlet.http.HttpServlet.service(HttpServlet.java:723) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293) at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:861) at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:606) at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489) at java.lang.Thread.run(Thread.java:662) </code></pre> <p>Can anyone help me with this? Thanks</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload