Implement data duration in the Java class library through DuckDB JDBC Driver
Use DuckDB JDBC to drive the data durable in the Java class library
introduce
Implementing data persistence in the Java library can help developers store data in the database and easily manage them.In this article, we will introduce how to use the DuckDB JDBC driver to implement the data persistence of the Java library.We will introduce the installation process of DuckDB JDBC drive in detail and how to connect to the database and perform basic CRUD (creation, reading, update, deletion) operation.
DuckDB Introduction
DuckDB is an analytical column database with memory optimization.It aims to provide fast query performance and low latency, which is very suitable for use in Java applications.DuckDB supports the JDBC protocol, which means that we can use the DuckDB JDBC driver to connect to the database and execute the SQL query.
Install DuckDB JDBC driver
To start using DuckDB JDBC driver, we need to add it to the dependency of the Java library.You can download the latest version of the JDBC driver from DuckDB's official website (https://www.duckdb.org/).Then add the jar file to the class path of your Java project.
Connect to DuckDB database
To connect to the DuckDB database, we need to use JDBC to connect string, user name and password.Please make sure you have installed DuckDB databases on the system and have available connection credentials.The following is an example code that shows how to connect to the DuckDB database:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class DuckDBConnection {
private static final String JDBC_URL = "jdbc:duckdb:<database_path>";
private static final String USERNAME = "<username>";
private static final String PASSWORD = "<password>";
public static Connection getConnection() throws SQLException {
return DriverManager.getConnection(JDBC_URL, USERNAME, PASSWORD);
}
}
In the above code, you need to replace the path of `database_path>` to the DuckDB database file, and the `USAMERNAME> and` <Password> `to your connection credentials.
Perform the basic CRUD operation
Once we establish a connection with the DuckDB database, we can perform various CRUD operations.Here are some example code to show how to perform basic CRUD operations:
1. Create table:
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
public class CreateTableExample {
public static void main(String[] args) {
try (Connection conn = DuckDBConnection.getConnection();
Statement stmt = conn.createStatement()) {
String createTableQuery = "CREATE TABLE users (id INT PRIMARY KEY, name VARCHAR(50), age INT)";
stmt.execute(createTableQuery);
System.out.println("Table created successfully!");
} catch (SQLException e) {
e.printStackTrace();
}
}
}
2. Insert data:
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class InsertDataExample {
public static void main(String[] args) {
try (Connection conn = DuckDBConnection.getConnection();
PreparedStatement pstmt = conn.prepareStatement("INSERT INTO users (id, name, age) VALUES (?, ?, ?)")) {
// Setting parameters
pstmt.setInt(1, 1);
pstmt.setString(2, "John");
pstmt.setInt(3, 25);
pstmt.executeUpdate();
System.out.println("Data inserted successfully!");
} catch (SQLException e) {
e.printStackTrace();
}
}
}
3. Query data:
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class FetchDataExample {
public static void main(String[] args) {
try (Connection conn = DuckDBConnection.getConnection();
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM users")) {
while (rs.next()) {
int id = rs.getInt("id");
String name = rs.getString("name");
int age = rs.getInt("age");
System.out.println("ID: " + id + ", Name: " + name + ", Age: " + age);
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
4. Update data:
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class UpdateDataExample {
public static void main(String[] args) {
try (Connection conn = DuckDBConnection.getConnection();
PreparedStatement pstmt = conn.prepareStatement("UPDATE users SET age = ? WHERE id = ?")) {
// Setting parameters
pstmt.setInt(1, 30);
pstmt.setInt(2, 1);
pstmt.executeUpdate();
System.out.println("Data updated successfully!");
} catch (SQLException e) {
e.printStackTrace();
}
}
}
5. Delete data:
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class DeleteDataExample {
public static void main(String[] args) {
try (Connection conn = DuckDBConnection.getConnection();
PreparedStatement pstmt = conn.prepareStatement("DELETE FROM users WHERE id = ?")) {
// Setting parameters
pstmt.setInt(1, 1);
pstmt.executeUpdate();
System.out.println("Data deleted successfully!");
} catch (SQLException e) {
e.printStackTrace();
}
}
}
Summarize
Through the DuckDB JDBC driver, we can easily implement the data persistence function in the Java class library.This article introduces the installation process of DuckDB, the establishment of database connections, and how to perform basic CRUD operations.It is hoped that this article can provide developers with an entry guide to help them manage and store data effectively in the Java class library.