31

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?

Ole
48.2k72 gold badges247 silver badges464 bronze badges
asked Apr 4, 2009 at 17:17

5 Answers 5

51

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");
answered Apr 4, 2009 at 17:27
Sign up to request clarification or add additional context in comments.

Comments

7

To create database through Java code, you must use executeUpdate(sql) instead of executeQuery(sql); and connect to the mysql database as root:

connection = DriverManager.getConnection(
 "jdbc:mysql://localhost:3306/mysql?zeroDateTimeBehavior=convertToNull",
 "root", "root"
);
Statement st = connection.createStatement();
st.executeUpdate(sql);
st.close();
Matteo
15k11 gold badges70 silver badges109 bronze badges
answered Nov 30, 2012 at 15:19

Comments

2

An elegant approach to such issues is using DDL Utils from Apache. Not only would it serve the basic purpose of allowing to execute your (externally configurable) DDLs, but also would make your application database independent.

answered May 17, 2009 at 12:24

Comments

1

To make things even easier, you can use NetBeans 6.5 and it makes setting up SQL databases SO much easier. I'm using them right now and its a lifesaver on GUI layouts and database connections. Here's some code on how I connect to mysql database from NetBeans:

 //these are variables i declare in the beginning of my code
 public static final String DRIVER = "com.mysql.jdbc.Driver";
 public static final String DATABASE_URL = "jdbc:mysql://localhost:3306/jtschema";
 private Connection connection = null;
 public static Statement statement = null;
 public void initSQLServer() {
 try {
 Class.forName(DRIVER).newInstance();
 try {
 connection = DriverManager.getConnection(DATABASE_URL, "root", "Dropatrain!248");
 statement = connection.createStatement();
 } catch (SQLException e) {
 System.out.println("SQLException: " + e.getMessage());
 System.out.println("SQLState: " + e.getSQLState());
 System.out.println("VendorError: " + e.getErrorCode());
 }
 } catch (Exception ex) {
 System.out.println(ex);
 }
 }
Young Emil
2,2763 gold badges29 silver badges39 bronze badges
answered Apr 5, 2009 at 19:56

1 Comment

NetBeans wiki has all the info you need tho set up a sql table in your project also!
1

You can use this lines:

 try {
 String databaseName = "dbName";
 String userName = "root";
 String password = "yourPassword";
 String url = "jdbc:mysql://localhost:3306/mysql?zeroDateTimeBehavior=convertToNull";
 Connection connection = DriverManager.getConnection(url,username, password);
 String sql = "CREATE DATABASE " + databaseName;
 Statement statement = connection.createStatement();
 statement.executeUpdate(sql);
 statement.close();
 JOptionPane.showMessageDialog(null, databaseName + " Database has been created successfully", "System Message", JOptionPane.INFORMATION_MESSAGE);
 } catch (Exception e) {
 e.printStackTrace();
}
Peter Badida
12.3k10 gold badges53 silver badges99 bronze badges
answered Jan 21, 2015 at 6:13

2 Comments

Pm me your email address so that i can send the JAR file.
Thanks. But please notice the question was 6 years ago :)

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.