HERDDB JDBC driver guidelines
HERDDB JDBC driver guidelines
HERDDB is a distributed NOSQL database that provides JDBC drivers as a connection interface that interacts with the application.This article will introduce you how to use Herddb JDBC driver to integrate with Java applications and provide some code examples.
Step 1: Download and install Herddb JDBC driver
To use Herddb JDBC driver, you need to download and install it first.You can get the jar file of the driver from the Herddb official website (https://www.herddb.org) or Maven repository.Add jar files to the road of your project.
Step 2: Create a database connection
Use JDBC to connect to Herddb database, you need to provide URL, username and password.Here are a sample code connected to the local Herddb database:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class HerdDBConnectionExample {
public static void main(String[] args) {
Connection connection = null;
try {
// Register the HERDDB driver class
Class.forName("io.herd.jdbc.HerdDriver");
// Create a connection
String url = "jdbc:herddb:local?serverDirectory=/path/to/server";
String username = "your_username";
String password = "your_password";
connection = DriverManager.getConnection(url, username, password);
// The connection is successful, you can perform the database operation
// ...
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
// Turn off the connection
try {
if (connection != null && !connection.isClosed()) {
connection.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
Please make sure the `/path/to/server` is replaced with the actual path of your Herddb server, and at the same time, the` your_username` and `YOUR_PASSWORD` are replaced with appropriate credentials.
Step 3: Execute the database operation
After connecting to the Herddb database, you can perform various database operations, such as creating tables, inserting data, query data, etc.Here are some example code:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class HerdDBExample {
public static void main(String[] args) {
Connection connection = null;
PreparedStatement preparedStatement = null;
try {
// Connect the database (code slightly)
// Execute the insert operation
String insertQuery = "INSERT INTO my_table (id, name) VALUES (?, ?)";
preparedStatement = connection.prepareStatement(insertQuery);
preparedStatement.setInt(1, 1);
preparedStatement.setString(2, "John Doe");
int rowsAffected = preparedStatement.executeUpdate();
// Execute the query operation
String selectQuery = "SELECT * FROM my_table";
preparedStatement = connection.prepareStatement(selectQuery);
ResultSet resultSet = preparedStatement.executeQuery();
while (resultSet.next()) {
int id = resultSet.getInt("id");
String name = resultSet.getString("name");
System.out.println("ID: " + id + ", Name: " + name);
}
// Other operations ...
} catch (SQLException e) {
e.printStackTrace();
} finally {
// Turn off the connection and pre -compilation statement
// ...
}
}
}
The above examples demonstrate how to perform insertion and query operations.Please modify and expand according to your actual needs.
By following the above steps, you can successfully use the HerddB JDBC driver to integrate with Java applications and perform various database operations.