Start New Discussion within our Web Development Community

hi all ,

Am doing event management project. In jsp page am using javascript date picker. Then i passed that to servlet page by the following code.

DateFormat formatter ;
formatter = new SimpleDateFormat("dd-MM-yyyy");
Date date=formatter.parse(request.getParameter("date"));

now I want to insert date in mysql database , How can I achieve that.Pls help me.

Thanks in advance ..

Do you know how to run queries with java and how in general to insert data in a database?
There are several ways so can you post the code that use to insert normal data, so we can give you a suggestion similar to your code style?
Do you use PreparedStatements or jua Statements to create your query?

DataManager dataManager = new DataManager();
        DateFormat formatter ; 
        formatter = new SimpleDateFormat("dd-MM-yyyy");
Date date=formatter.parse(request.getParameter("date"));

connectivity coding below

public void getUserData(Date date, String str_type, String str_eventholiday){

    query ="INSERT INTO `krishna`.`events_and_holidays` (`date`, `type`, `description`) " +
            "VALUES " +
    pstm = con.prepareStatement(query);
    pstm.setDate(1, date);
catch (SQLException e) {

That parse method is giving you a java.util.Date object, you need a java.sql.Date object in the setDate method. See the Date(long) constructor of java.sql.Date and the getTime() method of java.util.Date and use the latter as the argument in the former to get a java.sql.Date instance.

Edit: P.S. it would have helped to know the exception you were getting.

Edit Again: It is also not normal to surround the schema, table, and column names with single quotes ( ' ), although you might surround them with double quotes ( " ) if they also represent reserved words in the db.

<%@ page language="JAVA" contentType="text/html" %>//directive to specify that language used is java and specifying the content type
<%@ page import="java.sql.*" %>//to import sql package
 String driverClass ="com.mysql.jdbc.Driver";
	String connectionString="jdbc:mysql://localhost:3306/login"	;
	String username="root";
	String password="password";
	String query="INSERT into table_name values(?)"	;
	Class.forName(driverClass)	;//to use jdbc driver to connect to mysql database
	con=DriverManager.getConnection(connectionString,username,password);//to make connection

 java.util.Date today = new java.util.Date();//date object
 stmt.setDate(1, today.getTime());//to insert the date at the wildcard sql statement used above
int a=stmt.executeUpdate()	;//executeUpdate will return the number of affected rows
	if(a>0)  {
		out.println("Data successfully inserted")	;
else {

}catch(Exception sql){out.println(sql.toString());}


Thanks IIM ,masijade and javaAddict.. Thanks a lot for Ur answers .

Do not use scriptlets. Scriptlets were included in the first version of JSP and quickly gotten rid of in the second. They only still exist to facilitate backwards compatability. You need to use beans, or at the very least JSTL sql tags. And JSPs should not being doing anything except displaying data, anyway. Any site request that is to actually do anything should be going to a servlet (and that should also make use of existing beans) and then, after the work is done, should be forwarding the request to a JSP to actually display the page.

You can even use this way to insert record...

<%@ page import="*,java.util.*,java.sql.*"%>
<%@ page import="javax.servlet.http.*,javax.servlet.*" %>
<%@ taglib uri="" prefix="c"%>
<%@ taglib uri="" prefix="sql"%>
<title>JINSERT Operation</title>
<sql:setDataSource var="snapshot" driver="com.mysql.jdbc.Driver"
     user="root"  password="pass123"/>

<sql:query dataSource="${snapshot}" var="result">
INSERT INTO Employees VALUES (104, 2, 'Nuha', 'Ali');
<sql:query dataSource="${snapshot}" var="result">
SELECT * from Employees;
<table border="1" width="100%">
   <th>Emp ID</th>
   <th>First Name</th>
   <th>Last Name</th>
<c:forEach var="row" items="${result.rows}">
   <td><c:out value="${}"/></td>
   <td><c:out value="${row.first}"/></td>
   <td><c:out value="${row.last}"/></td>
   <td><c:out value="${row.age}"/></td>

And again, JSPs should not be performing actions. Their purpose is display, and display only. You have heard the phrase "just because you can do something does not mean that you should", right?

Doing these sorts of things in a JSP usually leads to unscaleable and/or nearly unmaintainable code.

while u are picking the date using date class and populating it to the database fields u might not able to store the value,if the field type in database is of type date,and u r parsing a long value ie:while ur picking it from date it is going to return the date inthe form of long value so how it is actually mapped if we use different data types for dates with respect to different databases....I think it make sence if u found any solutions plese remind here....TQ.

the solution provided by mr iim is still giving me one error.
it gives "incompatible types: long cannot be converted to date". and secondly in my program i am using sql server 2008r2 i am facing a sqlexception for the insertion of date
in my database i have a column name date and the data type is datetime

e = ( An explicit value for the identity column in table 'letterhead' can only be specified when a column list is used and IDENTITY_INSERT is ON.
This question has already been answered. Start a new discussion instead.