All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.duckdb.DuckDBDatabase Maven / Gradle / Ivy

There is a newer version: 1.1.3
Show newest version
package org.duckdb;

import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.sql.SQLException;

public class DuckDBDatabase {

	protected String url;
	protected boolean read_only;

	public DuckDBDatabase(String url, boolean read_only) throws SQLException {
		if (!url.startsWith("jdbc:duckdb")) {
			throw new SQLException("DuckDB JDBC URL needs to start with 'jdbc:duckdb:'");
		}
		this.url = url;
		String db_dir = url.replaceFirst("^jdbc:duckdb:", "").trim();
		if (db_dir.length() == 0) {
			db_dir = ":memory:";
		}
		this.read_only = read_only;
		db_ref = DuckDBNative.duckdb_jdbc_startup(db_dir.getBytes(StandardCharsets.UTF_8), read_only);
	}

	public void shutdown() {
		try {
			finalize();
		} catch (Throwable e) {
		}
	}

	protected synchronized void finalize() throws Throwable {
		if (db_ref != null) {
			DuckDBNative.duckdb_jdbc_shutdown(db_ref);
			db_ref = null;
		}
	}

	protected ByteBuffer db_ref;

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy