0% found this document useful (0 votes)
11 views11 pages

JDBC CRUD Operations in Java Guide

The document outlines the algorithm and implementation for connecting to a database using JDBC in Java, detailing steps for CRUD operations. It includes sample code for establishing a connection, executing SQL queries, and processing results. Additionally, it features an assessment rubric for evaluating the experiment.
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)
11 views11 pages

JDBC CRUD Operations in Java Guide

The document outlines the algorithm and implementation for connecting to a database using JDBC in Java, detailing steps for CRUD operations. It includes sample code for establishing a connection, executing SQL queries, and processing results. Additionally, it features an assessment rubric for evaluating the experiment.
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

School: ............................................................................................................. Campus: .......................................................

Academic Year: ...................... Subject Name: ........................................................... Subject Code: ..........................

Semester: ............... Program: ........................................ Branch: ......................... Specialization: ..........................

Date: .....................................

Name of the Experiement :

Coding Phase: Pseudo Code / Flow Chart / Algorithm

Algorithm to Connect to a Database Using JDBC in Java:

1. Load the Database Driver:

o You need to load the appropriate database driver class. This is done using the
[Link]() method (for older versions of JDBC) or automatically by the
JDBC driver in newer versions (if you are using the [Link]
API).

2. Establish a Connection:

o Use [Link]() to establish a connection to the database


by providing the appropriate URL, username, and password.

3. Create a Statement Object:

o Use [Link]() to create a statement object that will be


used to execute SQL queries.

4. Execute a Query:

o Use methods like executeQuery() for SELECT queries and executeUpdate() for
INSERT, UPDATE, DELETE queries.

5. Process the Result Set:

o If the query is a SELECT statement, the result will be stored in a ResultSet


object, and you can iterate through it to retrieve the data.

6. Close the Resources:

o Always close the database connection, statement, and result set to release
resources once the database operations are done.

Page No.............
Implementation Phase: Final Output (no error)

Input import [Link].*;

public class Main {


public static void main(String[] args) {
try {
[Link]("[Link]");
Connection con =
[Link]("jdbc:mysql://localhost:3306/account","root","Virat@345");
[Link]("Connected to Database");
[Link](con);

PreparedStatement ps = [Link]("select * from deposit_account


where account_type = 'Current' ");
PreparedStatement pt = [Link]("insert into deposit_account
values(2004,'Sneha Mehta','Savings',8000.00,8700.40,4.25,'Axis Bank')");
int rowsInserted = [Link]();

if (rowsInserted > 0) {
[Link]("Record inserted successfully.");
} else {
[Link]("Insertion failed.");
}

ResultSet output = [Link]();

while ([Link]()){
String account_type = [Link](3);
int account_number = [Link](1);
[Link](account_number+" "+account_type);
}
}
catch (ClassNotFoundException | SQLException e){
[Link]("Connection failed : "+[Link]());
} Output
}
}
School: ............................................................................................................. Campus: .......................................................

Academic Year: ...................... Subject Name: ........................................................... Subject Code: ..........................

Semester: ............... Program: ........................................ Branch: ......................... Specialization: ..........................

Date: .....................................

Name of the Experiement :

Coding Phase: Pseudo Code / Flow Chart / Algorithm

Algorithm to Perform CRUD Operations Using JDBC in Java:

1. Establish a Database Connection:

o First, load the database driver and establish a connection to the database
using [Link]().

2. Create (Insert) Operation:

o Use an INSERT INTO SQL query to add new records to the database.

o Execute the query using [Link]() method.

3. Read (Select) Operation:

o Use a SELECT SQL query to retrieve data from the database.

o Execute the query using [Link]() and retrieve the results


using a ResultSet.

4. Update Operation:

o Use an UPDATE SQL query to modify existing records in the database.

o Execute the query using [Link]().

5. Delete Operation:

o Use a DELETE SQL query to remove records from the database.

o Execute the query using [Link]().

6. Close the Resources:

o Always close the Connection, Statement, and ResultSet objects to release


resources.

Page No.............
Implementation Phase: Final Output (no error)

Input
import [Link].*;

public class CrudOperationsExample {

public static void createEmployee(Connection conn, int id, String name, double salary) {
String query = "INSERT INTO employee (EMPID, NAME, SALARY) VALUES (?, ?, ?)";
try (PreparedStatement pstmt = [Link](query)) {
[Link](1, id);
[Link](2, name);
[Link](3, salary);
[Link]();
[Link]("Employee created successfully.");
} catch (SQLException e) {
[Link]();
}
}

public static void readEmployees(Connection conn) {


String query = "SELECT * FROM employee";
try (Statement stmt = [Link](); ResultSet rs = [Link](query))
{
while ([Link]()) {
int id = [Link]("EMPID");
String name = [Link]("NAME");
double salary = [Link]("SALARY");
[Link]("ID: " + id + ", Name: " + name + ", Salary: " + salary);
}
} catch (SQLException e) {
[Link]();
}
}

public static void updateEmployeeSalary(Connection conn, int id, double newSalary) {


String query = "UPDATE employee SET SALARY = ? WHERE EMPID = ?";
try (PreparedStatement pstmt = [Link](query)) {
[Link](1, newSalary);
[Link](2, id);
int rowsUpdated = [Link]();
if (rowsUpdated > 0) {
[Link]("Employee salary updated successfully.");
} else {
Implementation Phase: Final Output (no error)

Input public static void deleteEmployee(Connection conn, int id) {


String query = "DELETE FROM employee WHERE EMPID = ?";
try (PreparedStatement pstmt = [Link](query)) {
[Link](1, id);
int rowsDeleted = [Link]();
if (rowsDeleted > 0) {
[Link]("Employee deleted successfully.");
} else {
[Link]("Employee not found with ID: " + id);
}
} catch (SQLException e) {
[Link]();
}
}

public static void main(String[] args) {


Connection conn = null;
try {
// Step 1: Establish a connection to the database
String url = "jdbc:mysql://localhost:3306/banker";
String username = "root";
String password = "Virat@345";
conn = [Link](url, username, password);

[Link]("Performing Create operation:");


createEmployee(conn, 501, "Ratan Tata", 550000);

[Link]("\nPerforming Read operation:");


readEmployees(conn);

[Link]("\nPerforming Update operation:");


updateEmployeeSalary(conn, 501, 200000);

[Link]("\nPerforming Read operation after update:");


readEmployees(conn);

[Link]("\nPerforming Delete operation:");


deleteEmployee(conn, 501);

[Link]("\nPerforming Read operation after deletion:");


readEmployees(conn);

} catch (SQLException e) {
[Link]();
} finally {
try {
if (conn != null) [Link]();
} catch (SQLException e) {
[Link]();
}
}
}
}
Implementation Phase: Final Output (no error)

Output
Create :_

Update :-
Implementation Phase: Final Output (no error)

Output

Delete :-

ASSESSMENT
Rubrics Full Mark Marks Obtained Remarks
Concept 10
Planning and Execution/ 10
Practical Simulation/ Programming
Result and Interpretation 10
Record of Applied and Action Learning 10
Viva 10
Total 50

Signature of fhe Student:


/\lame :
Signature of the Faculty: Regn. /\/o. :
ASSESSMENT
Rubrics Full Mark Marks Obtained Remarks
Concept 10
Planning and Execution/ 10
Practical Simulation/ Programming
Result and Interpretation 10
Record of Applied and Action Learning 10
Viva 10
Total 50

Signature of fhe Student:


/\lame :
Signature of the Faculty: Regn. /\/o. :
ASSESSMENT
Rubrics Full Mark Marks Obtained Remarks
Concept 10
Planning and Execution/ 10
Practical Simulation/ Programming
Result and Interpretation 10
Record of Applied and Action Learning 10
Viva 10
Total 50

Signature of fhe Student:


/\lame :
Signature of the Faculty: Regn. /\/o. :
ASSESSMENT
Rubrics Full Mark Marks Obtained Remarks
Concept 10
Planning and Execution/ 10
Practical Simulation/ Programming
Result and Interpretation 10
Record of Applied and Action Learning 10
Viva 10
Total 50

Signature of fhe Student:


/\lame :
Signature of the Faculty: Regn. /\/o. :
ASSESSMENT
Rubrics Full Mark Marks Obtained Remarks
Concept 10
Planning and Execution/ 10
Practical Simulation/ Programming
Result and Interpretation 10
Record of Applied and Action Learning 10
Viva 10
Total 50

Signature of fhe Student:


/\lame :
Signature of the Faculty: Regn. /\/o. :

You might also like