JayDeBeApi

1.2.3 · active · verified Sun Apr 05

The JayDeBeApi module allows Python (cPython) and Jython code to connect to databases using Java JDBC drivers, providing a Python DB-API v2.0 compliant interface. It aims to offer a unified and fast interface through a flexible plug-in mechanism. The current version is 1.2.3, released in June 2020, indicating an infrequent release cadence.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to establish a connection to a database using `jaydebeapi`. You need to provide the path to your JDBC driver JAR file, the fully qualified Java class name of the driver, the JDBC connection URL, and your database credentials. Remember to set the `JDBC_DRIVER_PATH`, `JDBC_DRIVER_CLASS`, `JDBC_URL`, `DB_USER`, and `DB_PASSWORD` environment variables or replace the placeholders.

import jaydebeapi
import os

# NOTE: Replace with your actual JDBC driver path, class name, and connection details
# For example, using a placeholder for a generic JDBC driver (e.g., PostgreSQL)
JDBC_DRIVER_PATH = os.environ.get('JDBC_DRIVER_PATH', '/path/to/your/jdbc_driver.jar')
JDBC_DRIVER_CLASS = os.environ.get('JDBC_DRIVER_CLASS', 'org.postgresql.Driver')
JDBC_URL = os.environ.get('JDBC_URL', 'jdbc:postgresql://localhost:5432/mydatabase')
DB_USER = os.environ.get('DB_USER', 'user')
DB_PASSWORD = os.environ.get('DB_PASSWORD', 'password')

try:
    conn = jaydebeapi.connect(
        JDBC_DRIVER_CLASS,
        JDBC_URL,
        [DB_USER, DB_PASSWORD],
        JDBC_DRIVER_PATH
    )
    cursor = conn.cursor()
    cursor.execute("SELECT 'Hello, JayDeBeApi!' AS message")
    result = cursor.fetchone()
    print(f"Connection successful: {result[0]}")
    cursor.close()
    conn.close()
except Exception as e:
    print(f"Error connecting to database: {e}")
    print("Please ensure your JDBC driver path, class name, URL, and credentials are correct, and that a compatible JRE is installed and JAVA_HOME is set if necessary.")

view raw JSON →