English 中文(简体)
HSQLDB - Connect
  • 时间:2024-12-27

HSQLDB - Connect


Previous Page Next Page  

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 apppcation and the database.

Example

import java.sql.Connection;
import java.sql.DriverManager;

pubpc class ConnectDatabase {
   pubpc 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.4hsqldb
hsqldb>java -classpath pb/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
Advertisements