0% found this document useful (0 votes)
9 views4 pages

Java Database Creation Example

The document provides a Java program that connects to a MySQL database and creates a sample database named 'sampleDB' along with a table called 'employees'. It includes the necessary SQL queries and connection details, as well as output messages indicating the success of each operation. The program demonstrates basic database operations such as creating a database, using it, and creating a table within it.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views4 pages

Java Database Creation Example

The document provides a Java program that connects to a MySQL database and creates a sample database named 'sampleDB' along with a table called 'employees'. It includes the necessary SQL queries and connection details, as well as output messages indicating the success of each operation. The program demonstrates basic database operations such as creating a database, using it, and creating a table within it.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

//Name: Om Attarde

//Class: SYCO

//Roll No: 21

//Subject: Java programming

//Subject Code: 314317

//Practical Name:27. Write a program to create sample database.


Make connectivity with database

INPUT:

import [Link].*;

public class CreateDatabaseExample {

public static void main(String[] args) {

// Database credentials

String url = "jdbc:mysql://localhost:3306/";

String username = "root"; // Replace with your MySQL username

String password = "password"; // Replace with your MySQL password

// SQL queries

String createDatabaseQuery = "CREATE DATABASE IF NOT EXISTS


sampleDB";

String useDatabaseQuery = "USE sampleDB";

String createTableQuery = "CREATE TABLE IF NOT EXISTS employees


("

+ "id INT PRIMARY KEY, "


+ "name VARCHAR(100), "

+ "age INT)";

// Establishing connection and executing SQL queries

try (Connection conn = [Link](url, username,


password)) {

[Link]("Connected to MySQL successfully.");

// Create database

try (Statement stmt = [Link]()) {

[Link](createDatabaseQuery);

[Link]("Database 'sampleDB' created or already exists.");

// Use the database

[Link](useDatabaseQuery);

[Link]("Using 'sampleDB' database.");

// Create table

[Link](createTableQuery);

[Link]("Table 'employees' created or already exists.");

// Verifying connection by querying the database

try (Statement stmt = [Link]();

ResultSet rs = [Link]("SHOW TABLES")) {

[Link]("Tables in 'sampleDB' database:");


while ([Link]()) {

[Link]([Link](1));

} catch (SQLException e) {

[Link]();

}
OUTPUT:

C:\java pro>javac [Link]

C:\java pro>java CreateDatabaseExample

Connected to MySQL successfully.

Database 'sampleDB' created or already exists.

Using 'sampleDB' database.

Table 'employees' created or already exists.

Tables in 'sampleDB' database:

employees

You might also like