0% found this document useful (0 votes)
5 views

Exp 8

This Java program performs CRUD (create, read, update, delete) operations on a MySQL database using JDBC. It connects to a database called javadb, defines queries to insert, select, update and delete records from a table called employees, and offers the user a menu to choose which operation to perform. Based on the user's input, it executes the corresponding query and prints out the results.

Uploaded by

Om Jadhav
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Exp 8

This Java program performs CRUD (create, read, update, delete) operations on a MySQL database using JDBC. It connects to a database called javadb, defines queries to insert, select, update and delete records from a table called employees, and offers the user a menu to choose which operation to perform. Based on the user's input, it executes the corresponding query and prints out the results.

Uploaded by

Om Jadhav
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

//Name-Mitali Santosh Panzade

//Roll No-304B065

import java.io.*;
import java.sql.*;

public class CrudOperations {

static final String JDBC_DRIVER = "com.mysql.cj.jdbc.Driver";


static final String DB_URL = "jdbc:mysql://localhost/javadb";

static final String USER = "root";


static final String PASS = "yash25";

public static void main(String[] args) {


Connection conn = null;
Statement stmt = null;
try {
// Register JDBC driver
Class.forName(JDBC_DRIVER);

// Open a connection
conn = DriverManager.getConnection(DB_URL, USER, PASS);

// Execute a query
stmt = conn.createStatement();

// Switch case to perform CRUD operations


int choice = 0;
while (true) {
System.out.println("\n1. Create\n2. Read\n3. Update\n4.
Delete\n5. Exit\n");
System.out.print("Enter your choice: ");
try {
choice = Integer.parseInt(new BufferedReader(new
InputStreamReader(System.in)).readLine());
} catch (Exception e) {
System.out.println("Invalid input. Please try again.");
continue;
}

switch (choice) {
case 1:
// Create operation
String insertQuery = "INSERT INTO employees VALUES (1,
'Yash', '[email protected]',20 )";
int rowsInserted = stmt.executeUpdate(insertQuery);
if (rowsInserted > 0) {
System.out.println("Record inserted successfully.");
}
break;

case 2:
// Read operation
String selectQuery = "SELECT * FROM employees";
ResultSet rs = stmt.executeQuery(selectQuery);
while (rs.next()) {
int id = rs.getInt("id");
String name = rs.getString("name");
int age = rs.getInt("age");
int salary = rs.getInt("salary");

System.out.println("ID: " + id + ", Name: " + name +


", Age: " + age + ", Salary: " + salary);
}
break;

case 3:
// Update operation
String updateQuery = "UPDATE employees SET salary =
60000 WHERE id = 1";
int rowsUpdated = stmt.executeUpdate(updateQuery);
if (rowsUpdated > 0) {
System.out.println("Record updated successfully.");
}
break;

case 4:
// Delete operation
String deleteQuery = "DELETE FROM employees WHERE id =
1";
int rowsDeleted = stmt.executeUpdate(deleteQuery);
if (rowsDeleted > 0) {
System.out.println("Record deleted successfully.");
}
break;

case 5:
// Exit
System.out.println("Exiting...");
break;

default:
System.out.println("Invalid choice. Please try
again.");
break;
}
if (choice == 5) {
break;
}
}
} catch (SQLException se) {
se.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (stmt != null)
stmt.close();
} catch (SQLException se2) {
}
try {
if (conn != null)
conn.close();
} catch (SQLException se) {
se.printStackTrace();
}
}
}
}
OUPUT-

You might also like