This topic describes how to build a Java application with OceanBase Database and OceanBase Connector/J.
Applicability
This topic applies only to OceanBase Database Enterprise Edition. OceanBase Database Community Edition provides only the MySQL-compatible mode.
Prerequisites
A basic database development environment is deployed.
The Java environment on your computer is Java JDK 8.
You have obtained the installation package of OceanBase Connector/J. If not, obtain it from OceanBase Download Center.
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 -usys@oracle -p****** -P2883
The database connection string contains parameters required for accessing OceanBase Database. Before you create an application, you can log in 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. In Oracle-compatible mode, the default username of the administrator is
sys. 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.
Step 2: Install the OceanBase Connector/J driver
Decompress the JAR package of OceanBase Connector/J, place it in the local /usr/share/java path, and set the temporary environment variables.
mv ./oceanbase-client-{version}.jar /usr/share/java
export CLASSPATH=/usr/share/java/oceanbase-client-{version}.jar:$CLASSPATH
Note
Perform the preceding operations based on the downloaded file version.
Step 3: Write an application
Write the Java sample file Test.java as follows:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.ResultSet;
import java.sql.Statement;
public class Test {
public static void main(String[] args) {
try {
Class.forName("com.oceanbase.jdbc.Driver");
Connection connection = DriverManager.getConnection("jdbc:oceanbase://172.30.xx.xx:2881/?pool=false&user=s**@oracle&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 ex) {
System.out.println("error!");
ex.printStackTrace() ;
}catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
Modify the database connection parameters in the code. The parameters are as follows, and the values are from the database connection string obtained in Step 1.
url: The value is obtained from the
-hand-Pparameters, in the format ofjdbc:oceanbase://IP:port/?pool=false. This specifies the IP address for connecting to OceanBase Database, which is usually an ODP address, and the port number for access.user: The value is obtained from the
-uparameter. The username for connecting to a tenant, in the format of username@tenant name#cluster name. In Oracle-compatible mode, the default username of the administrator issys. The cluster name is not required when you directly connect to OceanBase Database, but is required when you connect through an ODP.password: The value is obtained from the
-pparameter. The user password.
Step 4: Run the application
After you edit the code, run the following command to compile:
javac Test.java
After the compilation is completed, execute the application. If the following results are returned, you have connected to OceanBase Database successfully, and the sample script is correctly executed:
java Test
true
an,1
More information
For a detailed example of connecting to OceanBase Database through OceanBase Connector/J, see Connect to OceanBase Database by using OceanBase Connector/J.