简体   繁体   中英

pass a method from one class to another class in java swing

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM