OceanBase Connector/J connects Python-based applications to OceanBase Database. This topic describes the prerequisites and procedure of this connection method.
Prerequisites
The basic database development environment has been set up.
You have installed or upgraded to JDK 8 on your computer.
The Python environment on your computer is version 3.6.8.
You have obtained the installation package of the OceanBase Connector/J driver from OceanBase Technical Support.
Procedure
Install JayDeBeApi. We recommend that you install JayDeBeApi with pip. For more information, see Install JayDeBeApi with pip.
Place the JAR installation package of OceanBase Connection/J in a local directory.
Create a test object in the database, for example:
CREATE TABLE test_python(id NUMBER, name VARCHAR2(20)); INSERT INTO test_python VALUES (1, 'test1'); INSERT INTO test_python VALUES (2, 'test2'); COMMIT;Establish a connection with OceanBase Database. Enter the corresponding parameter information in the Python code as follows. If valid running results are returned, the connection to the database is established.
In the
urlfield, enter the corresponding IP address and port number.In the
driverfield, enter the classpath.In the
jarFilefield, enter the path of the JAR installation package.
#!/usr/bin/env python3.6 # -*- coding: UTF-8 -*- encoding = "utf8" import jaydebeapi def ob_test(): //ob_test() is a custom Python project name. url = 'jdbc:oceanbase://host:port/database' user = 'testUser' password = 'passxxx' driver = 'com.alipay.oceanbase.jdbc.Driver' jarFile = './oceanbase-client-2.2.3.jar' sqlStr = 'select * from test_python' // SQL test statement # conn=jaydebeapi.connect('oracle.jdbc.driver.OracleDriver','jdbc:oracle:thin:@127.0.0.1:1521/orcl',['hwf_model','hwf_model'],'E:/pycharm/lib/ojdbc14.jar') conn = jaydebeapi.connect(driver, url, [user, password], jarFile) curs = conn.cursor() curs.execute(sqlStr) result = curs.fetchall() print(result) curs.close() conn.close() ob_test()
For more information about how to use OceanBase Connector/J, see OceanBase Connector/J Developer Guide.