You can connect to OceanBase Database by using the Java Database Connectivity (JDBC) driver of MySQL.
Prerequisites
You have installed or upgraded to JDK 8 or later on your computer.
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.
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.
Notice
For the MySQL Connector/J 8.x version, you must replace
com.mysql.jdbc.Driverwithcom.mysql.cj.jdbc.DriverinClass.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: specifies the IP address for connecting to the OceanBase database, which is usually the IP address of an OBProxy.
port: specifies the port for connecting to the OceanBase database, which is also the listening port of the OBProxy. Default value: 2883, which can be changed.
dbname: specifies the name of the database to access.
username: specifies the account for connecting to the tenant. In MySQL mode, the default username of the tenant administrator is
root.password: specifies the password of the account.
Example: jdbc:mysql://172.30.xx.xx:2883/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