How to Create Database for MySQL

From Java Example Source Code

Jump to: navigation, search

Contents

[edit] Overview - How to Create Database for MySQL

This Java example program shows how to create database for mysql.

[edit] Java Source Code

  • Package: example.jdbc.mysql
  • File: CreateDatabase.java
package example.jdbc.mysql;
 
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
 
public class CreateDatabase {
    public static void main(String[] args) {
	Connection connection = null;
	Statement statement = null;
	try {
	    Class.forName("org.gjt.mm.mysql.Driver").newInstance();
	    String url = "jdbc:mysql://localhost/mysql";
	    connection = DriverManager.getConnection(url, "username", "password");
 
	    statement = connection.createStatement();
	    String hrappSQL = "CREATE DATABASE hrapp";
	    statement.executeUpdate(hrappSQL);
	} catch (Exception e) {
	    e.printStackTrace();
	} finally {
	    if (statement != null) {
		try {
		    statement.close();
		} catch (SQLException e) {
		} // nothing we can do
	    }
	    if (connection != null) {
		try {
		    connection.close();
		} catch (SQLException e) {
		} // nothing we can do
	    }
	}
    }
 
}

[edit] What Result You Can Get

Coming soon...

[edit] Required External Libraries and/or Files for this Java Example

In order to run this Java example, one of the following libraries may be required: In order to run this example program, one of the following libraries may be required:


[edit] How to Run this Java Example Program

We recommend running this Java example program with Eclipse.

For assistance in working with Eclipse, please see How to Run Java Program with Eclipse.

It's fairly easy.



[edit] Question & Answer

Any question?

Click edit and post your question or answer here.


Personal tools