Entering content frame

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

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 SAP 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:

This graphic is explained in the accompanying text

 

Note

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 (Structure linkejb-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. In the example above, 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.

       5.      Save and close ejb-jar.xml.

 

To add a resource reference for a Web component in the SAP NetWeaver Developer Studio:

...

       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 (Structure linkweb-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.

This graphic is explained in the accompanying text

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.

This graphic is explained in the accompanying text

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.

This graphic is explained in the accompanying text

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

 

Leaving content frame