Practical No.
20
1) Write a program to delete a record from a table.
package MyPrject;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
public class resultsetEx {
public static void main(String[] args) {
try
{
Class.forName("com.mysql.cj.jdbc.Driver");
System.out.println("Driver Loaded...");
Connection
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/MSBTE","root","m
obile");
System.out.println("Connection Established...");
Statement su=con.createStatement();
System.out.println("Table Created...");
su.executeUpdate("delete from Student where Name='Saee'");
System.out.println("Deletion Successful in Student
table...");
con.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
Output:
Before:
After:
2) Write a program to update name of a student from Jack to John.
package MyProject;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class EXPtEx {
public static void main(String[] args) {
try
Class.forName("com.mysql.cj.jdbc.Driver");
System.out.println("Driver Loaded...");
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/MSBTE","root","mobile");
System.out.println("Connection Established...");
Statement su=con.createStatement();
Statement sc=con.createStatement();
Statement si=con.createStatement();
sc.executeUpdate("create table expt_20(Name varchar(30),percentage Integer)");
System.out.println("Table Created...");
si.executeUpdate("insert into expt_20 values('Jack',88)");
System.out.println("Value Inserted...");
su.executeUpdate("update expt_20 set Name='Jack' where Name='John')");
con.close();
}
catch(Exception e)
System.out.println(e);
Output: