HSQLDB Tutorial on HSQLDB Connect

in the installation chapter, we discussed how to connect the database manually. in this chapter, we will discuss how to connect the database programmatically (using java programming).

take a look at the following program, which will start the server and create a connection between the java application and the database.

example

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);
      }
   }
}

save this code into connectdatabase.java file. you will have to 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

you can use the following command to compile and execute the code.

\>javac connectdatabase.java
\>java connectdatabase

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

connection created successfully