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

Prac 19

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views3 pages

Prac 19

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

1. Write a Program to update row of student table from MSBTE database using Mysql 5.

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", "");

Statement stmt = con.createStatement();


ResultSet rs = stmt.executeQuery("SELECT * FROM student");

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:

You might also like