HSQLDB - Delete Clause


Advertisements


Whenever you want to delete a record from any HSQLDB table, you can use the DELETE FROM command.

Syntax

Here is the generic syntax for DELETE command to delete data from a HSQLDB table.

DELETE FROM table_name [WHERE Clause]
  • If WHERE clause is not specified, then all the records will be deleted from the given MySQL table.

  • You can specify any condition using WHERE clause.

  • You can delete records in a single table at a time.

Example

Let us consider an example that deletes the record data from the table named tutorials_tbl having id 105. Following is the query that implements the given example.

DELETE FROM tutorials_tbl WHERE id = 105;

After execution of the above query, you will receive the following output −

(1) rows effected

HSQLDB – JDBC Program

Here is the JDBC program that implements the given example. Save the following program into DeleteQuery.java.

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

public class DeleteQuery {
   
   public static void main(String[] args) {
      Connection con = null;
      Statement stmt = null;
      int result = 0;
      
      try {
         Class.forName("org.hsqldb.jdbc.JDBCDriver");
         con = DriverManager.getConnection(
            "jdbc:hsqldb:hsql://localhost/testdb", "SA", "");
         stmt = con.createStatement();
         result = stmt.executeUpdate(
            "DELETE FROM tutorials_tbl   WHERE id=105");
      } catch (Exception e) {
      
         e.printStackTrace(System.out);
      }
      System.out.println(result+" Rows effected");
   }
}

You can start the database using the following command.

\>cd C:\hsqldb-2.3.4\hsqldb
hsqldb>java -classpath lib/hsqldb.jar org.hsqldb.server.Server --database.0
file:hsqldb/demodb --dbname.0 testdb

Compile and execute the above program using the following command.

\>javac DeleteQuery.java
\>java DeleteQuery

After execution of the above command, you will receive the following output −

1 Rows effected


Advertisements
E-Books Store