This topic describes how to create a data source instance for connecting to a database. Vendor-specific hard-coded property settings are required.
Create a OceanBaseDataSource instance, initialize its connection properties, and obtain a connection instance, as in the following example:
OceanBaseDataSource obds = new OceanBaseDataSource();
obds.setDriverType("oceanbase-client");
obds.setServerName("dlsun111");
obds.setNetworkProtocol("tcp");
obds.setDatabaseName("312");
obds.setPortNumber(1522);
obds.setUser("adam");
obds.setPassword("apple");
Connection conn = obds.getConnection();
Alternatively, you can choose to override the username and password, as in the following example:
Connection conn = obds.getConnection("alice", "orange");
You can use the Java Naming and Directory Interface (JNDI) feature when you connect to a database by using the data source instance. The vendor-specified hard-coded property settings are only required in the code that binds the data source instance to the JNDI logical name. You can use logical names to create portable code when you create data source instances, and thereby obtain connection instances.
Create a data source instance, register the data source instance with JNDI, and open the connection as follows:
Initialize data source properties
Create a
OceanBaseDataSourceinstance, and initialize its properties as required, as in the following example:OceanBaseDataSource obds = new OceanBaseDataSource(); obds.setDriverType("oceanbase-client"); obds.setServerName("dlsun111"); obds.setNetworkProtocol("tcp"); obds.setDatabaseName("312"); obds.setPortNumber(1522); obds.setUser("adam"); obds.setPassword("apple");
Register the data source instance
After you initialize the connection properties of the
OceanBaseDataSourceinstance, you can register the data source instance with JNDI, as in the following example:Context obctx = new InitialContext(); obctx.bind("jdbc/sampleobdb", obds);Call the JNDI
InitialContext()constructor to create a Java object that references the initial JNDI naming text. The system properties (not shown) indicate the service provider to be used by JNDI.The
obctx.bindcall binds theOceanBaseDataSourceinstance to a logical JNDI name. In this way, afterobctx.bindis called, you can open the database connection anytime by using the logical namejdbc/sampleobdband based on theabdsproperty of theOceanBaseDataSourceinstance. The logical table namejdbc/sampleobdbis logically bound to this database.The hierarchical structure of a JNDI namespace is similar to that of a file system. In this example, the JNDI name specifies the subcontext
jdbcunder the root naming context, and specifies the logical namesampleobdbwithin the subcontextjdbc.The Context interface and the
InitialContextclass are included in the standardjavax.namingpackage.
Open a connection
Use the logical JNDI name to perform a lookup and open a connection to the database that is logically bound to a JNDI name. To do this, you need to forcibly convert the search result (which can also be a Java
Object) toOceanBaseDataSource, and then use the correspondinggetConnectionmethod to open the connection.For example:
OceanBaseDataSource obdsconn = (OceanBaseDataSource)obctx.lookup("jdbc/sampleobdb"); Connection conn = obdsconn.getConnection();