如何从 main 方法获取值以供 getConnection 方法访问?

How can I get the values from the main method to be accessed by the getConnection method?

我正在尝试获取主要方法生成的值并在 getConnection 方法中使用它们。但是,当我尝试访问 getConnection 方法时,将返回空值。

我想使用 ConnectionManager class 连接到数据库。

代码如下。

public class ConnectionManager {

    public static String database;    
    public static String dbuser;
    public static String dbpassword;

    public static void main(String args[])  {

        Properties prop = new Properties();
        InputStream input = null;

        try {
            input = new FileInputStream("config.properties");

            // load a properties file
            prop.load(input);

            database = prop.getProperty("database");
            dbuser = prop.getProperty("dbuser");
            dbpassword = prop.getProperty("dbpassword");

            System.out.println(database);
        } catch (IOException ex) {
            ex.printStackTrace();
        } finally {
            if (input != null) {
                try {
                    input.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }


    public static String url = "jdbc:mysql://localhost:3306/" + database;    
    private static String driverName = "com.mysql.jdbc.Driver";   
    private static String username = dbuser;   
    private static String password = dbpassword;
    private static Connection con;

    public static Connection getConnection() {

        try {
            Class.forName(driverName);
            try {
                con = DriverManager.getConnection(url, username, password);
            } catch (SQLException ex) {
                // log an exception. For example:
                System.out.println("Failed to create the database connection."); 
                System.out.println(url + " " + username + " " + password);
            }
        } catch (ClassNotFoundException ex) {
            System.out.println("Your driver has not been found."); 
        }
        return con;
    }
}

您只需调用带参数的 getConnection() 方法即可。

public static Connection getConnection(String url, String username, String password) {
    /* Your code here */
}

然后,调用这个方法。

Connection connection = getConnection(url, username, password);

加载 class 时,静态 c 字段会初始化一次。 Connection 字段设置为 ConnectionManager 字段一次,当时它们仍然为空。

针对"fix"您的问题,让您在 Connection 中的代码使用 ConnectionManager 中的字段:

con = DriverManager.getConnection(url, ConnectionManager.dbuser, ConnectionManager.dbpassword);

您在 DriverManager.getConnection(url, username, password) 调用中得到空参数值,因为您已将它们声明为静态字段。

因此,在从 config.properties

中读取特定值之前,您将它们初始化为 nulls

让我们跟着流程走:

静态初始化步骤:

private static String username = dbuser;      //username==null; dbuser==null;
private static String password = dbpassword;  //password==null; dbpassword==null
private static Connection con;                //con==null;

方法主要执行:

database = (prop.getProperty("database"));
dbuser = (prop.getProperty("dbuser"));          //dbuser="smth"; username==null
dbpassword = (prop.getProperty("dbpassword"));  //dbpassword ="smth"; password==null

方法getConnection执行:

con = DriverManager.getConnection(url, username, password); //username==null; //password==null; 

P.S.: 如前所述,最好使用带有如下参数的函数:

public static Connection getConnection(String url, String username, String password) {
    /* Your code here */
}