2017-03-08 61 views
-1

嗨,大家好我有一個包含多個配置文件(「Admin」,「Delegate」)的應用程序,並且在我的數據庫中有相同的用戶進行操作。我想用一個ComboBox設置數據庫用戶Where用戶登錄並根據用戶配置文件通過我所有的數據庫調用使用該數據。爲連接類設置多個用戶/密碼

我的連接數據被硬編碼在Connection類SQL這樣的:

public class SQL { 

private PreparedStatement PStatement; 
private Connection connection; 
private String user; 
private String pass; 


public String getUser() { 
    return user; 
} 

public void setUser(String user) { 
    this.user = user; 
} 

public String getPass() { 
    return pass; 
} 

public void setPass(String pass) { 
    this.pass = pass; 
} 

public Connection dbConection() { 

    String loggedUser = "root" ; 
    String loggedUserPass = ""; 

    try { 
     Class.forName(cf.DB_DRIVER); 
     connection = DriverManager.getConnection(cf.SERVER_URL + cf.SERVER_DB, loggedUser, loggedUserPass); 

     if (connection == null) { 
      throw new SQLException("Connection no established"); 
     }    
     return connection; 

    } catch (ClassNotFoundException | SQLException e) { 

     JOptionPane.showMessageDialog(null, e.getMessage(), cf.WINDOW_TITLE, JOptionPane.ERROR_MESSAGE);    
    } 
    return connection; 
} 

每當我用我的DAO我必須調用SQL類:

public int insert(Users user) throws SQLException { 
    SQL sql = new SQL(); 
    query = sql.createPStatement(cf.INSERT_USER_DATA); 
    query.setInt(1, user.getUserId()); 
    query.setInt(2, user.getUserCencos()); 
    query.setInt(3, user.getUserProfile()); 
    query.setString(4, user.getUserPassword()); 
    query.setString(5, user.getUserName()); 
    query.setString(6, user.getUserPosition()); 
    query.setString(7, user.getUserOffice()); 
    try { 
     result = query.executeUpdate(); 
    } catch (SQLException | NumberFormatException e) { 
     throw e; 
    } finally { 
     SQLUtils.closeQuietly(sql.dbConection()); 
     SQLUtils.closeQuietly(query); 
    } 
    return result; 
} 

但每次我實例,它採取硬編碼值。

如何設置連接數據userpassword覆蓋SQL類中的「燒燬」連接字符串?

感謝您對這位新手程序員的幫助。

+0

刪除'String loggedUser =「root」;''和'String loggedUserPass =「」;',替換爲'user'和'pass' – MadProgrammer

回答

0

我有一個類cf與公共靜態變量。

public class cf {  

public static String DB_USER; 
public static String DB_PASSWORD; 
public static String INV_DEL="delegado"; 
public static String INV_DELXXX="123"; 
public static String A_="root"; 
public static String A_XXX=""; 
getter and setter... 

public static void setLogin(String profile){ 

    switch(profile){    
     case "Admin": 
      cf.setDB_USER(cf.A_); 
      cf.setDB_PASSWORD(cf.A_XXX); 
     break;  

     case "Delegado": 
      cf.setDB_USER(cf.INV_DEL); 
      cf.setDB_PASSWORD(cf.INV_DELXXX); 
     break;   
    } 

} 

所以在我的SQL類中,我使用靜態變量而不是實例變量。

public Connection dbConection() { 

try { 
     Class.forName(cf.DB_DRIVER); 
     connection = DriverManager.getConnection(cf.SERVER_URL + cf.SERVER_DB, cf.getDB_USER(), cf.getDB_PASSWORD()); 
    more SQL stuff... 
} 

因此,當用戶登錄與按鈕,在下拉框中,所以我設置了靜態變量,

private void loginButtonActionPerformed(java.awt.event.ActionEvent evt) {   

profile = profileComBoBox.getSelectedItem().toString();   
    setLogin(profile); 
do login stuff...  
} 

是不是很優雅,但有時​​是開始。