Nearby lessons

27 of 37

JDBC - Create Database

📌 What You Will Learn
  • Create a MySQL database with SQL
  • Learn the CREATE DATABASE syntax
  • Know how to select a database before working on it

Create your first MySQL database — the CREATE DATABASE statement, the USE command, and how the database name appears in your JDBC connection URL.

What is a Database?

Before you can store data with JDBC, a database must exist on your MySQL server. A database is a container that holds tables — and each table holds rows of related data (like employees, products or orders).

In simple words: a database is a folder on the server, and tables are the files inside it.

CREATE DATABASE Syntax

One line of SQL creates a database:

Example02
JCode Cell
1CREATE DATABASE company;
Output
Query OK, 1 row affected (0.01 sec)

USE — Pick the Database

Creating the database does not switch to it. Tell MySQL which database you want to work in:

Example03
JCode Cell
1USE company;
Output
Database changed

The Database in the JDBC URL

In JDBC you do not use USE — the database name goes straight into the connection URL:

Example04
JCode Cell
1String url = "jdbc:mysql://localhost:3306/company";
2Connection con = DriverManager.getConnection(url, "root", "password");

Check What Databases Exist

To list every database on the server:

Example05
JCode Cell
1SHOW DATABASES;
Output
+--------------------+
| Database           |
+--------------------+
| company            |
| information_schema |
| mysql              |
+--------------------+
📝 Key Takeaways
  • CREATE DATABASE makes a new, empty database
  • USE picks the database for the rest of your session
  • In JDBC, the database name goes inside the connection URL