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 installation package of OceanBase Connector/J in the local directory by using cmd and perform the following connection test:
Compile the Java file
HelloWorld.javaand load OceanBase Connector/J.public class HelloWorld { public static void main(String[] args) { try { Class.forName("com.oceanbase.jdbc.Driver"); } catch (ClassNotFoundException e) { e.printStackTrace(); } } }Run the following command to compile the Java sample file:
javac -cp target/oceanbase-client-{version}.jar HelloWorld.javaNote
You can change the compilation path as needed.
Run the following command to run the Java sample file:
java -cp .:target/oceanbase-client-{version}.jar HelloWorldIf steps 2 and 3 are completed with no error message returned, the JAR package of OceanBase Connector/J is correctly loaded.
Description
For OceanBase deployed in a private cloud or independently, the
usernameis in the format ofString username= "Username@Tenant name#Cluster name";Connect to OceanBase Database.
After OceanBase Connector/J is loaded, connect to OceanBase Database based on the IP address and port. Enter the actual IP address, port number, and schema name in the
String urlfield in the following code: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://ipaddress:port/shemaname?pool=false"; String user = "username"; String password = "password"; Class.forName("com.oceanbase.jdbc.Driver"); Connection connection = DriverManager.getConnection(url, user, password); } catch (ClassNotFoundException e) { e.printStackTrace(); } }After the connection to OceanBase Database is established, repeat steps 2 and 3 to load classes.
Note
For OceanBase deployed in a private cloud or independently, the
usernameis in the format ofString username= "Username@Tenant name#Cluster name";
For more information about OceanBase Connector/J, see OceanBase Connector/J.