-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;
}
但每次我實例,它採取硬編碼值。
如何設置連接數據user
和password
覆蓋SQL類中的「燒燬」連接字符串?
感謝您對這位新手程序員的幫助。
刪除'String loggedUser =「root」;''和'String loggedUserPass =「」;',替換爲'user'和'pass' – MadProgrammer