JDBC Module¶
The JDBC module provides a native DB-API 2.0 compliant implementation for JDBC connectivity using JPype.
Connection¶
- class sqlalchemy_jdbcapi.jdbc.connection.Connection(jclassname, url, driver_args=None, jars=None, libs=None)[source]¶
Bases:
objectDB-API 2.0 compliant connection to a JDBC database.
This class wraps a JDBC Connection object from JPype.
- __init__(jclassname, url, driver_args=None, jars=None, libs=None)[source]¶
Create a JDBC connection.
- Parameters:
- Raises:
InterfaceError – If JVM or driver cannot be loaded
DatabaseError – If connection fails
- cursor()[source]¶
Create a new cursor.
- Returns:
Cursor object
- Raises:
InterfaceError – If connection is closed
- Return type:
Cursor¶
- class sqlalchemy_jdbcapi.jdbc.cursor.Cursor(connection, jdbc_connection)[source]¶
Bases:
objectDB-API 2.0 compliant cursor for JDBC connections.
This class wraps a JDBC Statement/PreparedStatement and ResultSet.
- property description: tuple[tuple[str, Any, None, None, None, None, None], ...] | None¶
Get column descriptions from last query.
Returns 7-item tuples: (name, type_code, display_size, internal_size, precision, scale, null_ok). We only provide name and type_code.
- execute(operation, parameters=None)[source]¶
Execute a database operation.
- Parameters:
- Returns:
Self for chaining
- Raises:
ProgrammingError – If cursor is closed or operation fails
- Return type:
- executemany(operation, seq_of_parameters)[source]¶
Execute a database operation multiple times.
- Parameters:
- Raises:
ProgrammingError – If operation fails
- fetchone()[source]¶
Fetch next row from query result.
- Returns:
Tuple of column values or None if no more rows
- Raises:
InterfaceError – If no query has been executed
- Return type:
- fetchmany(size=None)[source]¶
Fetch multiple rows from query result.
- Parameters:
size (int | None) – Number of rows to fetch (default: arraysize)
- Returns:
List of row tuples
- Raises:
InterfaceError – If no query has been executed
- Return type:
- fetchall()[source]¶
Fetch all remaining rows from query result.
- Returns:
List of row tuples
- Raises:
InterfaceError – If no query has been executed
- Return type:
- to_arrow()¶
- to_dict()¶
- to_pandas()¶
- to_polars()¶
Driver Manager¶
JDBC driver auto-download and management.
This module handles automatic downloading of JDBC drivers from Maven Central and provides fallback to manual driver configuration via CLASSPATH.
- class sqlalchemy_jdbcapi.jdbc.driver_manager.JDBCDriver(group_id, artifact_id, version, classifier=None)[source]¶
Bases:
NamedTupleJDBC driver metadata for automatic download.
- sqlalchemy_jdbcapi.jdbc.driver_manager.get_driver_cache_dir()[source]¶
Get the driver cache directory.
- Returns:
Path to the driver cache directory.
- Return type:
- sqlalchemy_jdbcapi.jdbc.driver_manager.download_driver(driver, cache_dir=None, force=False)[source]¶
Download a JDBC driver from Maven Central.
- Parameters:
driver (JDBCDriver) – JDBC driver metadata.
cache_dir (Path | None) – Directory to cache downloaded drivers. If None, uses default.
force (bool) – Force re-download even if driver exists.
- Returns:
Path to the downloaded driver JAR file.
- Raises:
RuntimeError – If download fails.
- Return type:
- sqlalchemy_jdbcapi.jdbc.driver_manager.get_driver_path(database, driver=None, auto_download=True, cache_dir=None)[source]¶
Get the path to a JDBC driver, downloading if necessary.
- Parameters:
database (str) – Database name (e.g., ‘postgresql’, ‘mysql’).
driver (JDBCDriver | None) – Custom driver metadata. If None, uses recommended driver.
auto_download (bool) – Whether to auto-download driver if not found.
cache_dir (Path | None) – Directory to cache downloaded drivers.
- Returns:
Path to the JDBC driver JAR file.
- Raises:
RuntimeError – If driver not found and auto-download disabled.
- Return type:
- sqlalchemy_jdbcapi.jdbc.driver_manager.get_all_driver_paths(databases=None, auto_download=True, cache_dir=None)[source]¶
Get paths to multiple JDBC drivers.
- Parameters:
- Returns:
List of paths to JDBC driver JAR files.
- Return type:
- sqlalchemy_jdbcapi.jdbc.driver_manager.get_classpath_with_drivers(databases=None, auto_download=True, manual_classpath=None)[source]¶
Get comprehensive classpath including auto-downloaded and manual drivers.
- Parameters:
- Returns:
List of all classpath entries.
- Return type:
- sqlalchemy_jdbcapi.jdbc.driver_manager.verify_driver(driver_path)[source]¶
Verify that a JDBC driver JAR file is valid.
JVM Management¶
JVM management and initialization.
- sqlalchemy_jdbcapi.jdbc.jvm.get_classpath(auto_download=True, databases=None)[source]¶
Get JDBC driver classpath from environment, auto-download, or both.
- Parameters:
- Returns:
List of paths to JDBC driver JAR files.
- Return type:
- sqlalchemy_jdbcapi.jdbc.jvm.start_jvm(classpath=None, jvm_path=None, jvm_args=None, auto_download=True, databases=None)[source]¶
Start the JVM with specified classpath and arguments.
- Parameters:
classpath (list[Path] | None) – List of paths to add to classpath. If None, uses environment + auto-download.
jvm_path (Path | None) – Path to JVM library. If None, JPype will auto-detect.
jvm_args (list[str] | None) – Additional JVM arguments (e.g., [‘-Xmx512m’]).
auto_download (bool) – Whether to auto-download JDBC drivers. Default: True.
databases (list[str] | None) – List of database names for auto-download. If None, downloads common drivers.
- Raises:
JVMNotStartedError – If JVM fails to start.
- sqlalchemy_jdbcapi.jdbc.jvm.shutdown_jvm()[source]¶
Shutdown the JVM (optional, usually not needed).
- sqlalchemy_jdbcapi.jdbc.jvm.get_java_class(class_name)[source]¶
Get a Java class by name.
- Parameters:
class_name (str) – Fully qualified Java class name.
- Returns:
Java class object.
- Raises:
JVMNotStartedError – If JVM is not started.
- Return type:
Type Converter¶
Type conversion between JDBC and Python types.
- class sqlalchemy_jdbcapi.jdbc.type_converter.TypeConverter[source]¶
Bases:
objectHandles conversion between JDBC SQL types and Python types.
Java SQL Types (from java.sql.Types): -7 BIT -6 TINYINT -5 BIGINT -4 LONGVARBINARY -3 VARBINARY -2 BINARY -1 LONGVARCHAR 1 CHAR 2 NUMERIC 3 DECIMAL 4 INTEGER 5 SMALLINT 6 FLOAT 7 REAL 8 DOUBLE 12 VARCHAR 91 DATE 92 TIME 93 TIMESTAMP 2004 BLOB 2005 CLOB 2011 NCLOB
- JDBC_TYPES = {-7: 'BIT', -6: 'TINYINT', -5: 'BIGINT', -4: 'LONGVARBINARY', -3: 'VARBINARY', -2: 'BINARY', -1: 'LONGVARCHAR', 1: 'CHAR', 2: 'NUMERIC', 3: 'DECIMAL', 4: 'INTEGER', 5: 'SMALLINT', 6: 'FLOAT', 7: 'REAL', 8: 'DOUBLE', 12: 'VARCHAR', 91: 'DATE', 92: 'TIME', 93: 'TIMESTAMP', 2004: 'BLOB', 2005: 'CLOB', 2011: 'NCLOB'}¶
DataFrame Integration¶
DataFrame integration for pandas, polars, and Apache Arrow.
This module provides utilities to convert JDBC query results directly into DataFrames for data science and ML workflows.
- sqlalchemy_jdbcapi.jdbc.dataframe.cursor_to_pandas(cursor)[source]¶
Convert cursor results to pandas DataFrame.
- Parameters:
cursor (Cursor) – Cursor with executed query
- Returns:
pandas.DataFrame
- Raises:
ImportError – If pandas is not installed
ValueError – If cursor has no results
- Return type:
Any
Example
>>> cursor.execute("SELECT * FROM users") >>> df = cursor_to_pandas(cursor) >>> print(df.head())
- sqlalchemy_jdbcapi.jdbc.dataframe.cursor_to_polars(cursor)[source]¶
Convert cursor results to polars DataFrame.
- Parameters:
cursor (Cursor) – Cursor with executed query
- Returns:
polars.DataFrame
- Raises:
ImportError – If polars is not installed
ValueError – If cursor has no results
- Return type:
Any
Example
>>> cursor.execute("SELECT * FROM users") >>> df = cursor_to_polars(cursor) >>> print(df.head())
- sqlalchemy_jdbcapi.jdbc.dataframe.cursor_to_arrow(cursor)[source]¶
Convert cursor results to Apache Arrow Table.
- Parameters:
cursor (Cursor) – Cursor with executed query
- Returns:
pyarrow.Table
- Raises:
ImportError – If pyarrow is not installed
ValueError – If cursor has no results
- Return type:
Any
Example
>>> cursor.execute("SELECT * FROM users") >>> table = cursor_to_arrow(cursor) >>> print(table.schema)
- sqlalchemy_jdbcapi.jdbc.dataframe.cursor_to_dict(cursor)[source]¶
Convert cursor results to list of dictionaries.
- Parameters:
cursor (Cursor) – Cursor with executed query
- Returns:
List of row dictionaries
- Return type:
Example
>>> cursor.execute("SELECT * FROM users") >>> rows = cursor_to_dict(cursor) >>> print(rows[0]) {'id': 1, 'name': 'Alice', 'email': 'alice@example.com'}
Exceptions¶
JDBC Exception hierarchy following DB-API 2.0 specification.
- exception sqlalchemy_jdbcapi.jdbc.exceptions.Error[source]¶
Bases:
ExceptionBase class for all JDBC-related errors.
- exception sqlalchemy_jdbcapi.jdbc.exceptions.Warning[source]¶
Bases:
ExceptionException raised for important warnings.
- exception sqlalchemy_jdbcapi.jdbc.exceptions.InterfaceError[source]¶
Bases:
ErrorException raised for errors related to the database interface.
- exception sqlalchemy_jdbcapi.jdbc.exceptions.DatabaseError[source]¶
Bases:
ErrorException raised for errors related to the database.
- exception sqlalchemy_jdbcapi.jdbc.exceptions.InternalError[source]¶
Bases:
DatabaseErrorException raised when the database encounters an internal error.
- exception sqlalchemy_jdbcapi.jdbc.exceptions.OperationalError[source]¶
Bases:
DatabaseErrorException raised for operational database errors.
- exception sqlalchemy_jdbcapi.jdbc.exceptions.ProgrammingError[source]¶
Bases:
DatabaseErrorException raised for programming errors.
- exception sqlalchemy_jdbcapi.jdbc.exceptions.IntegrityError[source]¶
Bases:
DatabaseErrorException raised when database integrity is violated.
- exception sqlalchemy_jdbcapi.jdbc.exceptions.DataError[source]¶
Bases:
DatabaseErrorException raised for errors related to processed data.
- exception sqlalchemy_jdbcapi.jdbc.exceptions.NotSupportedError[source]¶
Bases:
DatabaseErrorException raised when a method or database API is not supported.
- exception sqlalchemy_jdbcapi.jdbc.exceptions.JDBCDriverNotFoundError[source]¶
Bases:
InterfaceErrorException raised when JDBC driver cannot be found.
- exception sqlalchemy_jdbcapi.jdbc.exceptions.JVMNotStartedError[source]¶
Bases:
InterfaceErrorException raised when JVM is not started.
DB-API Type Objects¶
DB-API 2.0 type objects and constructors.
- class sqlalchemy_jdbcapi.jdbc.types.DBAPITypeObject(*values)[source]¶
Bases:
objectBase class for DB-API type objects.
- sqlalchemy_jdbcapi.jdbc.types.Timestamp(year, month, day, hour, minute, second)[source]¶
Construct a timestamp object.
- sqlalchemy_jdbcapi.jdbc.types.DateFromTicks(ticks)[source]¶
Construct a date object from ticks since epoch.
- sqlalchemy_jdbcapi.jdbc.types.TimeFromTicks(ticks)[source]¶
Construct a time object from ticks since epoch.