0% found this document useful (0 votes)
13 views5 pages

Java Practical No 28

The document provides Java programs for implementing SQL Insert, Update, and Delete statements for a Student database. It includes code snippets for inserting new student records, updating existing records, and deleting a record based on StudentId. Each program handles database connections and outputs the results of the operations performed.

Uploaded by

dipya2121
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)
13 views5 pages

Java Practical No 28

The document provides Java programs for implementing SQL Insert, Update, and Delete statements for a Student database. It includes code snippets for inserting new student records, updating existing records, and deleting a record based on StudentId. Each program handles database connections and outputs the results of the operations performed.

Uploaded by

dipya2121
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/ 5

Practical No 28

Practical Related Questions:


3. Write a program for Implementing
Insert Statement.
Ans:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class StudentInsertion {
private static final String DB_URL = "jdbc:mysql://localhost:3306/example";
private static final String USER = "root";
private static final String PASSWORD = "root123";
public static void main(String[] args) {
try (Connection connection = DriverManager.getConnection(DB_URL,
USER, PASSWORD);
Statement statement = connection.createStatement()) {
String query2 = "INSERT INTO Student (StudentId, FirstName, LastName,
Age, Grade) " +
"VALUES (2, 'Emma', 'Smith', 22, 'A+')";
statement.executeUpdate(query2);
String query3 = "INSERT INTO Student (StudentId, FirstName, LastName,
Age, Grade) " +
"VALUES (3, 'Michael', 'Johnson', 19, 'B')";
statement.executeUpdate(query3);
System.out.println("Students inserted successfully!");
} catch (SQLException e) {
System.err.println("Database error: " + e.getMessage());
}
}
}

O/P:

Inserted Data:

4. Write a program for Implementing


Update Statement.
Ans:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class StudentUpdate {
private static final String DB_URL = "jdbc:mysql://localhost:3306/example";
private static final String USER = "root";
private static final String PASSWORD = "root123";
public static void main(String[] args) {
try (Connection connection = DriverManager.getConnection(DB_URL,
USER, PASSWORD);
Statement statement = connection.createStatement()) {
String updateQuery1 = "UPDATE Student SET Grade = 'A+', Age = 21
WHERE StudentId = 1";
int rowsAffected1 = statement.executeUpdate(updateQuery1);
System.out.println("Rows affected by first update: " + rowsAffected1);
String updateQuery2 = "UPDATE Student SET FirstName = 'Emily',
LastName = 'Williams' WHERE StudentId = 2";
int rowsAffected2 = statement.executeUpdate(updateQuery2);
System.out.println("Rows affected by second update: " +
rowsAffected2);
String updateQuery3 = "UPDATE Student SET Age = 20, Grade = 'B+'
WHERE StudentId = 3";
int rowsAffected3 = statement.executeUpdate(updateQuery3);
System.out.println("Rows affected by third update: " + rowsAffected3);
} catch (SQLException e) {
System.err.println("Database error: " + e.getMessage());
}
}
}
O/P:

Updated data:

5. Write a program for Implementing Delete


Statement.
Ans:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class StudentDelete {
private static final String DB_URL = "jdbc:mysql://localhost:3306/example";
private static final String USER = "root";
private static final String PASSWORD = "root123";
public static void main(String[] args) {
try (Connection connection = DriverManager.getConnection(DB_URL,
USER, PASSWORD);
Statement statement = connection.createStatement()) {
String deleteQuery1 = "DELETE FROM Student WHERE StudentId = 1";
int rowsAffected1 = statement.executeUpdate(deleteQuery1);
System.out.println("Rows deleted by first query: " + rowsAffected1);
} catch (SQLException e) {
System.err.println("Database error: " + e.getMessage());
}
}
}

O/P:

After Deletion:

You might also like