OceanBase logo

OceanBase

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

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

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

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 Database

SQL - V4.3.0

    Download PDF

    OceanBase logo

    The Unified Distributed Database for the AI Era.

    Follow Us
    Products
    OceanBase CloudOceanBase EnterpriseOceanBase Community EditionOceanBase seekdb
    Resources
    DocsBlogLive DemosTraining & Certification
    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 Database
    3. SQL
    4. V4.3.0
    iconOceanBase Database
    SQL - V 4.3.0
    SQL
    KV
    • V 4.4.2
    • V 4.3.5
    • V 4.3.3
    • V 4.3.1
    • V 4.3.0
    • V 4.2.5
    • V 4.2.2
    • V 4.2.1
    • V 4.2.0
    • V 4.1.0
    • V 4.0.0
    • V 3.1.4 and earlier

    Build a Java application

    Last Updated:2026-04-15 08:30:00  Updated
    share
    What is on this page
    Prerequisites
    Procedure
    Step 1: Obtain a database connection string
    Step 2: Write an application
    Step 3: Run the application
    More information

    folded

    share

    This topic describes how to build a Java application with OceanBase Database and MySQL Connector/J. MySQL Connector/J is an official Java Database Connectivity (JDBC) driver of MySQL.

    Prerequisites

    • You have installed Java JDK 8 or later for the Java environment.

    • You have installed MySQL Connector/J and configured the operating environment.

      We recommend that you use MySQL Connector/J 5.1.47. For more information about how to download and install MySQL Connector/J, see Connector/J Downloads and Connector/J Installation.

    Procedure

    Step 1: Obtain a database connection string

    Obtain a database connection string from a database deployment engineer or administrator. For example:

    obclient  -h100.88.xx.xx -uroot@test -p****** -P2881 -Doceanbase
    

    The database connection string contains parameters required for accessing OceanBase Database. Before you create an application, you can log on to OceanBase Database by using the database connection string, to verify that the parameters are correct.

    The parameters are described as follows:

    • -h: the IP address for connecting to OceanBase Database, which is sometimes the IP address of an OceanBase Database Proxy (ODP).
    • -u: the username for connecting to a tenant, in the format of username@tenant name#cluster name. The default tenant of a cluster is sys, and the default administrator of a tenant is root. The cluster name is not required when you directly connect to OceanBase Database, but is required when you connect to OceanBase Database through an ODP.
    • -p: the user password.
    • -P: the port for connecting to OceanBase Database, which is also the listening port of the ODP.
    • -D: the name of the database to be accessed.

    Step 2: Write an application

    The following example shows how to use Connector/J 5.1.47 to connect to an OceanBase database in Linux.

    After you install MySQL Connector/J 5.1.47 and configure the operating environment, you can connect to and use the database based on the sample code in the Test.java file.

    Notice

    For MySQL Connector/J 8.x, you must replace com.mysql.jdbc.Driver with com.mysql.cj.jdbc.Driver in Class.forName("com.mysql.jdbc.Driver").

    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    
    public class Test {
       public static void main(String[] args) {
           try {
    
                Class.forName("com.mysql.jdbc.Driver").newInstance();
    
                try{
                    
                    Connection connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1:2881/test?user=r***&password=");
                    System.out.println(connection.getAutoCommit());
                    Statement sm = connection.createStatement();
                    //Create a table named t_meta_form.
                    sm.executeUpdate("CREATE TABLE t_meta_form (name varchar(36) , id int)");
                    //Insert data.
                    sm.executeUpdate("insert into t_meta_form values ('an','1')");
                    //Query data and output the results.
                    ResultSet rs = sm.executeQuery("select * from t_meta_form");
                    while (rs.next()) {
                        String name = rs.getString("name");
                        String id = rs.getString("id");
                        System.out.println(name + ','+ id);
                    }
                    //Drop the table.
                    sm.executeUpdate("drop table t_meta_form");                
    
                }catch(SQLException se){
                    System.out.println("error!");
                    se.printStackTrace() ;
                }
                }catch (Exception ex) {
                    ex.printStackTrace();
            }
        }
    }
    

    Modify the database connection parameters in the code. The parameters are concatenated, and the values of the parameters are from the database connection string obtained in Step 1.

    connection = DriverManager.getConnection("jdbc:mysql://{host}:{port}/{dbname}?user={username}&password={******}")
    
    //Example
    jdbc:mysql://100.88.xx.xx:2881/test?user=r***&password=******`
    
    • host: the IP address for connecting to OceanBase Database, which is sometimes the IP address of an ODP. The value of this parameter is obtained from the -h parameter.

    • port: the port for connecting to OceanBase Database, which is also the listening port of the ODP. The value of this parameter is obtained from the -P parameter.

    • dbname: the name of the database to be accessed. The value of this parameter is obtained from the -D parameter.

    • username: the username for connecting to a tenant, in the format of username@tenant name#cluster name. The value of this parameter is obtained from the -u parameter. The default tenant of a cluster is sys, and the default administrator of a tenant is root. The cluster name is not required when you directly connect to OceanBase Database, but is required when you connect to OceanBase Database through an ODP.

    • password: the user password. The value of this parameter is obtained from the -p parameter.

    Step 3: Run the application

    After you edit the code, run the following command:

    # Configure a temporary environment based on the actual installation path of mysql-connector-java-5.1.47.jar.
    export CLASSPATH=/usr/share/java/mysql-connector-java-5.1.47.jar:$CLASSPATH
    # Compile the code.
    javac Test.java
    

    After the compilation is completed, execute the Test script:

    java Test
    # If the following results are returned, you have connected to OceanBase Database, and the sample script is correctly executed.
    true
    an,1
    

    More information

    For more information about how to create a Java application, see the sample Java application in the open-source community of OceanBase Database.

    Previous topic

    Build a Python application
    Last

    Next topic

    Build a C application
    Next
    What is on this page
    Prerequisites
    Procedure
    Step 1: Obtain a database connection string
    Step 2: Write an application
    Step 3: Run the application
    More information