Show TOC Start of Content Area

Procedure documentation Getting a Connection to the Database  Locate the document in its SAP Library structure

Use

To send commands to a database and to receive results, first you need a connection to the database.

You establish a database connection using a javax.sql.DataSource object. In the J2EE Engine you can use the default DataSource created at installation time. With this DataSource you get a connection to the underlying Java database schema using the standard schema user SAP<SID>DB. For more information, see Using the Default DataSource. In addition, you can create new DataSource objects using the JDBC Connector Service.

Procedure

Note

The following procedure is relevant for J2EE application components (enterprise beans, servlets, and Java ServerPages).

WebDynpro components do not support the declaration of resource references. To use a preconfigured data source, you must perform the lookup operation directly from the jdbc/ naming context, without using the prefix java:comp/env.

Adding a Resource Reference

To identify the DataSource, we use a logical name that is defined as a resource reference name in the deployment descriptor of the application component that uses the DataSource – that is, web.xml for Web components, or ejb-jar.xml for enterprise beans.

Using resource references has several purposes:

·         Resource references are defined by the J2EE standard. Using them makes your applications portable.

·         By specifying a resource reference, you can access resources on the J2EE Engine such as DataSource objects or other types of connection factories, even if they are not deployed with your application.

·         Declaring resource references enables you to manage the use of other features, such as connection sharing.

To add a resource reference for an enterprise bean:

...

       1.      In the J2EE Development perspective, expand the relevant EJB Module Project and open ejb-jar.xml.

       2.      Go to the Enterprise Beans tab and browse to the node of the relevant bean and expand it.

       3.      Select resource-ref and choose Add.

       4.      Edit the properties of the new resource reference as follows:

Resource Reference Parameters

Parameter Name

Value

Resource Reference Name

The resource reference name may be:

·        The proper name of the DataSource – this is the name that you specify when you create the DataSource. For example, CAR_RENTAL_POOL.

·        An alias that you have set for this DataSource. For information, see Managing Aliases.

·        An arbitrary name – in this case, you must specify the JNDI name of the DataSource in the additional deployment descriptor (ejb-j2ee-engine.xml). To do this, open the descriptor, go to the Enterprise beans tab and browse to the node of the relevant bean. Expand it and add a resource-ref. The JNDI name of the DataSource is either its proper name, or an alias. For example, if the resource reference name is set to jdbc/CAR_RENTAL_POOL, you must map it to CAR_RENTAL_POOL, which is the JNDI name of the used DataSource alias.

Resource Reference Type

javax.sql.DataSource

Resource Authentication

Container

Resource Sharing Scope

We recommend that you use this option and select Shareable. For more information about sharing scope, see Sharing Connections.

       5.      Save and close ejb-jar.xml.

 

To add a resource reference for a Web component:

...

       1.      In the J2EE Development perspective, expand the relevant Web Module Project and open web.xml.

       2.      Go to the Resource tab. Select Resource entries. Choose Add and edit the reference as described in step 4 above.

       3.      Save and close web.xml.

Note

If you choose to use an arbitrary name for the reference, you should specify the JNDI name of the DataSource in the additional deployment descriptor (web-j2ee-engine.xml).

Performing the Lookup Operation

The following steps describe the code that you have to include in your application component to look up the DataSource:

       1.      Get new initial naming context.

Syntax

import javax.naming.Context;

import javax.naming.InitialContext;

import javax.sql.DataSource;

 

...

try {

 

         InitialContext ctx = new InitialContext();

...

}

 

       2.      Look up the DataSource by the resource reference name – in our example, it is jdbc/CAR_RENTAL_POOL. You look up the DataSource from the java:comp/env naming context.

Syntax

DataSource ds = (DataSource) context.lookup("java:comp/env/jdbc/CAR_RENTAL_POOL")

 

Getting a Connection

The next step is to get the Connection object from the DataSource you acquired.

Syntax

import java.sql.Connection;

 

Connection con = ds.getConnection();

 

The Connection object is the crucial point for working with a database. This object has access to methods that return the additional objects that we require for sending commands to the database:

·        The method createStatement returns a Statement object that can be used to send simple SQL statements to the database.

·        The method createPreparedStatement returns a PreparedStatement object that can be used to issue SQL statements that contain parameters (host variables) to the database.

 

See also:

 

Inserting Data into A Table

Using Queries

 

End of Content Area