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

Week-7 Program

Uploaded by

pocacif488
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)
1 views

Week-7 Program

Uploaded by

pocacif488
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/ 6

7.

Write a program to perform CRUD


operations on the student table in
a database using JDBC.

import java.sql.*;

public class StudentCRUD


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

static final String DB_URL =


"jdbc:mysql://localhost:3306/universit
y";
static final String USER = "root";
static final String PASSWORD =
"vbit@123";

public static void main(String[]


args) {
Connection connection = null;
Statement statement = null;

try {
Class.forName(JDBC_DRIVER);
connection =
DriverManager.getConnection(DB_URL,
USER, PASSWORD);
statement =
connection.createStatement();

// Create
int newStudentId =
createStudent(statement, "John Doe",
20);
System.out.println("New
student created with ID: " +
newStudentId);
int newStudentId1 =
createStudent(statement, "Paul", 33);
System.out.println("New
student created with ID: " +
newStudentId);

// Read
System.out.println("Student details
before update:");
readStudent(statement,
newStudentId);

// Update
updateStudent(statement,
newStudentId, "vbit", 21);
System.out.println("Student details
after update:");
readStudent(statement,
newStudentId);

// Delete
//deleteStudent(statement,
newStudentId);
//System.out.println("Student
deleted.");

} catch
(ClassNotFoundException | SQLException
e) {
e.printStackTrace();
} finally {
try {
if (statement != null)
statement.close();
if (connection !=
null) connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}

private static int


createStudent(Statement statement,
String name, int age) throws
SQLException {
String insertQuery = "INSERT
INTO student (name, age) VALUES ('" +
name + "', " + age + ")";
statement.executeUpdate(insertQuery,
Statement.RETURN_GENERATED_KEYS);

try (ResultSet generatedKeys =


statement.getGeneratedKeys()) {
if (generatedKeys.next())
{
return
generatedKeys.getInt(1);
} else {
throw new
SQLException("Creating student failed,
no ID obtained.");
}
}
}
private static void
readStudent(Statement statement, int
studentId) throws SQLException {
String selectQuery = "SELECT *
FROM student WHERE id = " + studentId;
ResultSet resultSet =
statement.executeQuery(selectQuery);

while (resultSet.next()) {
System.out.println("ID: "
+ resultSet.getInt("id"));
System.out.println("Name:
" + resultSet.getString("name"));
System.out.println("Age: "
+ resultSet.getInt("age"));
}

resultSet.close();
}

private static void


updateStudent(Statement statement, int
studentId, String newName, int newAge)
throws SQLException {
String updateQuery = "UPDATE
student SET name = '" + newName + "',
age = " + newAge + " WHERE id = " +
studentId;
statement.executeUpdate(updateQuery);
}

private static void


deleteStudent(Statement statement, int
studentId) throws SQLException {
String deleteQuery = "DELETE
FROM student WHERE id = " + studentId;
statement.executeUpdate(deleteQuery);
}
}

You might also like