File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JDBC and the fly likes testing my java DAO  insert method using GregorianCalendar instead of java.sql.Date? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "testing my java DAO  insert method using GregorianCalendar instead of java.sql.Date?" Watch "testing my java DAO  insert method using GregorianCalendar instead of java.sql.Date?" New topic
Author

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

Jay Peigh
Greenhorn

Joined: Feb 15, 2007
Posts: 13
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
internet detective
Marshal

Joined: May 26, 2003
Posts: 30050
    
149

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.)


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Jay Peigh
Greenhorn

Joined: Feb 15, 2007
Posts: 13
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
internet detective
Marshal

Joined: May 26, 2003
Posts: 30050
    
149

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: testing my java DAO insert method using GregorianCalendar instead of java.sql.Date?
 
Similar Threads
Help with MS Access & Date
Question for insertint to Date field.
Passing java.sql.date to constructors
Gregorian roll() function- April Fools Bug??
formatting a SQL date in MS Access