The following code shows how to create a database in MySQL database by using JDBC connection.
The SQL used to create database is listed as follows.
CREATE DATABASE STUDENTS
The code loads the driver first and then makes a connection to the database.
Then it creates a Statement and issues the create database command.
import java.sql.Connection; import java.sql.DriverManager; import java.sql.Statement; //from w w w.jav a2s. c o m public class Main { static final String JDBC_DRIVER = "com.mysql.jdbc.Driver"; static final String DB_URL = "jdbc:mysql://localhost/"; static final String USER = "username"; static final String PASS = "password"; public static void main(String[] args) throws Exception { Connection conn = null; Statement stmt = null; Class.forName(JDBC_DRIVER); conn = DriverManager.getConnection(DB_URL, USER, PASS); stmt = conn.createStatement(); String sql = "CREATE DATABASE STUDENTS"; stmt.executeUpdate(sql); System.out.println("Database created successfully..."); stmt.close(); conn.close(); } }