how to escape special characters in mysql jdbc connection string - mysql

im trying to connect to a remote database. im sure that the remote database provide me all the privileges. but im getting this error when trying to connect to the remote mysql database through jdbc connection. my password contains, * and & symbol, i think that is the problem cause to this error. but i dont know how to escape those characters. please help
java.sql.SQLException: Access denied for user 'myusername'#'myhost' (using password: YES)
"jdbc:mysql://myhost/mydb?user=myusername&password=my&password*"
please help
Regards

The JDBC connection string is in url format so you must use URL encoded values for all parameters. In your case the connection string should look like:
"jdbc:mysql://myhost/mydb?user=myusername&password=my%26password%2A"

URLEncoder can be used:
String connectionString = "jdbc:mysql://myhost/mydb?" + URLEncoder.encode("user=myusername&password=my&password*", "UTF-8");

Related

ERROR Database:40 - java.sql.SQLNonTransientConnectionException

I use database.properties to save my database information. However, it pop up this error, every time I run the code. I don't know which part of the database is wrong. Please help!
host: 127.0.0.1
port: 3306
database: spider
username: root
password: !QAZxsw2
driver: com.mysql.cj.jdbc.Driver
drivertype: MYSQL
ERROR Database:40 - java.sql.SQLNonTransientConnectionException:
Cannot load connection class because of underlying exception:
com.mysql.cj.exceptions.WrongArgumentException: Failed to parse the
host:port pair
'127.0.0.1:3306;databaseName=spider;user=root;password=!QAZxsw2;serverTimezone=UTC&amp'.
You seem to be using SQL Server JDBC driver syntax for the URL, while you are using the MySQL JDBC driver.
See MySQL Connector/J 8, Connection URL Syntax, the URL to connect to MySQL would be:
jdbc:mysql://127.0.0.1:3306/spider?user=root&password=!QAZxsw2&serverTimezone=UTC
Note that I also removed the & you had, which has no place in a URL, unless you are putting it in XML and want to escape the & between key-value pairs.
Given the syntax of JDBC URLs is undefined, except for the jdbc:<sub-protocol>: prefix, you cannot try to dynamically construct it like you are doing and expect it to work on different drivers. Each driver has their own syntax, and although there is considerable overlap in syntax, each has their own idiosyncrasies (if not outright outlandish syntax). Instead, use a single property for the entire URL, or use a driver-specific strategy.
After I post this question, I found out that my connection string was wrong. jdbc:mysql://localhost:%s/%s?serverTimezone=UTC&"+"user=%s&password=%s It should be like that url pattern of mysql.

My asp.net website is trying to connect mysql db though I am not using mysql

My ASP.net web form website is using MSSQL server and site web.cofig has sql server connection string. But I am surprised that why it is trying to connect my sql db though there is no connection string for my sql.
I dont know why.. That's my problem too
Exception message :
Authentication to host '' for user '' using method 'mysql_native_password' failed with message: Access denied for user ''#'xxx' (using password: NO)\r\n at MySql.Data.MySqlClient.Authentication.MySqlAuthenticationPlugin.AuthenticationFailed(Exception ex)\r\n at MySql.Data.MySqlClient.Authentication.MySqlAuthenticationPlugin.ReadPacket()\r\n at MySql.Data.MySqlClient.Authentication.MySqlAuthenticationPlugin.Authenticate(Boolean reset)\r\n at MySql.Data.MySqlClient.NativeDriver.Open()\r\n at MySql.Data.MySqlClient.Driver.Open()\r\n at MySql.Data.MySqlClient.Driver.Create(MySqlConnectionStringBuilder settings)\r\n at MySql.Data.MySqlClient.MySqlPool.CreateNewPooledConnection()\r\n at MySql.Data.MySqlClient.MySqlPool.GetPooledConnection()\r\n at MySql.Data.MySqlClient.MySqlPool.TryToGetDriver()\r\n at MySql.Data.MySqlClient.MySqlPool.GetConnection()\r\n at MySql.Data.MySqlClient.MySqlConnection.Open()\r\n at MySql.Web.Common.SchemaManager.GetSchemaVersion(String connectionString)\r\n at MySql.Web.Common.SchemaManager.CheckSchema(String connectionString, NameValueCollection config)\r\n at System.Web.Configuration.ProvidersHelper.InstantiateProvider(ProviderSettings providerSettings, Type providerType)
I suspect this happened after I installed mysql-community-5.7.20.0
Update : I uninstalled everything (mysql) just now and it's normal again

JDBC for MySQL connection: works with MySQL Workbench, not with JDBC

I have a database server running on a bluehost machine, and I am giving myself remote IP access (via my IP address).
On my client machine, I have MySQL Workbench, and I have some Java code. MySQL workbench is getting the connection, using the database that I would like it to. With the driver manager connection, I cannot specify my database, such as with a forward slash after the port # in the URL, or it will give a connect error.
I can get the connection with the same userid and password using Workbench or the driver manager, but with JDBC it doesn't want to give me access to the same database.
When I try to execute this (JDBC): "use database", I get:
Access denied for user 'userid'#'184.11.22.%' to database 'database'
The URL I am using is
jdbc:mysql://(server-ip):(port)
If I try to append a '/(database)' on to the URL it gives the access violation. MySQL Workbench isn't getting any other special information, but appears to connect with the specified default schema. Same client machine; same user id and password. What is going wrong with the JDBC connection?
Java code added:
db = DriverManager.getConnection(
"jdbc:mysql://74.220.192.113/advanfc7_atm?" +
"user=(myuserid&password=(mypwd)");
System.out.println("Connected.");
statement = db.createStatement();
statement.execute("use advanfc7_atm");
} catch (Exception e) {
System.out.println("InitDB() "+e.getMessage());
}
I've also done it with 3 parameters to getConnection(), separating userid and password to the 2nd and 3rd params.
There is a small syntax error in your getConnection code:
db = DriverManager.getConnection(
"jdbc:mysql://74.220.192.113/advanfc7_atm?" +
"user=(myuserid&password=(mypwd)");
Note that the closing parenthesis on user=(myuserid is missing. It should be user=(myuserid).
Instead:
Since each driver has different url syntax, it's generally safer to use the DriverManager.getConnection(String url, String user, String password) method instead.
Example usage:
db = DriverManager.getConnection(
"jdbc:mysql://74.220.192.113/advanfc7_atm",
"myuserid",
"mypwd");

DBD Basic Authentication Apache 2.2

I have a DBD Basic Authentication setup. When I try to log into my webpage with this authentication it does not work. The error is password mismatch in the Apache error logs, and the connection to MySQL database is successful from its logs.
This is my connection string in the httpd.conf:AuthDBDUserPWQuery "SELECT Password FROM Users WHERE Username = %s"
This table does exist, and the Column names are right as far as I can see. My DB Structure stores Username and Password in plaintext. Where am I going wrong? (I am sorry I can't provide more output I am tunneling through my Universities bespoke system to access my server and it does not allow copy pasting text).
I fixed it by changing from using plaintext passwords in my DB to using the SQL Encrypt function on each password stored.

JDBC: DriverManager.getConnection(url) not reading user/password

I'm trying to open a connection to a db via the DriverManager. When I use the getConnection(url, properties) function it works fine, however when I try to append the username and password (something like jdbc:mysql:address;user=user;password=password) to the url and call getConnection(url) I get the following error
Access denied for user ''#'localhost' to database 'db_aux_linux64_mysql;user=user;password=password'
So while it was able to find the db, it is trying to pass in an empty string as the user argument (and probably the password as well) for some reason. Any ideas on why this might be?
You're using the wrong url syntax, try this:
Connection conn = DriverManager.getConnection("jdbc:mysql://hostname/dbname?user=user&password=password");
Note: Don't forget to add your database name after your hostname -- the error message you are getting is indicating that the jdbc driver is looking for a database that is literally named "db_aux_linux64_mysql;user=user;password=password".
Alternatively, you can just use the DriverManager.getConnection(url, uname, pass) as suggested in the comments, then you don't need to remember the url syntax for your jdbc driver (and your code will be more portable since each jdbc driver is subject to having different url syntax).
Source: Mysql doc seciton 6.1