Advanced Java Programming Practical 20
Question 1
Develop a program to update name of a student from Jack to John.
UpdateStudent.java
Java
import java.sql.*;
public class UpdateStudent
{
public static void main(String args[])
{
try
{
Class.forName("com.mysql.jdbc.Driver");
System.out.println("Driver loaded");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/javadb1", "root", "");
System.out.println("Connection to the database created");
Statement stmt = con.createStatement();
String query = "update student set name='John' where name='Jack'";
int i= stmt.executeUpdate(query);
System.out.println(i+" records updated");
con.close();
}
catch (SQLException e)
{
System.out.println(e);
}
catch (ClassNotFoundException e)
{
System.out.println(e);
}
}
}
Output

Question 2
Develop a program to delete all record for a product whose "price is greater than 500" and Id is "P1234".
DeletePoduct.java
Java
import java.sql.*;
public class DeletePoduct
{
public static void main(String args[])
{
try
{
Class.forName("com.mysql.jdbc.Driver");
System.out.println("Driver loaded");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/javadb1", "root", "");
System.out.println("Connection to the database created");
Statement stmt = con.createStatement();
String query = "delete from product where id='P1234' and price>500";
int i= stmt.executeUpdate(query);
System.out.println(i+" records deleted");
con.close();
}
catch (SQLException e)
{
System.out.println(e);
}
catch (ClassNotFoundException e)
{
System.out.println(e);
}
}
}
Output
