【发布时间】:2017-04-19 14:12:53
【问题描述】:
public class A {
// method to get connection
ComboPooledDataSource cpDataSource = new ComboPooledDataSource();
public Connection getconnection() throws Exception {
Connection con = null;
try {
// open a connection
con = cpDataSource.getConnection();
} catch (SQLException e) {
e.printStackTrace();
}
return con;
}
public Class B {
Statement stat = null;
Connection con = getconnection();
stat = con.createStatement();
stat.execute("create table if not exists Node1(id int NOT NULL AUTO_INCREMENT, name varchar(255), ip varchar(255), port int, site varchar(255), object_referenece varchar(255), connectivity_status varchar(255))");
closeConnection(con,stat);
public void closeConnection(Connection con, Statement stat) throws
SQLException {
stat.close();
con.close();
}
}
我有这两个类,B类纯粹用于创建表。我需要从 A 类中获取 getConnection 方法才能在 B 类中继续。当我使用此语法时,它要求我在此类中再次创建 getconnection 方法。有什么方法可以从 A 类中获取方法并在 B 类中使用?
【问题讨论】:
-
您的代码有点乱,请检查一下。类
B不见了。