1.11M Members

insert date in to mysql database through jsp

 
0
 

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 ..

 
0
 

anyone pls help me

 
0
 

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?

 
0
 
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=null;
    try{
    query ="INSERT INTO `krishna`.`events_and_holidays` (`date`, `type`, `description`) " +
            "VALUES " +
            "(?,?,?)";
    pstm = con.prepareStatement(query);
    pstm.setDate(1, date);
    pstm.setString(2,str_type);
    pstm.setString(3,str_eventholiday);
    pstm.executeUpdate();
}
catch (SQLException e) {
    //e.printStackTrace();
    System.out.println(e.getMessage());
}
 
0
 

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.

 
0
 
<%@ 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
<html>
<head>
<title>Database</title>
</head>
<body>
<%
 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(?)"	;
try{
	Class.forName(driverClass)	;//to use jdbc driver to connect to mysql database
	con=DriverManager.getConnection(connectionString,username,password);//to make connection
	stmt=con.prepareStatement(query);

 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 {
	out.println("Error");
}

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

%>
</body>
</html>
 
0
 

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

 
0
 

If your problem is solved....mark the thread as solved....

Question Answered as of 2 Years Ago by IIM, masijade and javaAddict
 
0
 

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.

 
0
 

Thanks masijade

 
0
 

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

<%@ page import="java.io.*,java.util.*,java.sql.*"%>
<%@ page import="javax.servlet.http.*,javax.servlet.*" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql"%>
 
<html>
<head>
<title>JINSERT Operation</title>
</head>
<body>
 
<sql:setDataSource var="snapshot" driver="com.mysql.jdbc.Driver"
     url="jdbc:mysql://localhost/TEST"
     user="root"  password="pass123"/>


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

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.

 
0
 

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.

You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: