[英]pass a method from one class to another class in java swing
我有下面的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);
}
現在我有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("")
}
我嘗試了幾件事,但沒有做我想做的事。
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("")
}
您可以創建類A的實例,然后像這樣調用方法a.add(); 但是A類必須是靜態的。 您應該嘗試使用構造函數將rs傳遞給B類。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.