HSQLDB Tutorial
HSQLDB Useful Resources
Selected Reading
- HSQLDB - Indexes
- HSQLDB - Alter Command
- HSQLDB - Transactions
- HSQLDB - Regular Expressions
- HSQLDB - Null Values
- HSQLDB - Joins
- HSQLDB - Sorting Results
- HSQLDB - Like Clause
- HSQLDB - Delete Clause
- HSQLDB - Update Query
- HSQLDB - Where Clause
- HSQLDB - Select Query
- HSQLDB - Insert Query
- HSQLDB - Drop Table
- HSQLDB - Create Table
- HSQLDB - Data Types
- HSQLDB - Connect
- HSQLDB - Installation
- HSQLDB - Introduction
- HSQLDB - Home
HSQLDB Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
HSQLDB - Connect
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 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 successfullyAdvertisements