Sample program using HSQL database

Hi Everyone, In this blog post, I'm specifying a sample program using HSQL database. HSQL Database stands for Hyper Structured Query Language Database. HSQL DB stores data in memory and is written completely in java. HSQL DB is mainly used for testing purposes. One of the main advantage of HSQL DB is that we need not have to install anything.

To do program using HSQL DB, first we need to import the jar file and run the jar file as a server.


Select the server option and click ok. This means now HSQL DB acts as a server where data and tables are stored in memory.

The sample program to connect using HSQL DB is written below.

 package com.blog.database;  
 import java.sql.Connection;  
 import java.sql.DriverManager;  
 import java.sql.ResultSet;  
 import java.sql.Statement;  
 import java.util.Iterator;  
 import java.util.List;  
 public class HSQLDatabase {  
      public static void main(String[] args) {  
           try  
           {  
                String connectionURL ="jdbc:hsqldb:hsql://localhost";  
                Class.forName("org.hsqldb.jdbcDriver");  
                Connection con = DriverManager.getConnection(connectionURL,"sa","");  
                Statement stmt = con.createStatement();  
      //Creating table  
      stmt.executeQuery(" create table user_table (user_id integer not null, name varchar(50), personal_info_id integer, manager_id integer)");  
           stmt.executeQuery("insert into user_table values (501, 'Sujith',101,501)");  
           stmt.executeQuery("insert into user_table values (502, 'Reshmi',102,501)");  
           stmt.executeQuery("insert into user_table values (503, 'Vrinda Kumari',103,502)");  
           stmt.executeQuery("insert into user_table values (504, 'Meera Menon',104,501)");  
           stmt.executeQuery("insert into user_table values (505, 'Arathi Kutty',105,501)");  
           ResultSet rs = stmt.executeQuery(" select * from user_table");  
           while(rs.next())  
           {  
                System.out.println("Name = "+rs.getString("name"));  
           }  
           }  
           catch(Exception e )  
           {  
                e.printStackTrace();  
           }  
      }  
 }  

The program is all about creating a database, inserting some values into it and querying a particular attribute from those values.

The output of the program is shown below:

 Name = Sujith  
 Name = Reshmi  
 Name = Vrinda Kumari  
 Name = Meera Menon  
 Name = Arathi Kutty  

Please comment below if you have any doubts. All comments will be responded soon. Thank you for reading the post.

Comments

Popular posts from this blog

Difference between "diff" and "sdiff" commands in Unix

Anonymous classes in C++