Results 1 to 12 of 12
  1. #1
    dmakshay2002 is offline Member
    Join Date
    May 2010
    Posts
    6
    Rep Power
    0

    Default Not able to return the method value

    Hey guys,
    I have a method which returns a calculated value as below

    Java Code:
      private static long getETTTime(long bugID,Connection con){  
               int etttime=0;  
                 
            try{  
                String query = "select max(date_modified) from mantis_bug_history_table where bug_id= ?";  
                PreparedStatement pst = con.prepareStatement(query);  
                pst.setLong(1,bugID);  
                ResultSet rst = pst.executeQuery();  
                pst.close();  
                System.out.println("RST value = "+rst);  
                  
                SimpleDateFormat sdf = new SimpleDateFormat("hhmm");  
                  
                java.util.Date date1 =  rst.getTime(query);  
                  
                int datedb = Integer.parseInt(sdf.format(date1));  
                java.util.Date date = new java.util.Date();  
                  
                int datesys = Integer.parseInt(sdf.format(date));  
                etttime=(datesys-datedb);  
                  
                }catch(Exception e){  
                      
                e.printStackTrace();  
            }  
         return etttime;  
        }
    I am taking the date from the database and then subtracting it with the current server time. So after that i am returning the etttime which will have the difference between the number of hours. So after this i have a condition which checks for this greater than as follows. (Condition's etttime should be greater than or equal to 2 hours then it should enter)

    Java Code:
    If((a==b) && ((getETTTime(169587, con))>=0200))  
    {  
       //Statements to be executed  
    }

    When i try to do it is giving a null pointer exception and also i am not able to retreive the etttime. And also rst.getTime(query) is returning a null value. So please help me out guys as to what to do? And also how to convert the rst returned date value into the corresponding hours. Thanks a lot in advance..
    Akshay.

  2. #2
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    841
    Rep Power
    6

    Default

    Please post the code with code tags...not at all understandable.

    Before retrieval of ResultSet ,how can you close the PreparedStatement object?

    Have you checked is there any records in database.
    Ramya:cool:

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,091
    Rep Power
    20

    Default

    OK, as already pointed out you've closed the statement before doing anything with the result set. You should have an exception. Though that probably depends on the driver, I would expect some form of state exception since the resultset will have been invalidated by the closing of the statement.

    Secondly, the resources (resultset then statement) should be closed in a finally block, not in the try block.

  4. #4
    dmakshay2002 is offline Member
    Join Date
    May 2010
    Posts
    6
    Rep Power
    0

    Default

    Quote Originally Posted by RamyaSivakanth View Post
    Please post the code with code tags...not at all understandable.

    Before retrieval of ResultSet ,how can you close the PreparedStatement object?

    Have you checked is there any records in database.
    Thanks i tried it and now its entering the loop and coming till there. But i have another problem now. When i try to execute it its saying
    Java Code:
       java.sql.SQLException: Column 'select date_modified from mantis_bug_history_table where bug_id = ? ' not found.
    Again i checked in the database. I've a table by mantis_bug_history_table name and also 'v a column by date_modified. But still its saying this. what to do? how to solve this?

  5. #5
    dmakshay2002 is offline Member
    Join Date
    May 2010
    Posts
    6
    Rep Power
    0

    Default

    Thanks i tried it and now its entering the loop and coming till there. But i have another problem now. When i try to execute it its saying
    Java Code:
       java.sql.SQLException: Column 'select date_modified from mantis_bug_history_table where bug_id = ? ' not found.
    Again i checked in the database. I've a table by mantis_bug_history_table name and also 'v a column by date_modified. But still its saying this. what to do? how to solve this?

  6. #6
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    841
    Rep Power
    6

    Default

    Iam not able to see the code above....

    what iam telling here is whatever query u are giving here ,just run directly in the database sql window and check...Let us see...
    Last edited by RamyaSivakanth; 05-28-2010 at 10:26 AM.
    Ramya:cool:

  7. #7
    dmakshay2002 is offline Member
    Join Date
    May 2010
    Posts
    6
    Rep Power
    0

    Default

    Quote Originally Posted by RamyaSivakanth View Post
    Iam not able to see the code above....
    This's my method.
    Java Code:
    private static long getETTTime(long bugID,Connection con){  
               int etttime=0;  
               PreparedStatement pst = null;
                 
            try{  
                String query = "select max(date_modified) from mantis_bug_history_table where bug_id= ?";  
                PreparedStatement pst = con.prepareStatement(query);  
                pst.setLong(1,bugID);  
                ResultSet rst = pst.executeQuery();  
                
                System.out.println("RST value = "+rst);  
                  
                SimpleDateFormat sdf = new SimpleDateFormat("hhmm");  
                  
                java.util.Date date1 =  rst.getTime(query);  
                  
                int datedb = Integer.parseInt(sdf.format(date1));  
                java.util.Date date = new java.util.Date();  
                  
                int datesys = Integer.parseInt(sdf.format(date));  
                etttime=(datesys-datedb);  
                  
                }catch(Exception e){  
                      
                e.printStackTrace();  
           }
                finally{
                try{
                    pst.close();
                }catch(Exception e){
                    e.printStackTrace();
                }
            }return etttime;
            
            
        }
    And im calling the method in this if condition.

    Java Code:
    If((a==b) && ((getETTTime(169587, con))>=0200))  
    {  
       //Statements to be executed  
    }
    Last edited by dmakshay2002; 05-28-2010 at 10:27 AM.

  8. #8
    dmakshay2002 is offline Member
    Join Date
    May 2010
    Posts
    6
    Rep Power
    0

    Default

    Quote Originally Posted by RamyaSivakanth View Post
    Iam not able to see the code above....

    what iam telling here is whatever query u are giving here ,just run directly in the database sql window and check...Let us see...
    Yes that is working perfectly. Its returning the date modified value for that perticular id.

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,091
    Rep Power
    20

    Default

    rst.getTime(query);

    The getters of a resultset expect a column name.
    You are giving it the whole query.

    Have you been through the Sun JDBC tutorials?

  10. #10
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,423
    Rep Power
    20

  11. #11
    dmakshay2002 is offline Member
    Join Date
    May 2010
    Posts
    6
    Rep Power
    0

    Default

    Quote Originally Posted by Tolls View Post
    rst.getTime(query);

    The getters of a resultset expect a column name.
    You are giving it the whole query.

    Have you been through the Sun JDBC tutorials?
    Hey i've given everythin.
    Java Code:
    System.out.println("After the prepared statement long method");
                ResultSet rst = pst.executeQuery();
                while (rst.next()) {
                        Date s = rst.getTime(1);
                        System.out.println("Inside result set next method"+s);
                }
    Now its perfectly printing the date modified. But i need it to retrieve from the result set to compare it with System date variable. So now i've to compare it with the system date variable. so how to do it?

  12. #12
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,091
    Rep Power
    20

    Default

    Stick to one place...honestly.
    It's been answered over at Java ranch...I'm out of here.

Similar Threads

  1. Method won't return value
    By footyvino in forum New To Java
    Replies: 2
    Last Post: 03-26-2010, 10:49 AM
  2. Replies: 3
    Last Post: 10-15-2009, 01:02 PM
  3. return a null method
    By valoyivd in forum New To Java
    Replies: 2
    Last Post: 04-21-2008, 11:19 PM
  4. Return question in a method.
    By MetalGear in forum New To Java
    Replies: 1
    Last Post: 01-13-2008, 04:45 AM
  5. Return value of method
    By cachi in forum New To Java
    Replies: 1
    Last Post: 08-01-2007, 08:23 AM

Posting Permissions

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