1. Get rid of all advertisements and get unlimited access to documents by upgrading to Premium Membership. Upgrade to Premium Now and also get a Premium Badge!

How to call a method from another class to execute a query

Discussion in 'Other Development Tools' started by 24042014a, Jun 19, 2014.

  1. 24042014a

    24042014a Active Member

    Messages:
    9
    Likes Received:
    0
    Trophy Points:
    55
    Hi,

    I have an error when i call a method from my utility class.
    Note that Database connection is working fine. so i did not put the code for this.


    i have a utility.java where i put this code:
    Code (Text):
    public static  ResultSet Select(Connection conn){
       
            //ResultSet result2 = null;
           
            try {                  
     
                Statement stmt = conn.createStatement();
                ResultSet result2 = stmt.executeQuery( "select * from my table" ) ;
                while( result2.next() )
                System.out.println( result2.getString(1) ) ;
                //conn.commit();
                //Close the statement
                result2.close() ;
                stmt.close() ;
               
               System.out.println("Show data from my table");
               return result2;
               
               
             } catch (SQLException se) {  
                 se.printStackTrace();
                 System.out.println(se);
             }
            return null;
        }
    And i call this method in another class like this:
    Code (Text):
    public class Query{

        public Query(Connection conn ){
           
        //Connection to Database
          System.out.println("Starting connection...");
              Utility.dbConnection();
             
         
           
        //Delete all records from all tables
          System.out.println("Start to clean the Report table");
          Utility.Select(conn);
         
        }
    }



    It throwing javalangNullPointerException error

    Any help please, thank you
     
  2. DTSIGuy

    DTSIGuy Forum Advisor

    Messages:
    402
    Likes Received:
    58
    Trophy Points:
    410
    Location:
    Texas
    I'm not a Java nut...but doesn't Java give you an indication of WHERE the error is occurring? Or are you supposed to guess?
     
  3. 24042014a

    24042014a Active Member

    Messages:
    9
    Likes Received:
    0
    Trophy Points:
    55
    yes the error is at lines:
    in utility class
    in Query class
     
  4. DTSIGuy

    DTSIGuy Forum Advisor

    Messages:
    402
    Likes Received:
    58
    Trophy Points:
    410
    Location:
    Texas
    Then it would appear that your DB Connection is not working...if "conn" is being populated by that process. Why not test for a null connection before attempting it?
     
  5. 24042014a

    24042014a Active Member

    Messages:
    9
    Likes Received:
    0
    Trophy Points:
    55
    Thank you, how can i test for a null connection

    my connection class:
    here i'm calling data from a property file.

    public static Connect Connection(){

    Connection conn = null;
    try{
    Class.forName("oracle.jdbc.driver.OracleDriver");

    String url = "jdbc:eek:racle:thin:mad:" + Util.getPropertyValue("host") + ":" + "xxxx" + ":" + "xxxx";

    conn = DriverManager.getConnection(url, Util.getPropertyValue("xxxx"), Util.getPropertyValue("xxxx"));
    System.out.println("Database Connection..Success");
    } catch (ClassNotFoundException e) {
    e.printStackTrace();
    } catch (SQLException e) {
    e.printStackTrace();
    }
    return conn;
    }
     
  6. DTSIGuy

    DTSIGuy Forum Advisor

    Messages:
    402
    Likes Received:
    58
    Trophy Points:
    410
    Location:
    Texas
  7. 24042014a

    24042014a Active Member

    Messages:
    9
    Likes Received:
    0
    Trophy Points:
    55
    When i call the method for connection, it shows the message : Database Connection..Success
    so that means the connection is ok, right?
     
  8. DTSIGuy

    DTSIGuy Forum Advisor

    Messages:
    402
    Likes Received:
    58
    Trophy Points:
    410
    Location:
    Texas
    As I said before...I'm not a Java nut. However, I would hazard the guess that your identified connection is out of scope w/ respect to the method where it's being used. That's the only thing that would make sense to me at the moment.
     
  9. 24042014a

    24042014a Active Member

    Messages:
    9
    Likes Received:
    0
    Trophy Points:
    55
    I just remove the connection from utility class and created another connection inside the second method and it's working for me.
    but i recognize that s really not elegant because i hace already a method for connection.
    For this moment, i will do like this but i should find the right way to use the connection method separately.
    Thank you
     
  10. Amitysoft14

    Amitysoft14 Active Member

    Messages:
    11
    Likes Received:
    0
    Trophy Points:
    80
    Location:
    chennai
    Hi 24042014a!

    I clearly don't know about this topic, But now when i read your posted information that time i get an idea to improve my knowlede about the call method from another class to execute a query. Thank you for your useful information, Keep in touch with me... :)
     
  11. dungskyvietnam

    dungskyvietnam Newly Initiated

    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    30
    Location:
    Hà nội
    Then it would appear that your DB Connection is not working...if "conn" is being populated by that process.