Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Aug 2008
    Location
    Bismarck, ND, USA
    Posts
    17
    Thanks
    1
    Thanked 0 Times in 0 Posts

    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/
    -=-=-=-

  • #2
    New Coder
    Join Date
    Aug 2008
    Location
    Bismarck, ND, USA
    Posts
    17
    Thanks
    1
    Thanked 0 Times in 0 Posts

    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
    -=-=-=-=-


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •