Coding Lab - TechOnTechnology

Java program to delete a record from the database. The regno of the record has to be deleted is read from the keyboard.




Click Here to Know the Steps to Connect database in Linux Environment System


import java.io.*;
import java.sql.*;
class jdbc2
{
        public static void main(String args[])throws SQLException
        {
                int reg;
        String name,address;
                try
                {
                        DataInputStream in=new DataInputStream(System.in);
                        Class.forName("com.mysql.jdbc.Driver").newInstance();
                        Connection con=DriverManager.getConnection("jdbc:mysql://localhost/student","root","password");
                        Statement stm=con.createStatement();

                        String s1="select * from student;";
                        ResultSet rs=stm.executeQuery(s1);
            while(rs.next())
                        {
                               reg=rs.getInt("reg");
                                name=rs.getString("name");
                                address=rs.getString("address");
                                System.out.println(reg + "\t" + name + "\t" + address + "\t");
                        }

                        System.out.println("Enter the register number");
                        reg=Integer.parseInt(in.readLine());

                        String s="Delete from student where reg="+reg+";";
                        stm.executeUpdate(s);
                        System.out.println("Record is deleted");

                        String s2="select * from student;";
                        ResultSet rs1=stm.executeQuery(s2);
                        while(rs1.next())
                        {
                               reg=rs1.getInt("reg");
                                name=rs1.getString("name");
                                address=rs1.getString("address");
                                System.out.println(reg + "\t" + name + "\t" + address + "\t");
                        }


                stm.close();
                con.close();
                }
                catch(Exception e)
                {
e.printStackTrace();

}
}
Click Here to Know the Steps to Connect database in Linux Environment System


Name

Email *

Message *