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

时间:2017-09-23 22:05:09

标签: java netbeans static-methods

我试图获取main方法产生的值,并在getConnection方法中使用它们。但是,当我尝试访问getConnection方法时,返回null值。

我想使用ConnectionManager类连接数据库。

以下代码。

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;
    }
}

3 个答案:

答案 0 :(得分:0)

您只需使用参数调用getConnection()方法。

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

然后,调用此方法。

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

答案 1 :(得分:0)

加载类时,静态c字段被初始化一次。连接字段设置为ConnectionManager字段一次,当它们仍为空时。

要“修复”您的问题,请让Connection中的代码使用ConnectionManager中的字段:

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

答案 2 :(得分:0)

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

因此,在您阅读nulls

中的特定值之前,请先将它们初始化为config.properties

让我们按照流程进行:

静态初始化步骤:

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 */
}