Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
22 views

Node Js

Uploaded by

Sriya
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

Node Js

Uploaded by

Sriya
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

COMPUTER SCIENCE AND ENGINEERING

PARTICIAPATORY REPORT ASSIGNMENT

RJS LAP PROJECT

NAME : N.SRIYA
ROLL.NO : 22P61A05G7
BRANCH/SECTION : CSE/C
LAB : REACT JS
FACULTY NAME : DR.SUBHADRA
AIM :
Develop a java stand alone application that connects with database and perform
the CRUD Operations on the database table.

Introduction to CRUD Operations :


CRUD stands for Create, Read, Update, and Delete. These are the four basic
operations that can be performed on data in a database. The provided Java
program demonstrates how to perform these operations using JDBC (Java
Database Connectivity) with a MySQL database. Here's an introduction to each
operation:

 Create (Insert) :
This operation involves adding new records to a database table. In the program,
the insertData method handles the creation of new records byinserting a student's
name and address into the student table.

 Read (Select) :
Reading data from the database is essential for retrieving and displayingexisting
records. The readData method in the program retrieves and displays all records
from the student table.

 Update :
The update operation is used to modify existing records in the database. The
updateData method in the program updates a student's name and address based
on their ID.

 Delete :
Deleting records from the database removes unwanted or obsolete data. The
deleteData method in the program deletes a record from the studenttable based on
the student's ID.
Java Program Implementation :
The Java program provided uses JDBC to connect to a MySQL database and
perform the CRUD operations. Here's a breakdown of the main components of
the program:

 Database Connection :
The program establishes a connection to the MySQL database using the
DriverManager.getConnection method, which requires the database URL,
username, and password.

 User Interaction :
The program uses a Scanner object to read input from the user, allowingthem to
choose and execute different CRUD operations.

 Statement Execution :
The program creates Statement objects to execute SQL queries for each CRUD
operation.

 Exception Handling :
Proper exception handling is implemented to catch and display SQL errors.
SOURCE CODE :

import java . sql .*; import


java . util . Scanner;

public class CRUDOperations {

privatestaticfinalString URL = "jdbc:mysql://localhost:3306/mydb";private static


final String USER = "root";
private static final String PASSWORD = "password";

public static void main(String[] args) { Scanner


scanner = new Scanner(System.in);

try (Connection connection = DriverManager.getConnection(URL, USER,


PASSWORD)) {
while (true) {

System.out.println("Select an operation:");
System.out.println("1. Insert");
System.out.println("2. Read");
System.out.println("3. Update");
System.out.println("4. Delete");
System.out.println("5. Exit");

int choice = scanner .nextInt();


switch(choice){ca
se 1:
insertData(connection,scanner);break;
case 2:
readData(connection);
break;

case 3:

updateData(connection, scanner);
break;
case 4:
deleteData(connection,scanner);break;
case 5:
System.out.println("Exiting...");
return;

default:

System.out.println("Invalid choice. Please try again.");

} catch (SQLException e) {

System.err.println("Database error: " + e.getMessage());


}

private staticvoid insertData(Connection connection, Scanner


scanner) { try (Statement stmt = connection.createStatement()) {
System.out.println("Enter student name:"); String
name = scanner.next(); System.out.println("Enter
student address:");String address = scanner.next();

String insertQuery = "INSERT INTO student (name, address)


VALUES ('" + name + "', '" + address + "')";
stmt.executeUpdate(insertQuery); System.out.println("Data
inserted successfully.");
} catch (SQLException e) {

System.err.println("Insert error: " + e.getMessage());

private static void readData(Connection connection) { try


(Statement stmt = connection.createStatement()) { String
selectQuery = "SELECT * FROM student";
ResultSet rs = stmt.executeQuery(selectQuery);
System.out.println("ID\tName\tAddress");while
(rs.next()) {
int id = rs.getInt("id");

String name = rs.getString("name"); String


address = rs.getString("address");
System.out.println(id + "\t" + name + "\t" + address);

} catch (SQLException e) {

System.err.println("Read error: " + e.getMessage());

private static void updateData(Connection connection, Scanner


scanner) {
try (Statement stmt = connection.createStatement())
{System.out.println("Enter student ID to update:");
int id = scanner.nextInt();

System.out.println("Enter new student name:"); String


name = scanner.next(); System.out.println("Enter new
student address:");String address = scanner.next();

String updateQuery = "UPDATE student SET name='" + name


+ "', address='" + address + "' WHERE id=" + id;
stmt.executeUpdate(updateQuery);
System.out.println("Data updated successfully.");

} catch (SQLException e) {

System.err.println("Update error: " + e.getMessage());

private static void deleteData(Connection connection, Scanner


scanner) {
try (Statement stmt = connection.createStatement())
{System.out.println("Enter student ID to delete:");
int id = scanner.nextInt();

String deleteQuery = "DELETE FROM student WHERE id="


+id;

stmt.executeUpdate(deleteQuery); System.out.println("Data
deleted successfully.");

} catch (SQLException e) {

System.err.println("Delete error: " + e.getMessage());

}
}
Running the Program :

• Create the Database and Table: Execute the provided SQL commands to
set up the mydb database and student table.
• Add JDBC Driver: Ensure the MySQL Connector/J JAR file is added to
your project's classpath. • Compile and Run: Compile and run the Java
program .

1. Initial Setup

MySQL Database and Table Creation:

2. Java Code:
Assuming the Java code provided in CRUDOperations.java is compiledand run:
3. Program Start :

OUTPUT:

Insert Operation :

Explanation : The user enters a name and address, and the program
constructs and executes an INSERT SQL statement, adding a new record to the
student table.
Read Operation (to see the current data) :
Explanation : The program executes a SELECT SQL statement to retrieve
and display all records in the student table. The output shows the newly inserted
record with ID 1.

Insert Another Record :

Explanation : Similar to the first insert operation, a new record is added to


the table with the provided name and address.

Read Operation (to see the updated data) :


Explanation : The program retrieves and displays both records now present
in the student table.

Update Operation (update John's address) :

Explanation : The user specifies the ID of the record to update and provides
new values for the name and address. The program constructs and executes an
UPDATE SQL statement to modify the record.

Delete Operation (delete Jane's record) :


Explanation : The user specifies the ID of the record to delete. Theprogram
constructs and executes a DELETE SQL statement to remove the record from
the student table.
Exit :

Explanation : The user chooses to exit the program, terminating theloop and
closing the application.

Summary :

Create (Insert): Successfully adds new records to the database.

Read (Select): Retrieves and displays records from the database.

Update: Modifies existing records based on the specified criteria.

Delete : Removes records from the database based on the specified


criteria.

Conclusion :
The program effectively illustrates how to use JDBC for performing CRUD
operations on a MySQL database. It shows the interaction between the user and
the database, ensuring that data is correctly inserted, read, updated, and deleted.
Proper error handling and resource management are implemented to handle
potential issues and ensure smooth operation.

You might also like