Prac 19
Prac 19
Prac 19
5 a
database server.
Or
Develop a program to update a record in database table
Code:
import java.sql.*;
public class Prac_19_1 {
public Prac_19_1() {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
Connection con =
DriverManager.getConnection("jdbc:mysql://localhost:3306/MSBTE", "root", "");
String updateQuery = "UPDATE student SET name = ? WHERE roll_no = ?";
PreparedStatement pstmt = con.prepareStatement(updateQuery);
pstmt.setString(1, "Pra");
pstmt.setInt(2, 3302);
int rowsAffected = pstmt.executeUpdate();
if (rowsAffected > 0) {
System.out.println("Record updated successfully.");
} else {
System.out.println("No record found with the given roll_no.");
}
con.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new Prac_19_1();
}
}
Output:
Before
After
2. Develop JDBC program to retrieve data using ResultSet
Code:
import java.sql.*;
public class Prac_19_2 {
public Prac_19_2() {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
Connection con =
DriverManager.getConnection("jdbc:mysql://localhost:3306/Pratik", "root", "");
System.out.println("Student Data:");
while (rs.next()) {
int roll_no = rs.getInt("roll_no");
String name = rs.getString("name");
int percentage = rs.getInt("percentage");
System.out.println("Roll No: " + roll_no + ", Name: " + name + ", Percentage: " +
percentage);
}
con.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new Prac_19_2();
}
}
Output: