How to connect to HSQLDB database using a JDBC program?

JDBCJava 8Object Oriented ProgrammingProgramming

HSQLDB is a relational database management system implemented in pure Java. You can easily embed this database to your application using JDBC. Or you can use the operations separately.

Installing HSQLDB:

  • Download the latest version of HSQLDB database.

  • Install HSQLDB following the steps given in HSQLDB Tutorial.

Make sure that the HSQLDB database is up and running. The URL to connect to this database is jdbc:hsqldb:hsql://host_name/database_name and the driver class name is org.hsqldb.jdbc.JDBCDriver. Download the driver and set classpath to it.

Example

Following JDBC program establishes a connection with HSQL database.

import java.sql.Connection;
import java.sql.DriverManager;
public class ConnectDatabase {
   public static void main(String[] args) {
      Connection con = null;
      try {
         //Registering the HSQLDB JDBC driver
         Class.forName("org.hsqldb.jdbc.JDBCDriver");
         //Creating the connection with HSQLDB
         con = DriverManager.getConnection("jdbc:hsqldb:hsql://localhost/testdb", "SA", "");
         if (con!= null) {
            System.out.println("Connection created successfully");
         }else{
            System.out.println("Problem with creating connection");
         }
      } catch (Exception e) {
         e.printStackTrace(System.out);
      }
   }
}

Output

Connection created successfully
raja
Published on 02-Apr-2019 13:10:46
Advertisements