• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Jay Peigh
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • 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
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34695
367
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • 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
  • 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
Marshal
Posts: 34695
367
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic