0

i Have a method of ClassA which is following

public class ClassA{
public void add(){

 try {
            Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
            String url = "jdbc:oracle:thin:@localhost:1521:XE";
            conn = DriverManager.getConnection(url, "Hr", "Hr");
            System.out.println("Connection Established");

            Statement stmt = conn.createStatement();
            ResultSet rs = stmt.executeQuery(""); // in which class i pass this method i want to write the insert query  in the inverted commas


}} } 
            catch (Exception e) {

                System.err.println("connection error " + e);
        }

Now i have a class B

public class ClassB{

//how can i get the add() method of classA in such a way that i can write my query direct into the  ResultSet rs = stmt.executeQuery("") 

}

I tried several things but fails to do what i want to do.

1 Answers1

0
public class ClassB{
ClassA a = new ClassA();
//how can i get the add() method of classA in such a way that i can write my query direct into the  ResultSet rs = stmt.executeQuery("") 

}

you can make a instance of class A and just call the method like this a.add(); but class A will have to be static. You should try to use a constructor to pass rs to class B.

Lodington
  • 13
  • 3
  • i know that make object of class A in classB and we can we use the ClassA method, but my question is how can i use rs in the classB? its the main question – Shaun Tait May 10 '18 at 12:21
  • And if making ClassA static then we don't need to to make object in the classB for use ClassA method. – Shaun Tait May 10 '18 at 12:23
  • Here take a look a this. [How Do getters and setters work](https://stackoverflow.com/questions/2036970/how-do-getters-and-setters-work). – Lodington May 10 '18 at 12:33
  • And why i should see gett and setter? – Shaun Tait May 10 '18 at 12:38
  • you can create a instance of your ResultSet in class B and use getters and setters to get this variable without setting it as static. – Lodington May 10 '18 at 12:39