<分区>
我的方法返回 Connection
对象的值。
Public class DatabaseConnection
{
public Connection establishConnection(C)
{
try
{
this.readLogin(); // prompt user to enter String values for user, pass, host
this.createDatabaseIfNeeded(); // make chessleaguedb if not found
conn = DriverManager.getConnection
("jdbc:mysql://"+host+":3306/chessleaguedb", user, pass);
System.out.println("Successfully connected to chessleaguedb");
}
catch (Exception e)
{
// logic
}
return conn; // want the logic to handle opening a connection in this class and method, then pass it to a Connection object in my 'menu' class
}
}
此方法在另一个类中调用,并将返回值传递给从现在开始使用的新 Connection 对象。
public class DBAMenu
{
DatabaseConnection startConnection = new DatabaseConnection; // new instance of class that contains the aforementioned method establishConnection()
Connection conn = null;
conn = startConnection.establishConnection();
}
这里是否只打开了一个或两个数据库连接,因为我正在返回值并将其传递给新的 Connection
对象?
我试过使用 NetBeans 调试器,Connection
值在此过程后保持不变,但不可否认,我不是 100% 了解 Netbeans 调试器中值列的含义。
(不使用 Java EE,所以不能使用池化,也不能使用开源软件来处理池化,因为二年级本科项目的工作必须是我自己的)