Sunday, 21 October 2012

deleting a row/data from a table in JAVA




import java.sql.*;
import java.util.*;
class delete
{
    public static void main(String[] argv)
    {
        try
        {
            Scanner sc=new Scanner(System.in);
            String roll;
         
         
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            String database="jdbc:odbc:eceDSN";
            Connection con=DriverManager.getConnection(database,"","");
            System.out.println("DATABASE OF ECE is Connected");
            System.out.println("Enter The Roll No");

            roll=sc.nextLine();
            PreparedStatement ps=con.prepareStatement("delete from ECE where roll=?");
            ps.setString(1,roll);
         
            //create a statement object
            //Statement st=con.createStatement();
            //Execute the statement
            int result=ps.executeUpdate();
            if(result==1)
            {
                System.out.println("Record is Deleted Successfully");
            }
            con.close();
        }
        catch(Exception err)
        {
            System.out.println(err.getMessage());
        }
    //loading all the driver to the memory

    }
}

No comments:

Post a Comment

Tech N Science © 2013