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.0
  • What is OceanBase Connector J?
    • Compatibility requirements
    • Overview of OceanBase Connector/J
  • Install Driver
    • Install and load the OceanBase Connector/J driver
  • Instructions
    • Quick start
      • Import packages
      • Create 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
      • Example
    • Data sources and URL
      • Data source overview
      • Data source characteristics and properties
      • Create and connect to a data source instance
      • 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 API
      • CHAR, VARCHAR, and RAW data types
    • Statement caching
      • Overview of statement caching
      • Use statement caching
      • Reuse statement objects
    • Use CallableStatements to call stored procedures
    • Handle SQL exceptions
    • Result set
      • Overview of result sets
      • Limits
      • FetchSize
      • refreshRow
      • useCursorFetch
    • Batch processing
    • Failover and Load-Balancing
      • Failover and load balancing modes
      • LoadBalance strategies
      • LoadBalance strategies configuration methods
    • Security features
    • SQL NetworkCostInfo
    • 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
      • Result set performance
      • Use ARRAY and STRUCT classes
      • Distributed transactions
        • Overview of distributed transactions
        • XA components
        • Distributed transaction examples
      • Error messages in Oracle mode
      • Get remarks information
      • Data types supported in Oracle mode
    • Supported SQL and PL data types
    • Common APIs
      • Overview of common APIs
      • 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
  • Release Notes
    • V2.4
      • 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.0

Create and connect to a data source instance

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

This topic describes how to create a data source instance for connecting to a database. Vendor-specific hard-coded property settings are required.

Create a OceanBaseDataSource instance, initialize its connection properties, and obtain a connection instance, as in the following example:

OceanBaseDataSource obds = new OceanBaseDataSource();
obds.setDriverType("oceanbase-client");
obds.setServerName("dlsun111");
obds.setNetworkProtocol("tcp");
obds.setDatabaseName("312");
obds.setPortNumber(1522);
obds.setUser("adam");
obds.setPassword("apple");
Connection conn = obds.getConnection();

Alternatively, you can choose to override the username and password, as in the following example:

Connection conn = obds.getConnection("alice", "orange");

You can use the Java Naming and Directory Interface (JNDI) feature when you connect to a database by using the data source instance. The vendor-specified hard-coded property settings are only required in the code that binds the data source instance to the JNDI logical name. You can use logical names to create portable code when you create data source instances, and thereby obtain connection instances.

Create a data source instance, register the data source instance with JNDI, and open the connection as follows:

  1. Initialize data source properties

    Create a OceanBaseDataSource instance, and initialize its properties as required, as in the following example:

    OceanBaseDataSource obds = new OceanBaseDataSource();
    obds.setDriverType("oceanbase-client");
    obds.setServerName("dlsun111");
    obds.setNetworkProtocol("tcp");
    obds.setDatabaseName("312");
    obds.setPortNumber(1522);
    obds.setUser("adam");
    obds.setPassword("apple");
    
  1. Register the data source instance

    After you initialize the connection properties of the OceanBaseDataSource instance, you can register the data source instance with JNDI, as in the following example:

    Context obctx = new InitialContext();
    obctx.bind("jdbc/sampleobdb", obds);
    

    Call the JNDI InitialContext() constructor to create a Java object that references the initial JNDI naming text. The system properties (not shown) indicate the service provider to be used by JNDI.

    The obctx.bind call binds the OceanBaseDataSource instance to a logical JNDI name. In this way, after obctx.bind is called, you can open the database connection anytime by using the logical name jdbc/sampleobdb and based on the abds property of the OceanBaseDataSource instance. The logical table name jdbc/sampleobdb is logically bound to this database.

    The hierarchical structure of a JNDI namespace is similar to that of a file system. In this example, the JNDI name specifies the subcontext jdbc under the root naming context, and specifies the logical name sampleobdb within the subcontext jdbc.

    The Context interface and the InitialContext class are included in the standard javax.naming package.

  1. Open a connection

    Use the logical JNDI name to perform a lookup and open a connection to the database that is logically bound to a JNDI name. To do this, you need to forcibly convert the search result (which can also be a Java Object) to OceanBaseDataSource, and then use the corresponding getConnection method to open the connection.

    For example:

    OceanBaseDataSource obdsconn = (OceanBaseDataSource)obctx.lookup("jdbc/sampleobdb");
    Connection conn = obdsconn.getConnection();
    

Previous topic

Data source characteristics and properties
Last

Next topic

Database URL
Next