- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to create a database in MySQL using a JDBC API?
A. In general, you can create a database using the CREATE DATABASE query.
Syntax
CREATE DATABASE DatabaseName;
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. Passing URL (String), username (String), password (String) as parameters to it.
Create Statement: Create a Statement object using the createStatement() method of the Connection interface.
Execute the Query: Execute the query using the execute() method of the Statement interface.
Example:
Following JDBC program establishes connection with MySQL and creates a database named mydatabase:
import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.Statement; public class CreateDatabaseExample { public static void main(String args[]) throws SQLException { //Registering the Driver DriverManager.registerDriver(new com.mysql.jdbc.Driver()); //Getting the connection String mysqlUrl = "jdbc:mysql://localhost/"; Connection con = DriverManager.getConnection(mysqlUrl, "root", "password"); System.out.println("Connection established......"); //Creating the Statement Statement stmt = con.createStatement(); //Query to create a database String query = "CREATE database MyDatabase"; //Executing the query stmt.execute(query); System.out.println("Database created"); } }
Output:
Connection established...... Database created......
The show databases command gives you the list of databases in MySQL. If you verify the list of databases using this command, you can see the newly created database as:
mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | base | | details | | exampledatabase | | logging | | mydatabase | | mydb | | mysql | | performance_schema | | students | | sys | | world | +--------------------+ 12 rows in set (0.00 sec)
- Related Articles
- How to create a table in a database using JDBC API?
- How to create a function in a database using JDBC API?
- How to create a Stored procedure in a database using JDBC API?
- How to create a Stored procedure in Oracle database using JDBC API?
- How to drop a database using JDBC API?
- How to select or, shift to another database in MySQL using a JDBC API?
- How to drop a table from a database using JDBC API?
- How to drop a table from Oracle database using JDBC API?
- How to call an existing function in a database using JDBC API?
- How to insert a record into a table in a database using JDBC API?
- How to call an existing stored procedure in a database using JDBC API?
- How to retrieve a record from an existing table in a database using JDBC API?
- How to remove a record from an existing table in a database using JDBC API?
- How to delete a column from an existing table in a database using JDBC API?
- How to drop constraint on a column of a table in a database using JDBC API?
