Ajp Exp 20 Op

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

X.

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;

public class UpdateQuery {


public static void main(String[] args) {
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:4008/Student", "root",
"dhruwalmakwana");
PreparedStatement st = con.prepareStatement("UPDATE student SET Roll_No = 2 WHERE
name = 'Dhruv'");
st.executeUpdate();
System.out.println("Updated!!");
} catch (Exception ex) {
System.out.println(ex);
}
}
}
XIII. Exercise:
1. Develop a program to update name of a student from Jack to John.
import java.sql.*;
class UpdateStudentName {
public static void main(String[] args) {
String oldName = "jack"; // Current name to update
String newName = "John"; // New name to set
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("UPDATE Student SET Name = ? WHERE Name = ?")) {

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();
}
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy