1,105,263 Community Members

insert date in to mysql database through jsp

Member Avatar
anand01
Posting Whiz
301 posts since Aug 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 23 [?]
Skill Endorsements: 0 [?]
 
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 ..

Member Avatar
anand01
Posting Whiz
301 posts since Aug 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 23 [?]
Skill Endorsements: 0 [?]
 
0
 

anyone pls help me

Member Avatar
javaAddict
Nearly a Senior Poster
3,274 posts since Dec 2007
Reputation Points: 900 [?]
Q&As Helped to Solve: 448 [?]
Skill Endorsements: 8 [?]
Team Colleague
Featured
 
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?

Member Avatar
anand01
Posting Whiz
301 posts since Aug 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 23 [?]
Skill Endorsements: 0 [?]
 
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());
}
Member Avatar
masijade
Industrious Poster
4,165 posts since Feb 2006
Reputation Points: 1,351 [?]
Q&As Helped to Solve: 510 [?]
Skill Endorsements: 22 [?]
Team Colleague
Featured
 
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.

Member Avatar
IIM
Master Poster
742 posts since Jun 2011
Reputation Points: 163 [?]
Q&As Helped to Solve: 169 [?]
Skill Endorsements: 24 [?]
 
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>
Member Avatar
anand01
Posting Whiz
301 posts since Aug 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 23 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
IIM
Master Poster
742 posts since Jun 2011
Reputation Points: 163 [?]
Q&As Helped to Solve: 169 [?]
Skill Endorsements: 24 [?]
 
0
 

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

Question Answered as of 2 Years Ago by IIM, masijade and javaAddict
Member Avatar
masijade
Industrious Poster
4,165 posts since Feb 2006
Reputation Points: 1,351 [?]
Q&As Helped to Solve: 510 [?]
Skill Endorsements: 22 [?]
Team Colleague
Featured
 
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.

Member Avatar
anand01
Posting Whiz
301 posts since Aug 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 23 [?]
Skill Endorsements: 0 [?]
 
0
 

Thanks masijade

Member Avatar
IIM
Master Poster
742 posts since Jun 2011
Reputation Points: 163 [?]
Q&As Helped to Solve: 169 [?]
Skill Endorsements: 24 [?]
 
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>
Member Avatar
masijade
Industrious Poster
4,165 posts since Feb 2006
Reputation Points: 1,351 [?]
Q&As Helped to Solve: 510 [?]
Skill Endorsements: 22 [?]
Team Colleague
Featured
 
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.

You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article