JDBC Examples
- JDBC Create a Table Example
- JDBC Insert Multiple Rows Example
- JDBC Update Query Example
- JDBC Select Query Example
- JDBC Delete Query Example
- JDBC Transaction Management Example
- JDBC Connection to Oracle Database Example
- JDBC Connection to PostgreSQL Example
- JDBC Connection to H2 Database Example
- Java JDBC Connection to HSQLDB Database
- Java PostgreSQL Example
- Java H2 Create Table Example
- Java H2 Insert Record Example
- Java H2 Embedded Database Example
A JDBC example to show you how to connect to an HSQLDB database with a JDBC driver.
HyperSQL Database (HSQLDB) is a modern relational database manager that conforms closely to the SQL:2011 standard and JDBC 4 specifications. It supports all core features and RDBMS. HSQLDB is used for development, testing, and deployment of database applications.
Learn JDBC at https://www.javaguides.net/p/jdbc-tutorial.html.
HyperSQL Database (HSQLDB) is a modern relational database manager that conforms closely to the SQL:2011 standard and JDBC 4 specifications. It supports all core features and RDBMS. HSQLDB is used for development, testing, and deployment of database applications.
Learn JDBC at https://www.javaguides.net/p/jdbc-tutorial.html.
HSQLDB - Installation
Refer below link to install HSQLDB:
HSQLDB Setup and Configuration
- Download HSQLDB dependency or jar file from the official website https://repo1.maven.org/maven2/org/hsqldb/hsqldb/2.4.0/hsqldb-2.4.0.jar.
- Add the HSQLDB Jar file to your project classpath.
- By default the Java application to connect to an HSQLDB with the username SA and an empty password. Example:
private static String jdbcURL = "jdbc:hsqldb:hsql://localhost/testdb";
private static String jdbcUsername = "SA";
private static String jdbcPassword = "";
JDBC Util Class
Let's create a JDBCUtils.java file with all JDBC common methods like:
package net.javaguides.jdbc.hsqldb.crud;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class JDBCUtils {
private static String jdbcURL = "jdbc:hsqldb:hsql://localhost/testdb";
private static String jdbcUsername = "SA";
private static String jdbcPassword = "";
public static Connection getConnection() {
Connection connection = null;
try {
connection = DriverManager.getConnection(jdbcURL, jdbcUsername, jdbcPassword);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return connection;
}
public static void printSQLException(SQLException ex) {
for (Throwable e: ex) {
if (e instanceof SQLException) {
e.printStackTrace(System.err);
System.err.println("SQLState: " + ((SQLException) e).getSQLState());
System.err.println("Error Code: " + ((SQLException) e).getErrorCode());
System.err.println("Message: " + e.getMessage());
Throwable t = ex.getCause();
while (t != null) {
System.out.println("Cause: " + t);
t = t.getCause();
}
}
}
}
}
JDBC Connection with HSQLDB and Create a Table in the HSQLDB
This JDBC program creates a users table into the HSQLDB.
package net.javaguides.jdbc.hsqldb.crud;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
/**
* Create Statement JDBC Example
* @author Ramesh Fadatare
*
*/
public class HSQLDBCreateExample {
private static final String createTableSQL = "create table users (\r\n" + " id int(3) primary key,\r\n" +
" name varchar(20),\r\n" + " email varchar(20),\r\n" + " country varchar(20),\r\n" +
" password varchar(20)\r\n" + " );";
public static void main(String[] argv) throws SQLException {
HSQLDBCreateExample createTableExample = new HSQLDBCreateExample();
createTableExample.createTable();
}
public void createTable() throws SQLException {
System.out.println(createTableSQL);
// Step 1: Establishing a Connection
try (Connection connection = JDBCUtils.getConnection();
// Step 2:Create a statement using connection object
Statement statement = connection.createStatement();) {
// Step 3: Execute the query or update query
statement.execute(createTableSQL);
} catch (SQLException e) {
// print SQL exception information
JDBCUtils.printSQLException(e);
}
}
}
References
https://www.javaguides.net/2019/08/java-hsql-tutorial-create-read-update-delete-jdbc-examples.html.
Related JDBC Source Code Examples
- JDBC Create a Table Example
- JDBC Insert Multiple Rows Example
- JDBC Update Query Example
- JDBC Select Query Example
- JDBC Delete Query Example
- JDBC Transaction Management Example
- JDBC Connection to Oracle Database Example
- JDBC Connection to PostgreSQL Example
- JDBC Connection to H2 Database Example
- Java JDBC Connection to HSQLDB Database
- Java PostgreSQL Example
- Java H2 Create Table Example
- Java H2 Insert Record Example
- Java H2 Embedded Database Example
Comments
Post a Comment