为Connection Class设置多个用户/密码

时间:2017-03-08 20:02:52

标签: java sql swing connection

大家好我有一个包含多个配置文件的应用程序(" Admin"," Delegate"),在我的数据库中,我有相同的用户来进行操作。我想用一个ComboBox设置数据库用户,用户登录并根据用户配置文件通过我的所有数据库调用使用该数据。

我的连接数据在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;
}

但每次我实例化时,它都需要Hardcoded值。

如何设置连接数据userpassword覆盖" Burned"连接SQL类中的字符串?。

感谢您对这位新手同事的帮助。

1 个答案:

答案 0 :(得分: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...
}

所以当用户使用按钮登录时,在comboBox中,所以我设置了静态变量,

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

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

不是那么优雅,但有时​​可以开始。