This topic describes how to connect Java applications to OceanBase Database.
Prerequisites
A basic database development environment is deployed.
You have installed or upgraded to JDK 8 on your computer.
You have obtained the installation package of the OceanBase Connector/J driver from OceanBase Technical Support.
Procedure
Place the JAR package of OceanBase Connector/J in the local
/usr/share/javapath and set the temporary environment variables.mv ./oceanbase-client-{version}.jar /usr/share/java export CLASSPATH=/usr/share/java/oceanbase-client-{version}.jar:$CLASSPATHNote
Perform the preceding operations based on the downloaded file version.
Compile the Java file
HelloWorld.java.import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; public class HelloWorld { public static void main(String[] args) { try { String url = "jdbc:oceanbase://172.30.xx.xx:2881/?pool=false"; String user = "s**@oracle"; String password = "***1**"; Class.forName("com.oceanbase.jdbc.Driver"); Connection connection = DriverManager.getConnection(url, user, password); System.out.println(connection.getAutoCommit()); }catch (SQLException ex) { System.out.println("error!"); ex.printStackTrace() ; }catch (ClassNotFoundException e) { e.printStackTrace(); } } }Parameters
url:
jdbc:oceanbase://IP:port/?pool=false. Specifies the IP address and port for connecting to OceanBase Database, which is usually the IP address of an OBProxy.user: the username for connecting to the tenant, in the
username@tenant nameformat. In Oracle mode, the default username of the administrator isSYS.Note
In a private cloud or standalone deployment scenario, the format of the
usernameisString username= "username@tenant#cluster".password: specifies the password of the account.
After you edit the code, you can run the following commands to compile and run the code. If true is returned, you have connected to the database.
// Compile the code. javac Test.java // Run the code. java Test
For more information about how to use OceanBase Connector/J, see OceanBase Connector/J Developer Guide.