Bug #13453 DriverManager.getConnection: user/password containing = or &
Submitted: 24 Sep 2005 1:41 Modified: 11 Nov 2009 2:35
Reporter: Joe Knall Email Updates:
Status: Won't fix Impact on me:
None 
Category:Connector / J Severity:S2 (Serious)
Version:3.1.10 OS:Linux (Linux)
Assigned to: Mark Matthews

[24 Sep 2005 1:41] Joe Knall
Description:
DriverManager.getConnection has problems with equal sign (=) and ampersand (&) in parameter -> Access denied;

Example:
String pw = "a+b=c";
DriverManager.getConnection("jdbc:mysql://192.168.2.4/test?user=me&password="+pw);

SQLException: Access denied for user 'me'@'192.168.3.12' (using password: YES)
SQLState: 28000
VendorError: 1045

If it's just a java beginners problem with escaping signs in strings it would be nice if you'd mention the solution in the docs; otherwise I consider it a bug.

How to repeat:
String pw = "a+b=c";
DriverManager.getConnection("jdbc:mysql://192.168.2.4/test?user=me&password="+pw);

Suggested fix:
use different passwords; not really convincing:(
[24 Sep 2005 3:09] Mark Matthews
Until such time as the JDBC specification has defined what is meant by a "URL", I'm afraid we're at a loss to pick an appropriate encoding scheme for these "special" characters that won't be overridden when JDBC-4.0 is released as a final spec w/ Mustang (Java6), so we're going to take a wait-and-see so that we don't have a backwards-compatibility issue on our hands.

The workaround for now is to pass them either in a java.util.Properties instance, or as individual strings using the DriverManager, Driver and/or DataSource methods that take those arguments, as those strings aren't parsed.

In general, the DriverManager and Driver interfaces have been discouraged (but not deprecated yet) by Sun, so using DataSource with the mutators for setPassword() and setUser() is the preferred mechanism.
[24 Sep 2005 13:10] Joe Knall
Thank you Mark

This is working:
String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://192.168.2.4/test";
String user = "me";
String passwd = "a+b=c";
Class.forName(driver);
Connection cn = DriverManager.getConnection(url, user, passwd);	

Please add a hint in the docs of the next release!