Auto-create MySQL Database in Java

Many Java / JDBC / JPA examples show only how to create DB table(s) if not existing. Like putting the table defintion or DDL in a .sql file, where the program can pick it up and execute on run-time.

But not with the database itself.

How can database be created on the fly?

ANSWER

With JDBC this can be done as a parameter to the URL connection string.

In the example shown below –

datasource.url=jdbc:mysql://localhost:3306/SCHEMA_NAME?createDatabaseIfNotExist=true

The parameter (comes after ?) – createDatabaseIfNotExist – must be set at value of true.

As I recall, this works only after MySQL version 5.1.

Note: SCHEMA is MySQL speak for database. It is common to call that even with other products. Other databases or code implementations may also term it as CATALOG.

Disable auto-rehash when connecting to mysql via jdbc?

Is there a way to specify equivalent of no-auto-rehash when connecting to mysql via jdbc?
I am trying to connect to a large db and the initialization is slow. If I connect without jdbc and specify -A, it’s instant. I don’t see an equivalent option described in the JDBC docs.

Go to Source
Author: confusedCoder