java - 在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 类。
推荐阅读
- java - 通过邮递员调用 AWS Lambda 函数时出错 - 403 错误
- php - PHP 导出 mysql 数据到 Excel crontab linux
- ios - 我们如何将我们在 dribbble 上看到的出色动画实现到 ios 应用程序?
- nativescript - NativeScript RadListView 滚动到底部问题
- javascript - JwtModule.registerAsync 在 NestJS 中不起作用
- java - 在 Java 中读取输入的最佳方式是什么,尤其是对于连续字符串?我不知道何时使用 next() 和 nextLine()
- bison - 野牛移位/减少与匿名函数调用的冲突
- python - 将 Pandas Dataframe 转换为 numpy 数组
- docker - 具有持久卷的 kube 上的弹性搜索
- python - 在 PCA 之后解释 OLS 权重(在 Python 中)