JDBC – Delete All Records

Delete all records from table: (static query)

executeUpdate() method:

  • The executeUpdate() method returns the number of rows affected by the SQL statement (an INSERT typically affects one row, but an UPDATE or DELETE statement can affect more).
package online;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class DeleteRecords
{
            public static void main(String args[]) throws Exception
            {
                        Connection conn = null;
                        String driver = “oracle.jdbc.driver.OracleDriver”;
                        try
                        {
                                    Class.forName(driver);
                                    System.out.println(“Driver loaded”);
                                   
                                    String url = “jdbc:oracle:thin:@localhost:1521:xe”;
                                    String uname = “system”;
                                    String pwd = “admin”;
                                   
                                    conn = DriverManager.getConnection(url, uname, pwd);
                                    System.out.println(“Connection is ready”);     
                                   
                                    Statement stmt = conn.createStatement();
                                    String query = “delete from account”;
                                    int count = stmt.executeUpdate(query);
                                    System.out.println(count + ” records deleted”);
                        }
                        finally
                        {
                                    if(conn != null)
                                    {
                                                conn.close();
                                                System.out.println(“Connection closed”);
                                    }
                        }
            }
}

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top