在java swing中将方法从一个类传递给另一个类

时间:2022-05-31 23:09:11

i Have a method of ClassA which is following

我有一个遵循ClassA的方法

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

现在我有一个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 个解决方案

#1


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.

你可以创建一个A类的实例,并像这样调用方法a.add();但是A级必须是静态的。您应该尝试使用构造函数将rs传递给B类。

#1


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.

你可以创建一个A类的实例,并像这样调用方法a.add();但是A级必须是静态的。您应该尝试使用构造函数将rs传递给B类。