cb0 cb0 - 4 months ago 10
MySQL Question

Create MySQL database from Java

Is there any possibility of creating a database within MySQL from Java?

I'm only aware of the

String url="jdbc:mysql://localhost:3306/test";

Connection con = DriverManager.getConnection( url, "cb0", "xxx" );


syntax, but here you have to specify a database name in the 'url'.

How can I create a MySQL database when I only have a login name and password, but no knowledge about the existing databases?

Answer

The database isn't required in the jdbc connection, so you can do something like recommended at http://forums.mysql.com/read.php?39,99321,102211#msg-102211 and http://marc.info/?l=mysql-java&m=104508605511590&w=2:

Conn = DriverManager.getConnection
("jdbc:mysql://localhost/?user=root&password=rootpassword"); 
s=Conn.createStatement();
int Result=s.executeUpdate("CREATE DATABASE databasename");