X.
Program Code:
1. Write a Program to update row of student table from the database.
import java.sql.*;
class UpdateStudent {
public static void main(String[] args) {
int rollNoToUpdate = 58; // Example Roll_No to update
int newPercentage = 98; // New Percentage to set
try (Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/Student", "root", "root");
Statement st = con.createStatement()) {
String updateSQL = "UPDATE Student SET Percentage = " + newPercentage + " WHERE Roll_No = " +
rollNoToUpdate + ";";
int rowsAffected = st.executeUpdate(updateSQL);
if (rowsAffected > 0) {
System.out.println("Updated Student with Roll_No " + rollNoToUpdate + " to new Percentage " +
newPercentage);
} else {
System.out.println("No student found with Roll_No " + rollNoToUpdate);
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
2. Write the output of the following JDBC code.
import java.sql.*;
public class PreparedStmtEx {
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 stmt = con.prepareStatement("INSERT INTO Student VALUES (?, ?, ?)");
stmt.setInt(1, 101); // 1 specifies the first parameter (1st ? in the query)
stmt.setString(2, "Abhishek"); // 2 specifies the second parameter (2nd ? in the query)
stmt.setInt(3, 2022); // 3 specifies the third parameter (3rd ? in the query)
int i = stmt.executeUpdate();
System.out.println(i + " records inserted");
con.close();
} catch (Exception e) {
System.out.println(e);
}
}
}
XIII. Exercise:
1. Develop JDBC program to retrive data using ResultSet.
import java.sql.*;
class RetrieveStudents {
public static void main(String[] args) {
try (Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/Student", "root", "root");
Statement st = con.createStatement()) {
String query = "SELECT * FROM Student;";
ResultSet rs = st.executeQuery(query);
System.out.println("Retrieving data from Student table:");
while (rs.next()) {
int rollNo = rs.getInt("Roll_No");
String name = rs.getString("Name");
int percentage = rs.getInt("Percentage");
System.out.println("Roll_No: " + rollNo + ", Name: " + name + ", Percentage: " + percentage);
}
System.out.println("\nExecuted By 43 Shresth and 58 Smith");
} catch (SQLException e) {
e.printStackTrace();
}
}
}
2. Develop a program to update a record in database table.
import java.sql.*;
class UpdateStudentRecord {
public static void main(String[] args) {
int rollNoToUpdate = 19; // Example Roll_No to update
String newName = "John Doe"; // New Name to set
int newPercentage = 90; // New Percentage to set
try (Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/Student", "root", "root");
PreparedStatement pstmt = con.prepareStatement("UPDATE Student SET Name = ?, Percentage = ?
WHERE Roll_No = ?")) {
pstmt.setString(1, newName);
pstmt.setInt(2, newPercentage);
pstmt.setInt(3, rollNoToUpdate);
int rowsAffected = pstmt.executeUpdate();
if (rowsAffected > 0) {
System.out.println("Updated Student with Roll_No " + rollNoToUpdate + " to new Name: " + newName + "
and Percentage: " + newPercentage);
} else {
System.out.println("No student found with Roll_No " + rollNoToUpdate);
}
System.out.println("\nExecuted By 43 Shresth and 58 Smith");
} catch (SQLException e){ e.printStackTrace(); }
}
}