English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
This tutorial provides an example of how to create a database using a JDBC application. Before executing the following example, please ensure that you have the following conditions:
You should have administrative privileges to create a database in the given schema. To execute the following example, you need to replaceUsernameAndPasswordReplace with the actual username and password.
Your MySQL or any database you are using has already started and is running.
To create a new database using a JDBC application, you need to follow the following steps-
Import package:It requires you to include the software package that contains the JDBC classes required for database programming. Typically, using import java.sql.* is enough.
Register JDBC driver: It requires you to initialize the driver so that you can open a communication channel with the database.
Establish connection:It is necessary to useDriverManager.getConnection()A method to create a Connection object representing a physical connection to the database server. To create a new database, no database name needs to be provided when preparing the database URL, as shown in the following example.
Execute query: It is necessary to use an object of the Statement type to construct SQL statements and submit them to the database.
Cleanup:It is necessary to explicitly close all database resources instead of relying on JVM's garbage collection.
Copy and paste the following example into JDBCExample.java, as follows compile and run:
//Step1Import the required software packages import java.sql.*; public class JDBCExample { // JDBC driver name and database URL static final String JDBC_DRIVER = "com.mysql.jdbc.Driver"; static final String DB_URL = "jdbc:mysql://localhost/"; // Database credentials static final String USER = "username"; static final String PASS = "password"; public static void main(String[] args) { Connection conn = null; Statement stmt = null; try{ //Step2Register JDBC driver Class.forName("com.mysql.jdbc.Driver"); //Step3Establish connection System.out.println("Connecting to database..."); conn = DriverManager.getConnection(DB_URL, USER, PASS); //Step4Execute query System.out.println("Creating database..."); stmt = conn.createStatement(); String sql = "CREATE DATABASE STUDENTS"; stmt.executeUpdate(sql); System.out.println("Database created successfully..."); catch(SQLException se){ //Handle JDBC error se.printStackTrace(); Handle Class.forName error //e.printStackTrace(); finally{ } //Used for closing resources try{ stmt.close(); catch(SQLException se }2){ } try{ if(conn!=null) conn.close(); catch(SQLException se){ se.printStackTrace(); } }//End try System.out.println("Goodbye!"); }//End main }//End JDBCExample
Now, let's compile the above example as follows:
C:\>javac JDBCExample.java C:\>
RuntimeJDBCExampleIt will produce the following results-
C:\>java JDBCExample Connecting to database... Creating database... Database created successfully... Goodbye! C:\>