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?
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.
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.
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.
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.
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");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With