This topic describes how to create a Java application that uses OceanBase Connector/J to connect to and use OceanBase Database.
Applicability
This topic applies only to OceanBase Database Enterprise Edition. OceanBase Database Community Edition does not support this feature.
Prerequisites
You have set up a basic database development environment.
You have installed Java JDK 8 on your computer.
You have downloaded the OceanBase Connector/J driver package. Go to the Resources > Download Center > Enterprise Edition > Drivers and Middleware section on the OceanBase Database official website, click the corresponding version of OceanBase JDBC Driver, and download the OceanBase Connector/J driver package.
Create a Java application
Step 1: Obtain the database connection string
Contact the deployment personnel or administrator of OceanBase Database to obtain the corresponding database connection string. Example:
obclient -h100.88.xx.xx -usys@oracle -p****** -P2883
The database connection string contains the parameters required to access the database. Before creating an application, you can use the database connection string to verify the login and ensure that the parameters are correct.
The parameters are described as follows:
-h: the IP address for connecting to OceanBase Database. This IP address is usually an ODP address.
-u: the username for connecting to the tenant, in the format of User@Tenant#Cluster Name. The default username for the administrator of the tenant in Oracle-compatible mode is
sys. If you connect to the database directly, you do not need to specify the cluster name. If you connect to the database through an ODP, you must specify the cluster name.-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
Unzip the OceanBase Connector/J JAR package, place it in the local /usr/share/java directory, and configure a temporary environment variable.
mv ./oceanbase-client-{version}.jar /usr/share/java
export CLASSPATH=/usr/share/java/oceanbase-client-{version}.jar:$CLASSPATH
Note
Perform the operations based on the actual version of the downloaded file.
Step 3: Write the application
Write a Java sample file named Test.java.
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 the t_meta_form table.
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 result.
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. Refer to the following fields. The corresponding values are obtained from the database connection string acquired in Step 1.
url: obtained from the
-hand-Pparameters, in the format ofjdbc:oceanbase://IP:port/?pool=false. It is the IP address for connecting to OceanBase Database, which is usually an ODP address, and the port number used for access.user: obtained from the
-uparameter. It is the username for connecting to a tenant, in the format of User@Tenant#Cluster Name. The default username for the administrator of the tenant in Oracle-compatible mode issys. If you connect to the database directly, you do not need to specify the cluster name. If you connect to the database through an ODP, you must specify the cluster name.password: obtained from the
-pparameter. It is the user password.
Step 4: Execute the application
After you edit the code, run the following command to compile it:
javac Test.java
After compilation is complete, run the following command. If the following results are returned, the database connection is successful and the sample statements are correctly executed:
java Test
true
an,1
