Write a JDBC program to delete the records of employees whose names are starting with ‘A’ character.

import java.sql.*;
public class DeleteEmployeeRecord
{           static Connection cn;
            static Statement st;
            public static void main(String args[])
            {     try {
                                    ResultSet rs,rs1;
                                    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                                    cn=DriverManager.getConnection("jdbc:odbc:emp1");
                                    st=cn.createStatement();
                                    System.out.println("\nBefore deleting records are :");
                                    rs=st.executeQuery("select * from employees");
                                    System.out.println("\nEno \t  Ename\t Sal \n");
                                    while(rs.next())
                                    {
                                    System.out.println(rs.getInt("eno")+"\t"+rs.getString("ename")+"\t"+rs.getInt("sal"));
                                    }
                                    st.executeUpdate("delete from employees where ename like 'A%'");
                                    System.out.println("\nAfter deleting records are :");
                                    rs1=st.executeQuery("select * from employees");
                                    System.out.println("\nEno \t  Ename\t Sal \n");
                                    while(rs1.next())
                                  {
                                    System.out.println(rs1.getInt("eno")+"\t"+rs1.getString("ename")+"\t"+rs1.getInt("sal"));
                                    }
                                    cn.close();
                        }catch(Exception e)
                             {      
                                     System.out.println(e);        
                             }
            }
}

Comments

Popular posts from this blog

Write a java program to create Teacher table(TNo.TName, Sal, Desg) and insert a record in it.

Create a JSP page to accept a number from an user and display it in words: Example: 123 – One Two Three. The output should be in red color.

Write a JSP program to accept the details of Account (ANo, Type, Bal) and store it into database and display it in tabular form. (Use PreparedStatement interface)