Practical 20: Write a program to update and delete a record from a database table.

Database:


Program to Update:

import java.sql.SQLException;
import java.sql.Statement;
import java.sql.DriverManager;
import java.sql.Connection;

public class UpdateStaticOracle 
{
 public static void main(String args[] ) throws java.sql.SQLException
 {
 oracle.jdbc.OracleDriver obj = new oracle.jdbc.OracleDriver();
 System.out.println("1 Driver loaded successfully");

 Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE","System","pass");
 if( con != null) 
  System.out.println("Connection established successfully");
 else
  System.out.println("Connection not established successfully");
 
 Statement st = con.createStatement();
 System.out.println("Statement established successful"); 
 
 String qry = "Update Student set Name='A' where id= 104";
 int count = st.executeUpdate(qry);
 
 System.out.println(count+"Statement Executed Successfully");

 st.close();
 con.close();
 }
}

Program to Delete:

import java.sql.SQLException;
import java.sql.Statement;
import java.sql.DriverManager;
import java.sql.Connection;

public class DeleteStaticOracle
{
 public static void main( String args[] ) throws SQLException
 {
  oracle.jdbc.OracleDriver obj = new oracle.jdbc.OracleDriver();
  
  Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE","System","pass");
 
  if(con != null)
   System.out.println("Connection established successfully");
  else
   System.out.println("Connection not established successfully");

  Statement st = con.createStatement();

  String qry  = "Delete from student where name = 'A' ";

  int count = st.executeUpdate(qry);

  System.out.println( count +" Record Deleted Successfully");     

  st.close();
  con.close();
 }
}

Previous
Next Post »