skip to main content
Getting started : Connecting from an application : Connecting using JDBC Data Sources : Calling a JDBC Data Source in an application
 

Try DataDirect Cloud Now
Calling a JDBC Data Source in an application
Applications can call a JDBC data source using a logical name to retrieve the javax.sql.DataSource object. This object loads the specified driver and can be used to establish a connection to the DataDirect Cloud service.
Once the JDBC data source has been registered with JNDI, it can be used by your JDBC application as shown in the following code example, where myusername and mypassword are the credentials for your DataDirect Cloud user account.
Context ctx = new InitialContext();
DataSource ds = (DataSource)ctx.lookup("Employee");
Connection con = ds.getConnection("myusername","mypassword");
In this example, the JNDI environment is first initialized. Next, the initial naming context is used to find the logical name of the JDBC data source (Employee). The Context.lookup() method returns a reference to a Java object, which is narrowed to a javax.sql.DataSource object. Finally, the DataSource.getConnection() method is called to establish a connection.
Note: If the login credentials of the data store are not stored in the specified Data Source, the JDBC data source must include them.