findbugs und Sicherheitsproblem mit Datenbankkennwörtern
Ich verwende den folgenden Code, um die Datenbankverbindung zu initialisieren:
public Connection getConnection() {
try {
if (null == connection) {
String driverName = "com.mysql.jdbc.Driver"; // MySQL MM JDBC driver
Class.forName(driverName);
// Create a connection to the database
String serverName = "localhost";
String database = "database";
String url = "jdbc:mysql://" + serverName + "/" + mydatabase; // a JDBC url
String username = "username";
String password = "password";
connection = DriverManager.getConnection(url, username, password);
}
return connection;
} catch (ClassNotFoundException cnfe) {
cnfe.printStackTrace();
} catch (SQLException sqle) {
sqle.printStackTrace();
}
throw new NullPointerException("Cannot establish database connection...");
}
und ich weiß, es ist schlechte Praxis, es zu tun, auch ich liefFindBugs
gegen den Code und das Sicherheitsproblem lautete wie folgt:This code creates a database connect using a hardcoded, constant password. Anyone with access to either the source code or the compiled code can easily learn the password.
Was ist der beste Weg, um die Datenbankverbindung zu initialisieren, ohne diese Sicherheitsverletzung zu haben?