Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rating: Thread Rating: 3 votes, 4.67 average.
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-19-2008, 10:02 PM   PM User | #1
damo.gets
New Coder

 
Join Date: Aug 2008
Location: Bismarck, ND, USA
Posts: 17
Thanks: 1
Thanked 0 Times in 0 Posts
damo.gets is an unknown quantity at this point
Question Why is java.sql.PreparedStatement x .setDate() method not working in my bean?

Alright, one more. Resolving this issue should have me compiling and tracking down my logic errors; it'll be nice to be beyond the small issues that I'm having lately preventing me from getting this project compiled to a workable .war file.

As you can see in the following snippet, I am creating the following java.sql PreparedStatement object in order to execute a query and thus retrieve my data. In this PreparedStatement, I need to set 4 parameters: 2 integers and 2 different java.util.Date objects which will be used to retrieve the applicable records. Here is the applicable code with how I'm attempting to do this:

(my bean class encloses, of course)

Code:
private ResultSet pollPunches() {
        ...
        startDate = new Date(Year, Month, 1, 0, 0, 0);
        endDate = new Date(Year, Month, 31, 23, 59, 59);
        //of course the above endDate is actually calculated with a switch/
        //case construct to test for a 31/30 day month or a 28/29 day February
        //in case of leap year
        ...

        try {
          //general database connection setup
          ...
          //setting of String sql = "MY SQL SELECT WHERE ouah = ? . . .";
          ...
          PreparedStatement stmt = myConnection.prepareStatement(sql);
          stmt.setInt(1, x);    //no error
          stmt.setInt(2, y);    //no error again
          stmt.setDate(3, startDate);   //problem as described below
          stmt.setDate(4, endDate);     //ditto

          ResultSet rs = stmt.executeQuery();

          return rs;
}
Here's the problem from the 2 stmt.setDate() lines:
---
cannot find symbol
symbol : method setDate(int, java.util.Date)
location : interface java.sql.PreparedStatement

---
And that's all that NetBeans is telling me.

The 'ant' output doesn't seem to tell anything more than what NetBeans is spewing at me, either.

TIA

-=-=-=-
Damon Getsman
http://www.ITRx-ND.com/
-=-=-=-
damo.gets is offline   Reply With Quote
Old 12-22-2008, 04:24 PM   PM User | #2
damo.gets
New Coder

 
Join Date: Aug 2008
Location: Bismarck, ND, USA
Posts: 17
Thanks: 1
Thanked 0 Times in 0 Posts
damo.gets is an unknown quantity at this point
Thumbs up

Well, after more in-depth digging that I had the time to do after this last weekend, I have discovered the error of my ways in this issue, as well. I didn't understand the APIs thoroughly enough on my first time through, evidently.

java.sql.PreparedStatement x.setDate() expect to receive a long data type, not a java.util.Date object. This can be achieved with the following call:

Code:
try {
  Class.forName(dbdriver);
  Connection con = DriverManager.getConnection(dburl, dbuser, dbpw);

  String sql = "SQL QUERY/STATEMENT HERE WHERE param = ?";
  PreparedStatement stmt = con.prepareStatement(sql);
  stmt.setDate(1, new java.sql.Date(myDate.getTime()));
  //in the above line, myDate is java.util.Date object

  ResultSet rs = stmt.executeQuery();

  return rs;
} catch (Exception exception) {
  //blah blah blah
  return null;
}
Again, hope this might help someone out in the future.

-=-=-=-=-
Damon Getsman
http://www.ITRx-ND.com/
Programmer/IT Customer Relations/Sysadmin
-=-=-=-=-
damo.gets is offline   Reply With Quote
Reply

Bookmarks

Tags
bean, date, java.sql, preparedstatement, sql

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 03:13 AM.


Advertisement
Log in to turn off these ads.