0% found this document useful (0 votes)
13 views3 pages

JDBC

Uploaded by

Monika Kumari
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)
13 views3 pages

JDBC

Uploaded by

Monika Kumari
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
You are on page 1/ 3

1.

Set Up MySQL Table:


CREATE DATABASE IF NOT EXISTS EmployeeDB;

USE EmployeeDB;

CREATE TABLE IF NOT EXISTS Employee (


Name VARCHAR(50),
ID INT PRIMARY KEY,
Designation VARCHAR(50),
Department VARCHAR(50),
Salary DECIMAL(10, 2),
Income DECIMAL(10, 2)
);
2. Java Code to Insert Values into Employee Table:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class EmployeeDBExample {

// JDBC URL, username, and password for MySQL


static final String JDBC_URL =
"jdbc:mysql://localhost:3306/EmployeeDB";
static final String USER = "root"; // Update to your MySQL
username
static final String PASSWORD = "password"; // Update to your
MySQL password

public static void main(String[] args) {


Connection conn = null;
PreparedStatement pstmt = null;

try {
// Step 1: Register JDBC driver
Class.forName("com.mysql.cj.jdbc.Driver");

// Step 2: Open a connection


System.out.println("Connecting to database...");
conn = DriverManager.getConnection(JDBC_URL, USER,
PASSWORD);

// Step 3: Create SQL insert statement


String sql = "INSERT INTO Employee (Name, ID, Designation,
Department, Salary, Income) VALUES (?, ?, ?, ?, ?, ?)";

// Step 4: Set the values


pstmt = conn.prepareStatement(sql);
pstmt.setString(1, "John Doe");
pstmt.setInt(2, 101);
pstmt.setString(3, "Software Engineer");
pstmt.setString(4, "IT");
pstmt.setDouble(5, 75000.00);
pstmt.setDouble(6, 2000.00);

// Execute the insert command


int rowsInserted = pstmt.executeUpdate();
if (rowsInserted > 0) {
System.out.println("A new employee was inserted
successfully!");
}

// Insert another employee


pstmt.setString(1, "Jane Smith");
pstmt.setInt(2, 102);
pstmt.setString(3, "Data Analyst");
pstmt.setString(4, "Business Intelligence");
pstmt.setDouble(5, 65000.00);
pstmt.setDouble(6, 1800.00);

// Execute the insert command


rowsInserted = pstmt.executeUpdate();
if (rowsInserted > 0) {
System.out.println("Another employee was inserted
successfully!");
}

} catch (SQLException se) {


// Handle errors for JDBC
se.printStackTrace();
} catch (Exception e) {
// Handle errors for Class.forName
e.printStackTrace();
} finally {
// Step 5: Finally, close resources
try {
if (pstmt != null) pstmt.close();
if (conn != null) conn.close();
} catch (SQLException se) {
se.printStackTrace();
}
}
}
}

OUTPUT:

NAME – MONIKA KUMARI

REG NO- 22BCE10972

You might also like