Bug #95564 createDatabaseIfNotExist is not working for databases with hyphen in name
Submitted: 29 May 2019 14:16 Modified: 12 Oct 2021 23:49
Reporter: M O Email Updates:
Status: Closed Impact on me:
None 
Category:Connector / J Severity:S3 (Non-critical)
Version:8.0.14, 8.0.16 OS:Windows
Assigned to: CPU Architecture:x86

[29 May 2019 14:16] M O
Description:
When I try to connect to a non-existing database with hyphen in the name, the create query failed because the name is not quoted.

How to repeat:
Connect to a non-existing database with hyphen in name and createDatabaseIfNotExist set to true. Example: jdbc:mysql://localhost:3306/my-db?createDatabaseIfNotExist=true

Suggested fix:
In NativeProtocol.java on row 543:

sendCommand(this.commandBuilder.buildComQuery(getSharedSendPacket(), "CREATE DATABASE IF NOT EXISTS " + database), false, 0);

Variable database should be quoted.
[11 Jun 2019 10:17] MySQL Verification Team
Hello!

Thank you for the report.

regards,
Umesh
[20 May 2021 13:41] OCA Admin
Contribution submitted via Github - Fix for bug 95564 
(*) Contribution by Lukasz Sanek (Github s4nk, mysql-connector-j/pull/61#issuecomment-844166588): I confirm the code being submitted is offered under the terms of the OCA, and that I am authorized to contribute it.

Contribution: git_patch_647478452.txt (text/plain), 2.98 KiB.

[8 Oct 2021 14:23] Arkady Karev
Still actual. Can't use createDatabaseIfNotExist on any DB with hyphens in names.
[12 Oct 2021 23:49] Daniel So
Posted by developer:
 
Added the following entry to the C/J 8.0.27 changelog: 

"When the connection property createDatabaseIfNotExist was set to true, a non-existing database could not be created when its name included a hyphen. Thanks to Lukasz Sanek for contributing the fix."