This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

java.util.Date to java.sql.Date

 
Rishi Upadhyay
Greenhorn
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3340
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can construct a new java.sql.Date object from the getTime() method of java.util.Date.

For your example

Hope this helps
 
Rishi Upadhyay
Greenhorn
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the input Carl.Its working fine now and I can insert records in the database.
 
ammuswar kumar
Greenhorn
Posts: 27
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1141
1
Java Oracle
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic