All Products
Search
Document Center

:Connect to OceanBase Database by using a native MySQL driver

Last Updated:Jan 13, 2023

OceanBase Database in MySQL mode supports open source MySQL drivers in any language. This topic uses the JDBC driver for MySQL as an example to describe how to connect to OceanBase Database by using a native MySQL driver.

Prerequisites

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

For more information about how to download and install MySQL Connector/J, see Connector/J Downloads and Connector/J Installation.

Connect to OceanBase Database

The following example describes 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 the database based on the sample code in the Test.java file.

Note

For the MySQL Connector/J 8.x version, 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://172.30.xx.xx:2883/test?user=r***&password=");
                System.out.println(connection.getAutoCommit());
                Statement sm = connection.createStatement();
                // Perform operations such as dropping a table, creating a table, and inserting data into a table. 
                String q1="drop table if exists t_meta_form";
                sm.executeUpdate(q1);
                String q2="CREATE TABLE t_meta_form ( name varchar(36) NOT NULL DEFAULT ' ', id int NOT NULL ) DEFAULT CHARSET = utf8mb4";
                String q3="insert into t_meta_form (name,id) values ('an','1')";
                sm.executeUpdate(q2);
                sm.executeUpdate(q3);                  

            }catch(SQLException se){
                System.out.println("error!");
                se.printStackTrace() ;
            }
            }catch (Exception ex) {
                ex.printStackTrace();
            }
    }
}

Parameters:

connection = DriverManager.getConnection("jdbc:mysql://{hostname}:{port}/{dbname}?user={username}&password={password}")
  • hostname: the IP address for connecting to the OceanBase database.

  • port: the port for connecting to the OceanBase database, which is also the listening port of OBProxy. The default value is 3306. You can customize the value as required.

  • dbname: the name of the database to access.

  • username: the username of the tenant connection account.

  • password: the password of the tenant connection account.

Example: jdbc:mysql://172.30.xx.xx:3306/test?user=r***&password=***1**

After you edit the code, you can run the following commands to compile and run the code. If true is returned, you have connected to the database.

// Manually add the driver to the environment configuration 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
// Run the code.
java Test