Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create MySQL database from Java

Is it possible to create a MySQL database from Java?

I have only seen connection URLs examples like this where the database name is specified in the URL:

String url="jdbc:mysql://localhost:3306/test";  Connection con = DriverManager.getConnection( url, "cb0", "xxx" ); 

How can I create a MySQL database when I only have a login name and password?

like image 575
cb0 Avatar asked Apr 04 '09 17:04

cb0


People also ask

Can I create a database in Java?

To create the database tables in Java DB, the database server included with Application Server, you need to create the database connection and execute the SQL commands in tut-install /examples/common/sql/javadb/tutorial.

How can I use JDBC to create a database?

To create a Database using JDBC API you need to: Register the driver: Register the driver class using the registerDriver() method of the DriverManager class. Pass the driver class name to it, as parameter. Establish a connection: Connect ot the database using the getConnection() method of the DriverManager class.

Can Java be used with MySQL?

In Java, we can connect to our database(MySQL) with JDBC(Java Database Connectivity) through the Java code. JDBC is one of the standard APIs for database connectivity, using it we can easily run our query, statement, and also fetch data from the database.

How do I create a new database in MySQL?

Open the MySQL Workbench as an administrator (Right-click, Run as Admin). Click on File>Create Schema to create the database schema. Enter a name for the schema and click Apply. In the Apply SQL Script to Database window, click Apply to run the SQL command that creates the schema.


1 Answers

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"); 
like image 181
Jeremy Stanley Avatar answered Oct 12 '22 14:10

Jeremy Stanley