{"library":"sqlalchemy-jdbcapi","title":"SQLAlchemy JDBCAPI","type":"library","description":"SQLAlchemy-JDBCAPI is a modern SQLAlchemy dialect that provides native DB-API 2.0 implementation for JDBC connections using JPype, and ODBC connections via pyodbc. It supports a wide range of databases including PostgreSQL, MySQL, SQL Server, Oracle, and many others, with features like automatic JDBC driver management. The current version is 2.2.1, and it maintains an active release cadence with frequent updates and new features such as full asyncio support.","language":"python","status":"active","last_verified":"Thu Apr 16","install":{"commands":["pip install sqlalchemy-jdbcapi","pip install sqlalchemy-jdbcapi[pyodbc]"],"cli":null},"imports":["from sqlalchemy_jdbcapi.async_dbapi import AsyncConnection","from sqlalchemy_jdbcapi.async_dbapi import AsyncCursor"],"auth":{"required":false,"env_vars":[]},"links":{"homepage":null,"github":"https://github.com/daneshpatel/sqlalchemy-jdbcapi","docs":"https://sqlalchemy-jdbcapi.readthedocs.io","changelog":"https://github.com/daneshpatel/sqlalchemy-jdbcapi/blob/main/CHANGELOG.md","pypi":"https://pypi.org/project/sqlalchemy-jdbcapi/","npm":null,"openapi_spec":null,"status_page":null,"smithery":null},"quickstart":{"code":"import os\nfrom sqlalchemy import create_engine, text\n\n# Ensure JAVA_HOME is set for JPype to find a JVM\n# Example: os.environ['JAVA_HOME'] = '/path/to/your/jdk'\n\n# Example for PostgreSQL via JDBC\n# Replace with your actual database details or environment variables\nDB_USER = os.environ.get('JDBC_DB_USER', 'your_user')\nDB_PASS = os.environ.get('JDBC_DB_PASS', 'your_password')\nDB_HOST = os.environ.get('JDBC_DB_HOST', 'localhost')\nDB_PORT = os.environ.get('JDBC_DB_PORT', '5432')\nDB_NAME = os.environ.get('JDBC_DB_NAME', 'your_database')\n\n# The dialect string uses 'jdbcapi+' followed by the driver name\n# For PostgreSQL, it's 'postgresql'. For MySQL, 'mysql', etc.\njdbc_url = f\"jdbcapi+postgresql://{DB_USER}:{DB_PASS}@{DB_HOST}:{DB_PORT}/{DB_NAME}\"\n\ntry:\n    engine = create_engine(jdbc_url)\n\n    with engine.connect() as connection:\n        result = connection.execute(text(\"SELECT 1\"))\n        print(f\"Connection successful, result: {result.scalar()}\")\n\n    # Example with asyncio (requires sqlalchemy-jdbcapi>=2.2.1 and asyncpg, aiomysql, etc.)\n    # from sqlalchemy.ext.asyncio import create_async_engine\n    # async_jdbc_url = f\"jdbcapi+postgresql+asyncpg://{DB_USER}:{DB_PASS}@{DB_HOST}:{DB_PORT}/{DB_NAME}\"\n    # async_engine = create_async_engine(async_jdbc_url)\n    # async def run_async_query():\n    #     async with async_engine.connect() as conn:\n    #         result = await conn.execute(text(\"SELECT 2\"))\n    #         print(f\"Async connection successful, result: {result.scalar()}\")\n    # import asyncio\n    # asyncio.run(run_async_query())\n\nexcept Exception as e:\n    print(f\"Error connecting to database: {e}\")\n\nfinally:\n    # JPype requires explicit JVM shutdown in some contexts or when done\n    try:\n        from jpype import isJVMStarted, shutdownJVM\n        if isJVMStarted():\n            shutdownJVM()\n            print(\"JVM shut down.\")\n    except ImportError:\n        pass # JPype might not be installed or available\n","lang":"python","description":"This quickstart demonstrates how to establish a connection to a PostgreSQL database using `sqlalchemy-jdbcapi` via a JDBC driver. It uses `create_engine` with the `jdbcapi+postgresql` dialect. Ensure you have a Java Runtime Environment (JRE) or Java Development Kit (JDK) installed and accessible to JPype, typically by setting the `JAVA_HOME` environment variable. The example includes a basic synchronous query and comments on how to extend for asyncio support.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}