Capturing System time and storing in mySQL while submitting a form in JSP

Posted on 2004-11-15
Last Modified: 2010-04-01

I developed a form in JSP. This form contains two fields, i.e. text box,text area . These are for entering news title and news description. When i submit the form the system time (submission time) has to be stored in the database. How can i achieve this in JSP?.

At the moment my mySQLl data model contains two columns, i.e. NewsTitle, NewsDescription. And what data type should i use for inserting time of submission in the database.

Please help me in this regard. A snippest of code would be helpful for capturing system time (time of news submission).

Question by:ramsin
    LVL 19

    Accepted Solution

    As JSP is based on Java you can use java.util.Date class. But during inserting data to RDBMS (MySQL) you should convert it to java.sql.Date. And in DB your table should have additional column in type DATETIME. eg:
    NewDate DATETIME

    java.util.Date curdate = new java.util.Date();  
    java.sql.Date dbdate  = new java.sql.Date(curdate.getTime());
    PreparedStatement st = ....
    LVL 1

    Assisted Solution

    Hello, i use the following:

    //setup current dates and times
    java.util.Date thedatetoday = new java.util.Date();
    SimpleDateFormat fulldate = new SimpleDateFormat( "EEE MMM dd HH:mm:ss z yyyy" );
    SimpleDateFormat datadate = new SimpleDateFormat ( "yyyy-MM-dd" );  
    SimpleDateFormat datatime = new SimpleDateFormat ( "HH:mm:ss" );
    String thesubdate = datadate.format(thedatetoday);
    String thesubtime = datatime.format(thedatetoday);

    Then you can add to mysql database using:

    PreparedStatement pStmt = Conn.prepareStatement("insert into databasename values('',?,?)");

    The mysql table needs fields creating as TIME and DATE respectively

    Hope that helps,

    LVL 1

    Expert Comment

    I fancy the points for myself having given a valid answer, with code

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Suggested Solutions

    The recent Microsoft changes on update philosophy for Windows pre-10 and their impact on existing WSUS implementations.
    Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now