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 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
Decompress the OceanBase Connector/J JAR package and copy it to the local /usr/share/java directory. Then, set the temporary environment variable.
mv ./oceanbase-client-{version}.jar /usr/share/java
export CLASSPATH=/usr/share/java/oceanbase-client-{version}.jar:$CLASSPATH
Note
Perform the corresponding operations based on the downloaded file version.
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 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. For more information, see the following table. The values of the parameters are obtained from the database connection string obtained in Step 1.
url: the value is obtained from the
-hand-Pparameters. The value is in thejdbc:oceanbase://IP:port/?pool=falseformat. The IP address for connecting to OceanBase Database is usually an ODP address, and the port number is the port for accessing the database.user: the value is obtained from the
-uparameter. The value is in the User@Tenant#Cluster Name format. The default username for the administrator of the tenant in Oracle 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: the value is obtained from the
-pparameter. The value 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 the code is compiled, run it. If the following result is returned, the database connection is successful and the sample statements are correctly executed:
java Test
true
an,1
