aspose file tools*
The moose likes JDBC and the fly likes java.util.Date to java.sql.Date Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "java.util.Date to java.sql.Date" Watch "java.util.Date to java.sql.Date" New topic
Author

java.util.Date to java.sql.Date

Rishi Upadhyay
Greenhorn

Joined: Oct 05, 2000
Posts: 6
I have to insert a date in the database(oracle 8).I am using following peice of code:
if(info.getLastUpdatedTime() == null )
{
System.out.println("if:UPDTime in ejbcreate oucbean....");
pstmt.setDate(7,date);//date is defined as java.sql.Date
}
else
{
System.out.println("else:UPDTime in ejbcreate oucbean....");
pstmt.setDate(7,info.getLastUpdatedTime());
}
The method getLastUpdatedTime() is defined to return a a java.util.Date.But I have to convert it to java.sql.Date before I issue an insert sql statement.
Can anybody give me a peice of code that converts java.util.Date to java.sql.Date.
(weblogic 5.1.0
oracle 8)
Carl Trusiak
Sheriff

Joined: Jun 13, 2000
Posts: 3340
You can construct a new java.sql.Date object from the getTime() method of java.util.Date.

For your example

Hope this helps

I Hope This Helps
Carl Trusiak, SCJP2, SCWCD
Rishi Upadhyay
Greenhorn

Joined: Oct 05, 2000
Posts: 6
Thanks for the input Carl.Its working fine now and I can insert records in the database.
ammuswar kumar
Greenhorn

Joined: Aug 19, 2001
Posts: 27
sir

i have a table in SQL-SERVER named as title
this table has three fields
1.id
2.tdate----this is of the type datetime
3.content
i am using JRUN 3.0
i want to make a java servlet in which u have a text link
Date todaysdate = new Date();
todays content;
In the servlet :
i am accepting this parameter as
String a = request.getParameter("todaysdate");

i will the database connecion---------------------
then
'
select * from title where tdate = 'a';

-----------but this not showing any result
error;
there is something wrong here in he compatitbility of 'a' with tdate
klein bottle
Greenhorn

Joined: Oct 28, 2003
Posts: 1
if using JDBC-ODBC instead of pure JDBC it seems you must explicitly format the date
// How to put a date into a sql database. 29 October 2003
//This is a repost, but theres a lot of suggestions out there,
// This is the one that works (took me half a day to find that out)
// I use sun.jdbc.odbc.JdbcOdbcDriver. (because my JB9 hates pure jdbc)
// This is the ONLY method that works for me
// original: Eddy Young Coding_MU

java.sql.Date sqldate (String dmy ) // dd/MM/yyyy to java.sql.Date
{
SimpleDateFormat dF = new SimpleDateFormat("dd/MM/yyyy");
java.util.Date d;
try {
d = dF.parse(dmy);
dF.applyPattern("yyyy-MM-dd");
dmy = dF.format(d);
} catch (Exception e) {
e.printStackTrace();
}
return java.sql.Date.valueOf(dmy);
} //sqldate_______________________________________________________________
Avi Abrami
Ranch Hand

Joined: Oct 11, 2000
Posts: 1121

If I remember correctly, the JDBC-ODBC bridge driver (in J2SE 1.4.2) supports the SQL escape syntax
Good Luck,
Avi.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: java.util.Date to java.sql.Date
 
Similar Threads
How to write to new table?
Question on ParseException
Problem in getting date from JSP Form
Problem in getting date from JSP Form
Syntax error converting datetime from character string.