HSQLDB Tutorial on HSQLDB Create Table

the basic mandatory requirements to create a table are table name, field names, and the data types to those fields. optionally, you can also provide the key constraints to the table.

syntax

take a look at the following syntax.

create table table_name (column_name column_type);

example

let us create a table named tutorials_tbl with the field-names such as id, title, author, and submission_date. take a look at the following query.

create table tutorials_tbl (
   id int not null,
   title varchar(50) not null,
   author varchar(20) not null,
   submission_date date,
   primary key (id) 
);

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

(0) rows effected

hsqldb – jdbc program

following is the jdbc program used to create a table named tutorials_tbl into the hsqldb database. save the program into createtable.java file.

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

public class createtable {
   
   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("create table tutorials_tbl (
            id int not null, title varchar(50) not null,
            author varchar(20) not null, submission_date date,
            primary key (id));
         ");
			
      }  catch (exception e) {
         e.printstacktrace(system.out);
      }
      system.out.println("table created 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 createtable.java
\>java createtable

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

table created successfully