OceanBase
English日本語简体中文
Docs Home
Products
Integrations
Release Notes

Databases

OceanBase DatabaseOceanBase Cloud OceanBase Best Practices

Tools

OceanBase Cloud PlatformOceanBase Migration ServiceOceanBase Developer CenterOceanBase Migration AssessmentOceanBase Admin ToolOceanBase Loader and DumperOceanBase DeployerKubernetes operator for OceanBaseOceanBase Diagnostic Tool OceanBase Binlog Service

Connectors and Middleware

OceanBase Database Proxy OceanBase Connector/C OceanBase Connector/J OceanBase Connector/ODBC OceanBase Connector/NETOceanBase Command-Line Client

Databases

OceanBase DatabaseOceanBase Cloud OceanBase Best Practices

Connectors and Middleware

OceanBase Database Proxy OceanBase Connector/C OceanBase Connector/J OceanBase Connector/ODBC OceanBase Connector/NETOceanBase Command-Line Client

Tools

OceanBase Cloud PlatformOceanBase Migration ServiceOceanBase Developer CenterOceanBase Migration AssessmentOceanBase Admin ToolOceanBase Loader and DumperOceanBase DeployerKubernetes operator for OceanBaseOceanBase Diagnostic Tool OceanBase Binlog Service
OceanBase DatabaseOceanBase Cloud ToolsConnectors and MiddlewareLifecycle Support Policy

OceanBase Connector/J

V2.4.15
  • What is OceanBase Connector J?
    • Overview of OceanBase Connector/J
    • Compatibility requirements
  • Install Driver
    • Install and load OceanBase Connector/J
  • Instructions
    • Quick start
      • Import packages
      • Establish a database connection
      • Create a Statement object
      • Run queries and retrieve ResultSet objects
      • Process ResultSet objects
      • Close ResultSet and Statement objects
      • Manage tables and data
      • Commit changes
      • Close a database connection
      • Examples
    • Data sources and URL
      • Data source overview
      • Database URL
    • Java data streams
      • Overview of Java data streams
      • LONG and LONG RAW data types
      • Use LOBs
        • Overview of LOB data types
        • LOB data interface
        • LOB locator interface
      • CHAR, VARCHAR, and RAW data types
    • Statement caching
      • Overview of statement caching
      • Use statement caching
      • Reuse statement objects
    • Call a stored procedure
    • Handle SQL exceptions
    • Result set
      • Overview of result sets
      • Limitations
      • FetchSize
      • refreshRow
      • useCursorFetch
    • Batch processing
    • Failover and Load-Balancing
      • Failover and load balancing modes
      • LoadBalance strategies
      • Load balancing strategy configuration methods
    • Rich client
    • Logging
    • Network overheads
    • Show Trace
    • Security features
    • Troubleshooting
  • Reference
    • Features specific to the Oracle mode
      • Binary Protocol (Prepared Statements)
        • COM_STMT_PREPARE_EXECUTE
        • COM_STMT_SEND_PIECE_DATA
      • Call PL stored procedures
      • Scrollability of a result set
      • Use ARRAY and STRUCT classes
      • Distributed transactions
        • Overview
        • XA components
        • Distributed transaction examples
      • Obtain remarks
      • Data types supported in Oracle mode
      • Error messages in Oracle mode
    • Supported SQL and PL data types
    • Common APIs
      • Overview
      • java.sql.Connection
      • java.sql.CallableStatement
      • java.sql.DatabaseMetaData
      • java.sql.Driver
      • java.sql.PreparedStatement
      • java.sql.ParameterMetaData
      • java.sql.ResultSet
      • java.sql.ResultSetMetaData
      • java.sql.Statement
      • javax.sql.ConnectionPoolDataSource
      • javax.naming.Context
      • javax.sql.PooledConnection
      • com.oceanbase.jdbc.ObPrepareStatement
  • Release Notes
    • V2.4
      • OceanBase Connector/J V2.4.15
      • OceanBase Connector/J V2.4.14
      • OceanBase Connector/J V2.4.13
      • OceanBase Connector/J V2.4.12
      • OceanBase Connector/J V2.4.11
      • OceanBase Connector/J V2.4.8
      • OceanBase Connector/J V2.4.5
      • OceanBase Connector/J V2.4.4
      • OceanBase Connector/J V2.4.3
      • OceanBase Connector/J V2.4.2
      • OceanBase Connector/J V2.4.1
      • OceanBase Connector/J V2.4.0
    • V2.2
      • OceanBase Connector/J V2.2.11
      • OceanBase Connector/J V2.2.10
      • OceanBase Connector/J V2.2.7
      • OceanBase Connector/J V2.2.6
      • OceanBase Connector/J V2.2.3
      • OceanBase Connector/J V2.2.0

Download PDF

Document Feedback
  1. Documentation Center
  2. OceanBase Connector/J
  3. V2.4.15

Establish a database connection

Last Updated:2026-09-04 08:24:26

Establish a database connection by using DriverManager
Establish a database connection by using a connection pool
Internal pool
External pool

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

Establish 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. To be specific, enter the corresponding IP address, port number, and schema name in String url.

The complete sample code for establishing a database connection is as follows:

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();
       }
   }

Note

In Apsara Stack or a self-managed environment, you must specify the username field in the format of String user = "username@tenant name#cluster name";.

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

Establish 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 datasource pool implementations:

  • OceanbaseDataSource: the basic implementation. It establishes a new connection each time the getConnection() method is called.

  • OceanbasePoolDataSource: a connection pool implementation. It maintains a connection pool, and when a new connection is requested, a connection is borrowed from the pool.

Internal pool

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

  • 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
on this page
Establish a database connection by using DriverManager
Establish a database connection by using a connection pool
Internal pool
External pool