2017-09-23 43 views
-1

我想獲取主要方法會產生的值,並在getConnection方法中使用它們。但是,當我嘗試訪問getConnection方法時,返回空值。如何從getConnection方法訪問主方法的值?

我想使用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; 
    } 
} 

回答

0

你只需要調用帶有參數的getConnection()方法。

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

然後,調用這個方法。

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

參數的值將來自公共靜態無效的主要(字符串參數[]) –

+0

謝謝。它進行了一些修改。 –

0

靜態c字段在加載類時被初始化一次。連接字段設置爲連接管理器字段一次,當它們仍然爲空時。

「修理」你的問題,有你的連接代碼使用的字段中的ConnectionManager:

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

不起作用。仍然返回null。 –

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; 

PS:正如先前所說,這是最好使用帶有參數的函數是這樣的:

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

謝謝。它進行了一些修改。 –