Win a copy of Pragmatic AI this week in the Artificial Intelligence forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

java.util.Date to java.sql.Date  RSS feed

 
Greenhorn
Posts: 6
  • 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)
 
Sheriff
Posts: 3341
  • 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
  • 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.
 
Greenhorn
Posts: 27
  • 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
 
Greenhorn
Posts: 1
  • 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_______________________________________________________________
 
Ranch Hand
Posts: 1141
1
Java Oracle
  • 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.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!