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 SAP NetWeaver Application Server Java you get an instance of a DataSource using resource injection.

For an example about resource injection: Using Dependency Injection

Procedure

To get the Connection object from a DataSource, you use the getConnection method:

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.

More Information

Inserting Data Into a Table

Using Queries

End of Content Area