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 MySQL mode.
Prerequisites
A basic database development environment is deployed.
The Java environment on your computer is Java JDK 8 or later.
You have obtained the installation package of OceanBase Connector/J.
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****** -P2881
The database connection string contains parameters required for accessing OceanBase Database. Before you create an application, you can log on 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 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
The following sample code of Test.java is for your reference:
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 view the output.
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. Values of the parameters are from the database connection string obtained in Step 1.
url: The value of this parameter is obtained from the
-hand-Pparameters, in the format ofjdbc:oceanbase://IP:port/?pool=false. This parameter specifies the IP address and port for connecting to OceanBase Database, which are usually the IP address of an ODP and the port for database access.user: the username for connecting to a tenant, in the format of username@tenant name#cluster name. The value of this parameter is obtained from the
-uparameter. In Oracle 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 to OceanBase Database through an ODP.password: the user password. The value of this parameter is obtained from the
-pparameter.
Step 4: Run the application
After you edit the code, run the following command:
javac Test.java
After the compilation is completed, execute the sample script. If the following results are returned, you have connected to OceanBase Database, and the sample script is correctly executed.
java Test
true
an,1
More information
For more information about how to use OceanBase Connector/J, see the OceanBase Connector/J documentation.