?
Solved

Incrementing a Date....

Posted on 2009-04-22
73
Medium Priority
?
973 Views
Last Modified: 2012-05-06
I am trying to write a method that gets a date from a database, increments that date by two days, and then puts the new date into the database as a duedate. I attempt this by turning a string into a date, then turning the date into a calendar type. I increment the calendar type, and then turn it back into a string for insertion into the database; however, I keep getting exceptions thrown., particularly parse exceptions. What am I doing wrong?
0
Comment
Question by:AlexanderMaxwell
  • 31
  • 22
  • 16
  • +2
73 Comments
 

Author Comment

by:AlexanderMaxwell
ID: 24201954


   public boolean dueDate(int movieID) {
        SimpleDateFormat Date = new SimpleDateFormat ("MM/dd/yyyy");
        SimpleDateFormat Time = new SimpleDateFormat ("K:mm");
        String date = "";
        String time="";
        String MovieIDHold="";
        MovieIDHold=Integer.toString(movieID);
        returnStatus = false;
        String dueDate="";
        time="08:00";
 
        sqlData = "SELECT orderDate FROM Order WHERE Order.movieID='" + movieID + "' AND closedOrderDate IS NULL";
        try {
            Class.forName("sun.jdbc.odbc.jdbcOdcbDriver");
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(actions.class.getName()).log(Level.SEVERE, null, ex);
        }
 
        try {
 
            Connection connection = DriverManager.getConnection(data, "", "");
            Statement statement = connection.createStatement();
            ResultSet record = statement.executeQuery(sqlData);
            record.next();
//retrieve info from database
            dueDate =record.getString("orderDate");
 
        } catch (SQLException s) {
            System.out.println("SQL Error:" + s.toString() + "" + s.getErrorCode() + "" + s.getSQLState());
        }
//turn date into calendar
        java.util.Date DateHold = null;
        try {
            DateHold = (java.util.Date) Date.parse(dueDate);
 
        } catch (ParseException ex) {
            Logger.getLogger(actions.class.getName()).log(Level.SEVERE, null, ex);
        }
        Calendar c1 = Calendar.getInstance();
        c1.setTime(DateHold);
 
        c1.add(Calendar.DATE, 2);
//turn into string
 
        date = Date.format(c1.getTime());
     
        try {
            Class.forName("sun.jdbc.odbc.jdbcOdcbDriver");
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(actions.class.getName()).log(Level.SEVERE, null, ex);
        }
 
        try {
            //update database with duedate information
            sqlData = "UPDATE Order, SET dueOrderDate=? AND dueOrderTime=? WHERE Order.movieID=? AND closedOrderDate IS NULL";
 
            Connection connection = DriverManager.getConnection(data, "", "");
            PreparedStatement update = connection.prepareStatement(sqlData);
            update.setString(1, date);
            update.setString(2, time);
            update.setString(3, MovieIDHold);
            update.executeUpdate();
            update.close();
            returnStatus =true;
        } catch (SQLException s) {
            System.out.println("SQL Error:" + s.toString() + "" + s.getErrorCode() + "" + s.getSQLState());
        }
 
        return returnStatus;
    }

Open in new window

0
 
LVL 92

Expert Comment

by:objects
ID: 24201964
no need to go to string, instead use Calendar class to add days

http://helpdesk.objects.com.au/java/how-do-i-add-a-specified-number-of-hours-to-a-java-date

(Replace Calendar.HOUR with Calendar.DAY for days)

0
 
LVL 26

Expert Comment

by:ksivananth
ID: 24201965
post the exception stack and which line you get that exception!
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 92

Expert Comment

by:objects
ID: 24201968
sorry misread your code. So is the date stored in the database as a string?

0
 
LVL 92

Expert Comment

by:objects
ID: 24201976
check the format of your dates in the database (print out the date after you extract from db)

0
 

Author Comment

by:AlexanderMaxwell
ID: 24201986
No, it is stored as a Date/Time, but I insert it as a string and  let the database program convert them automatically into the correct format.  I'm working with MS Access.
0
 
LVL 92

Expert Comment

by:objects
ID: 24202054
try using getTimestamp() instead to get it from db (and setTimestamp() to insert)]
0
 

Author Comment

by:AlexanderMaxwell
ID: 24202060
How would I increment Timestamps?
0
 
LVL 92

Expert Comment

by:objects
ID: 24202070
same way you are already (it a Date subclass)

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24202075
You need to use getDate. Try to work in Date throughout or restructure your data to amalgamate the date with the (probably redundant) time columns
0
 

Author Comment

by:AlexanderMaxwell
ID: 24203838
Access doesn't like setDate, I"m not sure if it will like getDate.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24203863
>>Access doesn't like setDate

What makes you think that
0
 

Author Comment

by:AlexanderMaxwell
ID: 24203874
perhaps I'm not using it right, but when I do use it, it never updates the database, but I'm giving it another shot as we speak.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24203884
You need to use a type of java,sql.Date as the parameter
0
 

Author Comment

by:AlexanderMaxwell
ID: 24203897
I did previously, I changed from java.sql.Date to Strings.
0
 
LVL 20

Expert Comment

by:Venabili
ID: 24204839
>>I did previously, I changed from java.sql.Date to Strings.
Why? You will need to convert them to Dates if you  want to increment...
You do not use strings when you want to pass a number, why use it when you want to pass Dates?
Just use the proper types (or convert them) and use the advices given above
0
 

Author Comment

by:AlexanderMaxwell
ID: 24204979
I have. I started out with the correct types, but they wouldn't update the database. In a separate project I was able to make it work with strings, so I was trying to do the same here. Now I'm changing it back to the regular date types to see if I can make it work. I got the idea for using strings for access from Java in 21 days if you're wondering. What I was doing originally was getting the information from the database, then turning it into a date, then incrementing it, and then turning it back into a string, and attempting to update. Now I am using .getDate to retrieve the information, I increment by using the calendar, but I'm still trying to update as a string--this I'm changing as we speak. I will post the code when I finish.
0
 
LVL 20

Expert Comment

by:Venabili
ID: 24205068
Again - then just convert from String to Date, increment using the Date object and convert back to String. :) But I would be using Dates and converting them to Strings where needed (using SimpleDateFormat for the formatting). Just my 2 cents :)
0
 

Author Comment

by:AlexanderMaxwell
ID: 24205310
I hate to be a total amateur...but I am. How would I use the date object to increment? I am using the java.sql.Date, not the java.util.Date.
0
 
LVL 20

Expert Comment

by:Venabili
ID: 24205546
The Calendar object can be set using setTimeInMillis;
java.sql.Date has a getTime method which return exactly this as does the java.util.Date one (the sql one extends the util one)
Just look in the APIs :)
0
 

Author Comment

by:AlexanderMaxwell
ID: 24205979
Do you think this would work for incrementing a date using the Date object?

orderDate.setDate(    (orderDate.getDate() + 2)    );
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 500 total points
ID: 24206315
>>Do you think this would work for incrementing a date using the Date object?

No. Use Calendar, or at a pinch
Date twoDaysLater = new Date(origDate.getTime() + (1000*60*60*24*2));

Open in new window

0
 

Author Comment

by:AlexanderMaxwell
ID: 24206565
What's the 1000 for, wouldn't two days be just  60*60*24*2?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24206648
Nope - Java time is measured in milliseconds
0
 

Author Comment

by:AlexanderMaxwell
ID: 24209676
ok, this is what I have so far. However, the

"dueDate = new java.sql.Date(orderDate.getTime()+(1000*60*60*24*2));" line throws a null pointer exception










public boolean dueDate(int movieID) {


returnStatus=false;
SimpleDateFormat dateFormat = new SimpleDateFormat ("MM/dd/yyyy");
SimpleDateFormat timeFormat = new SimpleDateFormat ("K:mm");
SimpleDateFormat sqlFormat = new SimpleDateFormat ("yyyy-MM-dd");

java.sql.Date orderDate=null;
java.sql.Date dueDate=null;

String time="08:00";
String MovieIDString=Integer.toString(movieID);
String getDate="";
String getDueDate="";//get date from database
sqlData ="SELECT orderDate FROM [Order] WHERE [Order].movieID="+ movieID +" AND closeOrderDate IS NULL";


try {
Class.forName("sun.jdbc.odbc.jdbcOdcbDriver");
} catch (ClassNotFoundException ex) {
Logger.getLogger(actions.class.getName()).log(Level.SEVERE, null, ex);
}

try {
Connection connection = DriverManager.getConnection(data, "", "");
Statement statement = connection.createStatement();
ResultSet record = statement.executeQuery(sqlData);
record.next();
getDate = record.getString("orderDate");

} catch (SQLException s) {
System.out.println("SQL Error:" + s.toString() + "" + s.getErrorCode() + "" + s.getSQLState());
}
try {
orderDate = new java.sql.Date(dateFormat.parse(getDate).getTime());
} catch (ParseException ex) {

Logger.getLogger(actions.class.getName()).log(Level.SEVERE, null, ex);
}

dueDate = new java.sql.Date(orderDate.getTime()+(1000*60*60*24*2));
getDueDate=dateFormat.format(dueDate);


try {
Class.forName("sun.jdbc.odbc.jdbcOdcbDriver");
} catch (ClassNotFoundException ex) {
Logger.getLogger(actions.class.getName()).log(Level.SEVERE, null, ex);
}

try {
sqlData = "UPDATE [Order] SET dueOrderDate=? AND dueOrderTime=? WHERE [Order].movieID=? AND closeOrderDate IS NULL";

Connection connection = DriverManager.getConnection(data, "", "");
PreparedStatement update = connection.prepareStatement(sqlData);
update.setString(1, getDueDate);

update.setString(2, time);
update.setString(3, MovieIDString);
update.executeUpdate();
update.close();
returnStatus =true;
} catch (SQLException s) {
System.out.println("SQL Error:" + s.toString() + "" + s.getErrorCode() + "" + s.getSQLState());
}

 return returnStatus;
}
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24209735
You need to do:
if (record.next()) {
   // Proceed
}
else {
   // No records found
}

Open in new window

0
 

Author Comment

by:AlexanderMaxwell
ID: 24210130
it compiles fine, but it doesn't update the database, any ideas on what I did wrong?
0
 
LVL 92

Expert Comment

by:objects
ID: 24210141
what did you change your code for incrementing the date?  What you already has was fine.

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24210152
Make sure you close the connection
0
 

Author Comment

by:AlexanderMaxwell
ID: 24210172
the original code I posted threw some sort of exception at c1.setTime(), so I rewrote it.
0
 
LVL 92

Expert Comment

by:objects
ID: 24210187
you code should look more like this:

      public boolean dueDate(int movieID) {

            returnStatus = false;
            SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
            SimpleDateFormat timeFormat = new SimpleDateFormat("K:mm");
            SimpleDateFormat sqlFormat = new SimpleDateFormat("yyyy-MM-dd");

            java.sql.Date orderDate = null;
            java.sql.Date dueDate = null;

            String time = "08:00";
            String MovieIDString = Integer.toString(movieID);
            String getDate = "";
            Date getDueDate = null;// get date from database
            sqlData = "SELECT orderDate FROM [Order] WHERE [Order].movieID="
                        + movieID + " AND closeOrderDate IS NULL";

            try {
                  Class.forName("sun.jdbc.odbc.jdbcOdcbDriver");
            } catch (ClassNotFoundException ex) {
                  Logger.getLogger(actions.class.getName()).log(Level.SEVERE, null,
                              ex);
            }

            try {
                  Connection connection = DriverManager.getConnection(data, "", "");
                  Statement statement = connection.createStatement();
                  ResultSet record = statement.executeQuery(sqlData);
                  if (record.next()) {
                        orderDate = record.getDate("orderDate");
                  }

            } catch (SQLException s) {
                  System.out.println("SQL Error:" + s.toString() + ""
                              + s.getErrorCode() + "" + s.getSQLState());
            }
            Calendar cal = Calendar.getInstance();
            cal.setTime(orderDate);
            cal.add(Calendar.DAY_OF_YEAR, 2);
            getDueDate = cal.getTime();
            
            try {
                  Class.forName("sun.jdbc.odbc.jdbcOdcbDriver");
            } catch (ClassNotFoundException ex) {
                  Logger.getLogger(actions.class.getName()).log(Level.SEVERE, null,
                              ex);
            }

            try {
                  sqlData = "UPDATE [Order] SET dueOrderDate=? AND dueOrderTime=? WHERE [Order].movieID=? AND closeOrderDate IS NULL";

                  Connection connection = DriverManager.getConnection(data, "", "");
                  PreparedStatement update = connection.prepareStatement(sqlData);
                  update.setDate(1, new java.sql.Date(getDueDate.getTime()));

                  update.setString(2, time);
                  update.setString(3, MovieIDString);
                  update.executeUpdate();
                  update.close();
                  returnStatus = true;
            } catch (SQLException s) {
                  System.out.println("SQL Error:" + s.toString() + ""
                              + s.getErrorCode() + "" + s.getSQLState());
            }

            return returnStatus;
      }

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24210215
Use the following (see comments made by me and Venabili earlier)
update.setDate(1, dueDate);
update.setString(2, time);
update.setString(3, MovieIDString);

Open in new window

0
 

Author Comment

by:AlexanderMaxwell
ID: 24210564
objects,


that code throws a null pointer at

cal.setTime(orderDate);

that's the same problem I had in the beginning. The code posted runs fine with the debugger, but then it goes crazy without.



java.lang.NullPointerException
        at java.util.Calendar.setTime(Calendar.java:1075)
        at classes.actions.dueDate(actions.java:287)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24210585
My comment 'Proceed' at http:#24209735 means of course you can NOT proceed with the update if you haven't obtained anything from the ResultSet
0
 

Author Comment

by:AlexanderMaxwell
ID: 24210721
my version of the code returns a false if there isn't any data in the ResultSet. Perhaps I should break this method up to located the errors.
0
 
LVL 92

Expert Comment

by:objects
ID: 24210767
That error is because no row was found in the database
change the code to:

                  if (record.next()) {
                        orderDate = record.getDate("orderDate");
                  } else {
                       return false;
                  }


0
 

Author Comment

by:AlexanderMaxwell
ID: 24210951
(First of all, thanks for all of the help I've received so far. You guys are great.)




It doesn't throw a null pointer anymore, but it still doesn't do anything--unless ran with the debugger. I guess the problem with the code is that I'm not getting anything from the data base since it returns false at that point.
0
 
LVL 92

Expert Comment

by:objects
ID: 24210962
thats correct, you need to give it a movieid that has a null closeOrderDate
0
 

Author Comment

by:AlexanderMaxwell
ID: 24211250
ok, I broke it up into smaller pieces . I have three methods now. One to get original date from the database. One to increment the date; and one to update the database. I found the problem in the second method--this is the method that increments the orderDate.
//First Method:this method returns a order where closeOrderDate is null
 
 
public String getOrderDate(int movieID){
String orderDate="";
 
 
 
sqlData = "SELECT orderDate FROM [Order] WHERE [Order].movieID=" + movieID + " AND closeOrderDate IS NULL";
 
        try {
            Class.forName("sun.jdbc.odbc.jdbcOdcbDriver");
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(actions.class.getName()).log(Level.SEVERE, null,
                    ex);
        }
 
        try {
            Connection connection = DriverManager.getConnection(data, "", "");
            Statement statement = connection.createStatement();
            ResultSet record = statement.executeQuery(sqlData);
            if (record.next()) {
                orderDate = record.getString(1);
            } else {
                return "ERROR: No DATE Returned";
 
            }
            record.close();
            connection.close();
 
 
        } catch (SQLException s) {
            System.out.println("SQL Error:" + s.toString() + "" + s.getErrorCode() + "" + s.getSQLState());
        }
 
 
return orderDate;
 
 
 
 
//this method returns:     "2009-04-22 00:00:00"
 
 
 
 
 
 
 
//Method Two: this method turns this string into a date, increments //it, and turns it back into a string
 
 
public String setDueDate(String orderDate){
String dueDate="";
 
 
        SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
        SimpleDateFormat timeFormat = new SimpleDateFormat("K:mm");
        SimpleDateFormat sqlFormat = new SimpleDateFormat("yyyy-MM-dd");
        java.sql.Date orderDateDate=null;
        java.sql.Date dueDateDate=null;
        try {
            orderDateDate = (java.sql.Date) sqlFormat.parse(dueDate);
 
        } catch (ParseException ex) {
            Logger.getLogger(actions.class.getName()).log(Level.SEVERE, null, ex);
        }
        dueDateDate= new java.sql.Date(orderDateDate.getTime()+1000*60*60*24*2);
        dueDate = dateFormat.format(dueDateDate);
 
return dueDate;
 
 
 
 
//This method has a null pointer exception

Open in new window

0
 

Author Comment

by:AlexanderMaxwell
ID: 24211262
nullpointer on the line "dueDateDate = new java.sql.Date(orderDateDate.getTime() + 1000 * 60 * 60 * 24 * 2);"

This is the exact line where the null pointer is thrown.
0
 
LVL 92

Expert Comment

by:objects
ID: 24211264
you got rid of the changes I posted above, any reason you keep getting it as a string and parsing it?

0
 
LVL 92

Expert Comment

by:objects
ID: 24211275
to get the date from database you can use this:

      public Date getOrderDate(int movieID){
      Date orderDate=null;
      
      
      
      sqlData = "SELECT orderDate FROM [Order] WHERE [Order].movieID=" + movieID + " AND closeOrderDate IS NULL";
      
              try {
                  Class.forName("sun.jdbc.odbc.jdbcOdcbDriver");
              } catch (ClassNotFoundException ex) {
                  Logger.getLogger(actions.class.getName()).log(Level.SEVERE, null,
                          ex);
              }
      
              try {
                  Connection connection = DriverManager.getConnection(data, "", "");
                  Statement statement = connection.createStatement();
                  ResultSet record = statement.executeQuery(sqlData);
                  if (record.next()) {
                      orderDate = record.getDate(1);
                  }
                  record.close();
                  connection.close();
      
      
              } catch (SQLException s) {
                  System.out.println("SQL Error:" + s.toString() + "" + s.getErrorCode() + "" + s.getSQLState());
              }
      
      
      return orderDate;
}

0
 
LVL 92

Assisted Solution

by:objects
objects earned 500 total points
ID: 24211291
to get the due date you just need the following

      public Date setDueDate(Date orderDate){
          if (orderDate==null) return null;
        Calendar cal = Calendar.getInstance();
        cal.setTime(orderDate);
        cal.add(Calendar.DAY_OF_YEAR, 2);
        return cal.getTime();

      }

0
 

Author Comment

by:AlexanderMaxwell
ID: 24211484
Whoa, I wasn't expecting this much feedback in such a short period of time. To address Objects, to be honest with you I don't know why I changed it back. I'm still a novice, so it was a stupid mistake based on impulse. I guess I should work with the standard types.I'll come back when I've had time to check them out.
0
 
LVL 92

Expert Comment

by:objects
ID: 24211506
don't worry, follow my instructions and we'll get it working :)

0
 

Author Comment

by:AlexanderMaxwell
ID: 24211645
this is the third method...I'll try it out in just a moment.

 
public boolean setDueDate(int movieID, java.sql.Date Date){
   String time="08:00";
   returnStatus=false;
  try {
  Class.forName("sun.jdbc.odbc.jdbcOdcbDriver");
      } catch (ClassNotFoundException ex) {
      Logger.getLogger(actions.class.getName()).log(Level.SEVERE, null,ex);
        }
 
        try {
 sqlData = "UPDATE [Order] SET dueOrderDate=?, dueOrderTime=? WHERE [Order].movieID=? AND closeOrderDate IS NULL";
 
 
            Connection connection = DriverManager.getConnection(data, "", "");
            PreparedStatement update = connection.prepareStatement(sqlData);
            update.setDate(1, Date);
            update.setString(2,time);
            update.setInt(3, movieID);
            update.executeUpdate();
            update.close();
            connection.close();
 
 
            returnStatus = true;
        } catch (SQLException s) {
            System.out.println("SQL Error:" + s.toString() + "" + s.getErrorCode() + "" + s.getSQLState());
        }
 
        return returnStatus;
 
 
 
}

Open in new window

0
 

Author Comment

by:AlexanderMaxwell
ID: 24211753
I'm getting this error in with the second method.



java.lang.ClassCastException: java.util.Date cannot be cast to java.sql.Date
0
 

Author Comment

by:AlexanderMaxwell
ID: 24211764
I'm thinking that the calendar object is having trouble becoming a java.sql.date object. Should I try using strings again?
0
 
LVL 92

Expert Comment

by:objects
ID: 24211790
better to use the following
 
public boolean setDueDate(int movieID, Date date){
   String time="08:00";
   returnStatus=false;
  try {
  Class.forName("sun.jdbc.odbc.jdbcOdcbDriver");
      } catch (ClassNotFoundException ex) {
      Logger.getLogger(actions.class.getName()).log(Level.SEVERE, null,ex);
        }
 
        try {
 sqlData = "UPDATE [Order] SET dueOrderDate=?, dueOrderTime=? WHERE [Order].movieID=? AND closeOrderDate IS NULL";
 
 
            Connection connection = DriverManager.getConnection(data, "", "");
            PreparedStatement update = connection.prepareStatement(sqlData);
            update.setDate(1, new java.sql.Date(date.getTime()));
            update.setString(2,time);
            update.setInt(3, movieID);
            update.executeUpdate();
            update.close();
            connection.close();
 
 
            returnStatus = true;
        } catch (SQLException s) {
            System.out.println("SQL Error:" + s.toString() + "" + s.getErrorCode() + "" + s.getSQLState());
        }
 
        return returnStatus;
}
0
 

Author Comment

by:AlexanderMaxwell
ID: 24211841
But what about the second method, that's where the error is coming from? Or is it?
0
 
LVL 92

Expert Comment

by:objects
ID: 24211943
did those changes fix the problem?
0
 

Author Comment

by:AlexanderMaxwell
ID: 24213209
I haven't got as far as the third method, I'm still working on the second method.
0
 
LVL 92

Expert Comment

by:objects
ID: 24213226
are you using the 2nd method I posted?  the 2nd method code you had won't work

0
 

Author Comment

by:AlexanderMaxwell
ID: 24213248
I'm using this code. but I keep getting:

java.lang.ClassCastException: java.util.Date cannot be cast to java.sql.Date
public java.sql.Date getDueDate(java.sql.Date orderDate){
          if (orderDate==null) return null;
        Calendar cal = Calendar.getInstance();
        cal.setTime(orderDate);
        cal.add(Calendar.DAY_OF_YEAR, 2);
        return (java.sql.Date) cal.getTime();
 
      }

Open in new window

0
 
LVL 92

Expert Comment

by:objects
ID: 24213254
>         return (java.sql.Date) cal.getTime();

don't do that, should be just:

        return cal.getTime();

0
 

Author Comment

by:AlexanderMaxwell
ID: 24213263
netbeans gives errors with that code. It says that return cal.getTime(); has to be casted into something.
0
 
LVL 92

Expert Comment

by:objects
ID: 24213278
> public java.sql.Date getDueDate(java.sql.Date orderDate){

thats been changed too, should just be:

public Date getDueDate(Date orderDate){

0
 

Author Comment

by:AlexanderMaxwell
ID: 24213318
I still have the casting problem. Here's what I have so far.
method one:
 
 
 
 
 
 
 
 
 
public Date getOrderDate(int movieID){
      Date orderDate=null;
 
      sqlData = "SELECT orderDate FROM [Order] WHERE [Order].movieID=" + movieID + " AND closeOrderDate IS NULL";
              try {
                  Class.forName("sun.jdbc.odbc.jdbcOdcbDriver");
              } catch (ClassNotFoundException ex) {
                  Logger.getLogger(actions.class.getName()).log(Level.SEVERE, null,
                          ex);
              }
     
              try {
                  Connection connection = DriverManager.getConnection(data, "", "");
                  Statement statement = connection.createStatement();
                  ResultSet record = statement.executeQuery(sqlData);
                  if (record.next()) {
                      orderDate = record.getDate(1);
                  }
                  record.close();
                  connection.close();
     
     
              } catch (SQLException s) {
                  System.out.println("SQL Error:" + s.toString() + "" + s.getErrorCode() + "" + s.getSQLState());
              }
     
     
       return orderDate;
 
 
 
 
 
method two:
 
 
 
 
 
public Date getDueDate(Date orderDate){
          if (orderDate==null) return null;
        Calendar cal = Calendar.getInstance();
        cal.setTime(orderDate);
        cal.add(Calendar.DAY_OF_YEAR, 2);
        return  cal.getTime();
 
      }

Open in new window

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24213334
You'll make your life a lot simpler if you just use java.sql.Date throughout and get rid of the Calendar - it's not necessary
0
 
LVL 92

Expert Comment

by:objects
ID: 24213348
can you post the stack trace for the exception

0
 

Author Comment

by:AlexanderMaxwell
ID: 24213351
that's true, but when I try increment a date, it throws a null pointer exception at

dueDateDate = new java.sql.Date(orderDateDate.getTime() + 1000 * 60 * 60 * 24 * 2);"
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24213369
>>that's true, but when I try increment a date, it throws a null pointer exception at

That's only because you had no 'orderDateDate' to add to. You need to make sure you do have one before you proceed, as i said earlier
0
 
LVL 92

Expert Comment

by:objects
ID: 24213375
> that's true, but when I try increment a date, it throws a null pointer exception at

Its actually *not* true. You're already using java.sql.Date, and the Calendar is the statndard/preferred way of doing what you are doing.
0
 
LVL 92

Expert Comment

by:objects
ID: 24213380
post the stack trace and I can easily fix your current problem, saves you wasting a lot of time. Its currently very close to working.

0
 

Author Comment

by:AlexanderMaxwell
ID: 24213410
at the time, I did have a value to add to, the value produced by the first method, which was enter into the second method as a parameter.
0
 
LVL 92

Expert Comment

by:objects
ID: 24213419
you can print a stack trace with:

ex.printStackTrace();

0
 

Author Comment

by:AlexanderMaxwell
ID: 24213462
I'll get that to you in a few hours if you're still online, I'm getting a few hours of some sleep for now, but thanks for your help.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24213463
This is all you need (i think you have the update bit working)
    public java.sql.Date getOrderDate(int movieID) {
	java.sql.Date result = null;
	Connection connection = null;
	Statement statement = null;
	ResultSet record = null;
	    final String sqlData = "SELECT orderDate FROM Order WHERE Order.movieID = ? AND closedOrderDate IS NULL";
 
	try {
 
	    Class.forName("sun.jdbc.odbc.jdbcOdcbDriver");
	    //TODO
	    String data = "";
	    connection = DriverManager.getConnection(data, "", "");
	    statement = connection.createStatement();
	    record = statement.executeQuery(sqlData);
	    if (record.next()) {
		//retrieve info from database
		result =record.getDate("orderDate");
	    }
	} catch (ClassNotFoundException ex) {
	    Logger.getLogger(getClass().getName()).log(Level.SEVERE, null, ex);
	} catch (SQLException s) {
	    Logger.getLogger(getClass().getName()).log(Level.SEVERE, null, s);
	    System.out.println("SQL Error:" + s.toString() + "" + s.getErrorCode() + "" + s.getSQLState());
 
	}
	finally {
	    try { record.close(); } catch (SQLException e) { /* ignore */ }
	    try { statement.close(); } catch (SQLException e) { /* ignore */ }
	    try { connection.close(); } catch (SQLException e) { /* ignore */ }
	}
	return result;
    }
 
    public java.sql.Date getDueDate(java.sql.Date d) {
	return new java.sql.Date(d.getTime() + 1000 * 60 * 60 * 24 * 2);
    }
 
    public void updateMovie(int id) {
	java.sql.Date orderDate = getOrderDate(id);
	if (orderDate == null) {
	    // Problem - no records
	}
	else {
	    java.sql.Date dueDate = getDueDate(orderDate);
	    // Now do upate
	}
    }

Open in new window

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24213481
Oops i forgot to do the PreparedStatement.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24213495
Very little difference actually:
    public java.sql.Date getOrderDate(int movieID) {
        java.sql.Date result = null;
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet record = null;
            final String sqlData = "SELECT orderDate FROM Order WHERE Order.movieID = ? AND closedOrderDate IS NULL";
 
        try {
 
            Class.forName("sun.jdbc.odbc.jdbcOdcbDriver");
            //TODO
            String data = ""; 
            connection = DriverManager.getConnection(data, "", "");
            statement = connection.prepareStatement(sqlData);
            statement.setInt(1, movieID);
            record = statement.executeQuery(sqlData);
            if (record.next()) {
                //retrieve info from database
                result =record.getDate("orderDate");
            }
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException s) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, null, s); 
            System.out.println("SQL Error:" + s.toString() + "" + s.getErrorCode() + "" + s.getSQLState());
 
        }
        finally {
            try { record.close(); } catch (SQLException e) { /* ignore */ }
            try { statement.close(); } catch (SQLException e) { /* ignore */ }
            try { connection.close(); } catch (SQLException e) { /* ignore */ }
        }
        return result;
    }   

Open in new window

0
 

Author Comment

by:AlexanderMaxwell
ID: 24215102
CEHJ:



this is what I have so far on your code

Objects: I will send you that stack trace soon.
 public java.sql.Date getOrderDate(int movieID) {
        java.sql.Date result = null;
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet record = null;
         sqlData = "SELECT orderDate FROM Order WHERE Order.movieID = ? AND closedOrderDate IS NULL";
 
        try {
 
            Class.forName("sun.jdbc.odbc.jdbcOdcbDriver");
            //TODO
 
            connection = DriverManager.getConnection(data, "", "");
            statement = connection.prepareStatement(sqlData);
            statement.setInt(1, movieID);
            record = statement.executeQuery(sqlData);
            if (record.next()) {
                //retrieve info from database
                result =record.getDate("orderDate");
            }
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException s) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, null, s);
            System.out.println("SQL Error:" + s.toString() + "" + s.getErrorCode() + "" + s.getSQLState());
 
        }
        finally {
            try { record.close(); } catch (SQLException e) { /* ignore */ }
            try { statement.close(); } catch (SQLException e) { /* ignore */ }
            try { connection.close(); } catch (SQLException e) { /* ignore */ }
        }
        return result;
    }
 
    public java.sql.Date getDueDate(java.sql.Date d) {
	return new java.sql.Date(d.getTime() + 1000 * 60 * 60 * 24 * 2);
    }
 
public boolean updateDueDate(int movieID) {
	java.sql.Date dueDate=null;
   	String time="08:00";
	java.sql.Date orderDate = getOrderDate(movieID);
	if (orderDate == null) {
	    return false;
	}
	else {
	     dueDate = getDueDate(orderDate);
 
	}
	//update database
 
 
 
 
  try {
  Class.forName("sun.jdbc.odbc.jdbcOdcbDriver");
 
      } catch (ClassNotFoundException ex) {
 
      Logger.getLogger(actions.class.getName()).log(Level.SEVERE, null,ex);
 
        }
 
        try {
 sqlData = "UPDATE [Order] SET dueOrderDate=?, dueOrderTime=? WHERE [Order].movieID=? AND closeOrderDate IS NULL";
 
 
 
            Connection connection = DriverManager.getConnection(data, "", "");
 
            PreparedStatement update = connection.prepareStatement(sqlData);
 
            update.setDate(1, dueDate);
 
            update.setString(2,time);
 
            update.setInt(3, movieID);
 
            update.executeUpdate();
 
            update.close();
 
            connection.close();
 
        } catch (SQLException s) {
 
            System.out.println("SQL Error:" + s.toString() + "" + s.getErrorCode() + "" + s.getSQLState());
 
        }
 
        return true;
 
 
    }

Open in new window

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24215138
That looks OK. And what results are you getting with that code?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24226228
:-)
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses
Course of the Month16 days, 13 hours left to enroll

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question