Ajp Exp 20 Op
Ajp Exp 20 Op
Ajp Exp 20 Op
Program Code:
1. Write a Program to delete a record from a table.
import java.sql.*;
class DeleteStudentRecord {
public static void main(String[] args) {
int rollNoToDelete = 07; // Example Roll_No to delete
String url = "jdbc:mysql://localhost:3306/Student";
String user = "root";
String password = "root";
try (Connection con = DriverManager.getConnection(url, user, password);
PreparedStatement pstmt = con.prepareStatement("DELETE FROM Student WHERE Roll_No =
?")) {
pstmt.setInt(1, rollNoToDelete);
int rowsAffected = pstmt.executeUpdate();
if (rowsAffected > 0) {
System.out.println("Deleted Student with Roll_No " + rollNoToDelete);
} else {
System.out.println("No student found with Roll_No " + rollNoToDelete);
}
System.out.println("\nExecuted by 43 Shresth and 58 Smith");
} catch (SQLException e) { e.printStackTrace(); }
}
}
2. Write the output of following JDBC code.
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
pstmt.setString(1, newName);
pstmt.setString(2, oldName);
int rowsAffected = pstmt.executeUpdate();
if (rowsAffected > 0) {
System.out.println("Updated Name from " + oldName + " to " + newName);
} else {
System.out.println("No student found with Name " + oldName);
}
} catch (SQLException e) {
e.printStackTrace();
} }
}
2. Develop a program to delete all record for a product whose “prize is greater than 500” and Id
is “P1234”.
import java.sql.*;
class DeleteProductRecords {
public static void main(String[] args) {
String productId = "P1238";
double priceThreshold = 500.0;
String url = "jdbc:mysql://localhost:4008/Student";
String user = "root";
String password = "dhruwalmakwana";
try (Connection con = DriverManager.getConnection(url, user, password);
PreparedStatement pstmt = con.prepareStatement("DELETE FROM Product WHERE Id = ? AND Price >
?")) {
pstmt.setString(1, productId);
pstmt.setDouble(2, priceThreshold);
int rowsAffected = pstmt.executeUpdate();
if (rowsAffected > 0) {
System.out.println("Deleted " + rowsAffected + " record(s) for product with Id " + productId + " where price
is greater than " + priceThreshold);
} else {
System.out.println("No records found for product with Id " + productId + " where price is greater than " +
priceThreshold);
}
} catch (SQLException e) {
e.printStackTrace();
}
}