0% found this document useful (0 votes)
8 views3 pages

AP20ASHWIIN

The document describes two Java programs. The first program updates a student record in a database by changing the name to "jack" where the student ID is 1. It executes the update statement and prints a message depending on the number of rows updated. The second program deletes product records from a database where the price is over 500 and the product ID is "P1234". It also executes the delete statement and prints a message depending on the number of rows deleted. Both programs connect to a MySQL database, prepare and execute SQL statements, then close the connection.

Uploaded by

gmpawar003
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)
8 views3 pages

AP20ASHWIIN

The document describes two Java programs. The first program updates a student record in a database by changing the name to "jack" where the student ID is 1. It executes the update statement and prints a message depending on the number of rows updated. The second program deletes product records from a database where the price is over 500 and the product ID is "P1234". It also executes the delete statement and prints a message depending on the number of rows deleted. Both programs connect to a MySQL database, prepare and execute SQL statements, then close the connection.

Uploaded by

gmpawar003
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/ 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