OceanBase logo

OceanBase

A unified distributed database ready for your transactional, analytical, and AI workloads.

Product Overview
DEPLOY YOUR WAY

OceanBase Cloud

The best way to deploy and scale OceanBase

OceanBase Enterprise

Run and manage OceanBase on your infra

TRY OPEN SOURCE

OceanBase Community Edition

The free, open-source distributed database

OceanBase seekdb

Open source AI native search database

Customer Stories

Real-world success stories from enterprises across diverse industries.

View All
BY USE CASES

Mission-Critical Transactions

Global & Multicloud Application

Elastic Scaling for Peak Traffic

Real-time Analytics

Active Geo-redundancy

Database Consolidation

Resources

Comprehensive knowledge hub for OceanBase.

Blog

Live Demos

Training & Certification

Documentation

Official technical guides, tutorials, API references, and manuals for all OceanBase products.

View All
PRODUCTS

OceanBase Cloud

OceanBase Database

Tools

Connectors and Middleware

QUICK START

OceanBase Cloud

OceanBase Database

BEST PRACTICES

Practical guides for utilizing OceanBase more effectively and conveniently

Company

Learn more about OceanBase – our company, partnerships, and trust and security initiatives.

About OceanBase

Partner

Trust Center

Contact Us

International - English
中国站 - 简体中文
日本 - 日本語
Sign In
Start on Cloud

OceanBase

A unified distributed database ready for your transactional, analytical, and AI workloads.

Product Overview
DEPLOY YOUR WAY

OceanBase Cloud

The best way to deploy and scale OceanBase

OceanBase Enterprise

Run and manage OceanBase on your infra

TRY OPEN SOURCE

OceanBase Community Edition

The free, open-source distributed database

OceanBase seekdb

Open source AI native search database

Customer Stories

Real-world success stories from enterprises across diverse industries.

View All
BY USE CASES

Mission-Critical Transactions

Global & Multicloud Application

Elastic Scaling for Peak Traffic

Real-time Analytics

Active Geo-redundancy

Database Consolidation

Comprehensive knowledge hub for OceanBase.

Blog

Live Demos

Training & Certification

Documentation

Official technical guides, tutorials, API references, and manuals for all OceanBase products.

View All
PRODUCTS
OceanBase CloudOceanBase Database
ToolsConnectors and Middleware
QUICK START
OceanBase CloudOceanBase Database
BEST PRACTICES

Practical guides for utilizing OceanBase more effectively and conveniently

Learn more about OceanBase – our company, partnerships, and trust and security initiatives.

About OceanBase

Partner

Trust Center

Contact Us

Start on Cloud
编组
All Products
    • Databases
    • iconOceanBase Database
    • iconOceanBase Cloud
    • iconOceanBase Tugraph
    • iconInteractive Tutorials
    • iconOceanBase Best Practices
    • Tools
    • iconOceanBase Cloud Platform
    • iconOceanBase Migration Service
    • iconOceanBase Developer Center
    • iconOceanBase Migration Assessment
    • iconOceanBase Admin Tool
    • iconOceanBase Loader and Dumper
    • iconOceanBase Deployer
    • iconKubernetes operator for OceanBase
    • iconOceanBase Diagnostic Tool
    • iconOceanBase Binlog Service
    • Connectors and Middleware
    • iconOceanBase Database Proxy
    • iconEmbedded SQL in C for OceanBase
    • iconOceanBase Call Interface
    • iconOceanBase Connector/C
    • iconOceanBase Connector/J
    • iconOceanBase Connector/ODBC
    • iconOceanBase Connector/NET
icon

OceanBase Connector/J

V2.4.2

    Download PDF

    OceanBase logo

    The Unified Distributed Database for the AI Era.

    Follow Us
    Products
    OceanBase CloudOceanBase EnterpriseOceanBase Community EditionOceanBase seekdb
    Resources
    DocsBlogWhite PaperLive DemosTraining & CertificationTicket
    Company
    About OceanBaseTrust CenterLegalPartnerContact Us
    Follow Us

    © OceanBase 2026. All rights reserved

    Cloud Service AgreementPrivacy PolicySecurity
    Contact Us
    Document Feedback
    1. Documentation Center
    2. OceanBase Connector/J
    3. V2.4.2
    iconOceanBase Connector/J
    V 2.4.2
    Databases
    • OceanBase Database
    • OceanBase Cloud
    • OceanBase Tugraph
    • Interactive Tutorials
    • OceanBase Best Practices
    Tools
    • OceanBase Cloud Platform
    • OceanBase Migration Service
    • OceanBase Developer Center
    • OceanBase Migration Assessment
    • OceanBase Admin Tool
    • OceanBase Loader and Dumper
    • OceanBase Deployer
    • Kubernetes operator for OceanBase
    • OceanBase Diagnostic Tool
    • OceanBase Binlog Service
    Connectors and Middleware
    • OceanBase Database Proxy
    • Embedded SQL in C for OceanBase
    • OceanBase Call Interface
    • OceanBase Connector/C
    • OceanBase Connector/J
    • OceanBase Connector/ODBC
    • OceanBase Connector/NET
    • V 2.4.18
    • V 2.4.17
    • V 2.4.16
    • V 2.4.15
    • V 2.4.14
    • V 2.4.5
    • V 2.4.4
    • V 2.4.3
    • V 2.4.2
    • V 2.4.1
    • V 2.4.0
    • V 2.2.11
    • V 2.2.10
    • V 2.2.7
    • V 2.2.6
    • V 2.2.3
    • V 2.2.0

    Create a database connection

    Last Updated:2026-04-09 07:21:36  Updated
    Share
    What is on this page
    Create a database connection by using DriverManager
    Create a database connection by using a connection pool

    folded

    Share

    OceanBase Connector/J enables you to create a database connection by using DriverManager or a connection pool.

    Create a database connection by using DriverManager

    We recommend that you use the DriverManager class to connect to OceanBase Connector/J.

    When you use the DriverManager class, set the parameters as follows to establish a connection to OceanBase Database:

    Connection connection = DriverManager.getConnection("jdbc:oceanbase://host:port/user=root&password=***");
    

    Before you use DriverManager, you need to use Class.forName to load the class, and then use the IP address and port number of OceanBase Database to connect to the database. In other word, enter the corresponding IP address, port number, and schema name in String url.

    The following example shows complete sample code.

    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.SQLException;
    public class HelloWorld {
       public static void main(String[] args) {
           try {
               String url = "jdbc:oceanbase://ipaddress:port/shemaname?pool=false";
               String  user = "username";
               String  password = "***";
               Class.forName("com.oceanbase.jdbc.Driver");
               Connection connection = DriverManager.getConnection(url, user, password);
           } catch (Throwable e) {
               e.printStackTrace();
           }
       }
    

    After the database connection is established, you can perform steps in the following example to load the class.

    javac -cp target/oceanbase-client-{version}.jar HelloWorld.java
    java -cp .:target/oceanbase-client-{version}.jar HelloWorld
    

    Create a database connection by using a connection pool

    You can also connect to OceanBase Connector/J by using a connection pool.

    OceanBase Connector/J supports two types of datasource pools:

    • OceanbaseDataSource: the basic implementation of a database connection. A connection is created each time you call the getConnection() method.

    • OceanbasePoolDataSource: a connection pool implementation. The connection pool maintains connection resources. When a connection request is received, a connection is borrowed from the connection pool.

    Internal pool

    The internal pool of OceanBase Connector/J provides a very fast pool implementation and solves the following problems found in the Java pool:

    • Two different connection state cleanup modes are available after a connection is released.

    • Inactive connections are automatically released. In a Java pool, connections are automatically released to avoid problems that may occur when the server closes connections after the @wait_timeout threshold is reached.

    External pool

    You must configure the com.oceanbase.jdbc.Driver class when you use an external connection pool.

    Example: Use the HikariCP JDBC connection pool.

    final HikariDataSource ds = new HikariDataSource();
    ds.setMaximumPoolSize(10);
    ds.setDriverClassName("com.oceanbase.jdbc.Driver");
    ds.setJdbcUrl("jdbc:oceanbase://localhost:2883/db");
    ds.addDataSourceProperty("user", "root");
    ds.addDataSourceProperty("password", "OBClient");
    ds.setAutoCommit(false);
    

    Previous topic

    Import packages
    Last

    Next topic

    Create a Statement object
    Next
    What is on this page
    Create a database connection by using DriverManager
    Create a database connection by using a connection pool