Show TOC

Procedure documentationGetting a Connection to the Database Locate this document in the navigation structure

 

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 Syntax

  1. import java.sql.Connection;
    
    Connection con = ds.getConnection();
    
End of the code.

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.