Node Js
Node Js
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.
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 :
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");
case 3:
updateData(connection, scanner);
break;
case 4:
deleteData(connection,scanner);break;
case 5:
System.out.println("Exiting...");
return;
default:
} catch (SQLException e) {
} catch (SQLException e) {
} catch (SQLException e) {
stmt.executeUpdate(deleteQuery); System.out.println("Data
deleted successfully.");
} catch (SQLException e) {
}
}
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
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.
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.
Explanation : The user chooses to exit the program, terminating theloop and
closing the application.
Summary :
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.