This topic describes how to build a Java application using OceanBase Connector/J to connect to and use OceanBase Database.
Applicability
This section applies only to OceanBase Database Enterprise Edition. OceanBase Database Community Edition provides only the MySQL mode.
Prerequisites
You have set up the basic database development environment.
You have installed Java JDK 8 on your computer.
You have obtained the OceanBase Connector/J driver package. On the Resources > Download Center > Enterprise Edition > Drivers and Middleware page of OceanBase's official website, OceanBase JDBC Driver, click the corresponding version, fill in the information, and download the OceanBase Connector/J driver package.
Build a Java application
Step 1: Obtain a database connection string
Obtain a database connection string from OceanBase deployment engineer or administrator. For example:
obclient -h100.88.xx.xx -usys@oracle -p****** -P2881
The database connection string contains parameters required for accessing the database. Before you create an application, you can log in to the database 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 ODP.
-u: the username for connecting to a tenant, in the format of username@tenant name#cluster name. The default username of the administrator in Oracle mode is
sys. The cluster name is not required when you directly connect to the database, but is required when you connect to the 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 OceanBase Connector/J
Decompress the OceanBase Connector/J JAR package and put it in the /usr/share/java directory on your local server. Then, 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 appropriate action based on the actual file version you downloaded.
Step 3: Write an application
Write a Java application 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. The values of the parameters are from the database connection string obtained in Step 1.
url: the IP address and port number for connecting to OceanBase Database, which is usually the IP address of an ODP and the port number for access. The value of this parameter is in the format of
jdbc:oceanbase://IP:port/?pool=false.user: the username for connecting to a tenant, in the format of username@tenant name#cluster name. The default value for the administrator in Oracle mode is
sys. The cluster name is not required when you directly connect to the database, but is required when you connect to the database through an ODP.password: the user password.
Step 4: Run the application
After you edit the code, run the following command:
javac Test.java
If the following results are returned, you have connected to the database successfully, and the sample script is correctly executed:
java Test
true
an,1
More information
For more information about OceanBase Connector/J, see the official documentation.