HSQLDB Tutorial on HSQLDB Insert Query

you can achieve insert query statement in hsqldb by using the insert into command. you have to provide the user-defined data following the column field order from the table.

syntax

following is the generic syntax to insert a query.

insert into table_name (field1, field2,...fieldn)
values (value1, value2,...valuen );

to insert a string type data into a table, you will have to use double or single quotes to provide string value into the insert query statement.

example

let us consider an example that inserts a record into a table named tutorials_tbl with the values id = 100, title = learn php, author = john poul, and the submission date is current date.

following is the query for the given example.

insert into tutorials_tbl values (100,'learn php', 'john poul', now());

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

1 row effected

hsqldb – jdbc program

here is the jdbc program to insert the record into the table with the given values, id =100, title = learn php, author = john poul, and the submission date is current date. take a look at the given program. save the code into the inserquery.java file.

import java.sql.connection; 
import java.sql.drivermanager; 
import java.sql.statement;  

public class insertquery {
   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("insert into tutorials_tbl 
            values (100,'learn php', 'john poul', now())"); 
         con.commit(); 
      }catch (exception e) { 
         e.printstacktrace(system.out); 
      } 
      system.out.println(result+" rows effected"); 
      system.out.println("rows inserted successfully"); 
   } 
} 

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 insertquery.java 
\>java insertquery 

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

1 rows effected 
rows inserted successfully 

try to insert the following records into the tutorials_tbl table by using the insert into command.

id title author submission date
101 learn c yaswanth now()
102 learn mysql abdul s now()
103 learn excell bavya kanna now()
104 learn jdb ajith kumar now()
105 learn junit sathya murthi now()