• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

testing my java DAO insert method using GregorianCalendar instead of java.sql.Date?

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

2 questions:

(1)
Just trying to test the insert method within my DAO. the method will insert mostly Strings and Integers, but a few Dates also. I checked the API for java.sql.Date, and it says to pass in a long Date. So I used a millisecond to Date converter and generated a number from a date. However, the compiler rejected the number, as it was too big.

So now, I just passed in a new GregorianCalendar(106, 11, 15).getTime() instead. Will this work?

(2)
I am a bit new to writing DAO classes, so just had a question on testing insert statements:

In order for an insert to work, do the fields in your query statement have to be in the exact same order as in the table? I am not hardcoding in values in the query, as these will come from getter methods from another class.

So, for example, i have: "INSERT INTO MYTABLE (COL1, COL2, COL3)" + "VALUES(?, ?, ?)";


Thanks in advance for any help.

Jay
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jay,
1) That won't compile because you have to pass a java.sql.Date rather than a java.util.Date. Instead, you can write:
new java.util.Date(new GregorianCalendar(106, 11, 15).getTime().getTime())

2) The columns can be in any order as long as you specify the order in your insert statement. (As you did in your example.)
 
Jay Peigh
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the help Jeanne,

I didn't quite understand your explanation on the Date. So I can basically pass a GregorianCalendar object to a Date object?

Thanks,

JP
 
Jeanne Boyarsky
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jay,
Let me break up my really long line of code and add some narration, so it makes sense.

// here we create the calendar object we want to store
Calendar cal = new GregorianCalendar(106, 11, 15);

// this is the date object we want to store
// unfortunately, we can't store it as is
java.util.Date utilDate = cal.getTime();

// now we have the # milliseconds in a non-object type
long time = utilDate.getTime();

// by going through a long, we have the necessary information
// to create a SQL time
java.sql.date sqlDate = new java.sql.Date(time);

And since we can pass the sqlDate to JDBC, we have achieved the needed conversion.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic