You are on page 1of 3

Name:- ASHWIN

PAWAR Roll
No:- 23
Practical No. : 20

Program1:-
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
public class update_ex {
public static void main(String[] args) {
try {
// JDBC URL, username, and password of MySQL server
String url = "jdbc:mysql://localhost:3306/update_db";
String user = "root";
String password = "123456789";
// Establish a connection
Connection connection =
DriverManager.getConnection("jdbc:mysql://localhost:3306/update_db",
"root",
"123456789");
// Prepare the update statement with the desired values
String updateQuery = "UPDATE student SET name = 'jack' WHERE stud_ID =
1";
PreparedStatement preparedStatement =
connection.prepareStatement(updateQuery);
// Execute the update statement
int rowsUpdated = preparedStatement.executeUpdate();
if (rowsUpdated > 0) {
System.out.println("Record updated successfully!");
} else {
System.out.println("No records were updated.");
}
// Close the connection
connection.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}

Output:-
Program2:-

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
public class DeleteProductRecords {
public static void main(String[] args) {
try {
// JDBC URL, username, and password of MySQL server
String url = "jdbc:mysql://localhost:3306/product_db";
String user = "root";
String password = "123456789";
// Establish a connection
Connection connection =
DriverManager.getConnection("jdbc:mysql://localhost:3306/product_db",
"root",
"123456789");
// Prepare the delete statement
String deleteQuery = "DELETE FROM products WHERE price > 500 AND
product_id = 'P1234'";
PreparedStatement preparedStatement =
connection.prepareStatement(deleteQuery);
// Execute the delete statement
int rowsDeleted = preparedStatement.executeUpdate();
if (rowsDeleted > 0) {
System.out.println("Records deleted successfully!");
} else {
System.out.println("No records were deleted.");
}
// Close the connection
connection.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
Output:-

You might also like