As the title says, I'm looking into finding a way to use a char[] array to establish a JDBC connection instead of creating a new String object from the char[] array and using that to establish the connection.
Because char[] arrays are more secure than Strings in java, I've been wanting to keep things as secure as possible when dealing with my JPasswordFields.
In this particular case, I'm taking the char[] array contents of a JPasswordField and attempting to establish a JDBC connection to a database. It works perfectly well, but I'm having to create a new String object from the char[] array to actually call the getConnection method.
Is there any way to ensure at-least some security doing this, or am I just forced to create the String object and continue on using that in the method?
This is my code:
/**
* Construct a new DataManager object with it's own
* connection to the database.
*
* @param ipAddress The IP address to the server on which MySQL is running.
* @param port The port to use when connecting to MySQL.
* @param databaseName The database name of the database to connect to.
* @param username The username for a MySQL account with access to the specified database.
* @param password The password for the MySQL account specified by the specified username.
*/
public DataManager(final String ipAddress, final String port, final String databaseName, final String username, final char[] password) throws ClassNotFoundException, IllegalAccessException, InstantiationException, SQLException {
Class.forName("com.mysql.jdbc.Driver").newInstance();
String url = "jdbc:mysql://" + ipAddress + ":" + port + "/" + databaseName + "?noAccessToProcedureBodies=true"; //?noAccessToProcedureBodies=true is required for using the stored procedures.
dbConnection = DriverManager.getConnection(url, username, new String(password));
}
Sorry for the formatting, but the lines are a bit long and they wrap.
Here's the Java docs for the DriverManager class that I'm using. I've checked and there doesn't seem to be a method to use the char[]
instead of the String
and that's what prompted this post.
Thanks for any help/tips.