To access a database directly from a J2EE application client, you retrieve a javax.sql.DataSource object from a resource reference configured in the client deployment descriptor. This resource reference is configured as part of the deployment descriptor for the client application, and provides a reference to a preconfigured data source object.
Note that data access from an application client uses the JDBC driver connection functionality directly from the client side. It does not take advantage of the additional pooling support available in the application server run time. For this reason, your client application should utilize an enterprise bean running on the server side to perform data access. This enterprise bean can then take advantage of the connection reuse and additional added functionality provided by the product run time.
import java.sql.*; import javax.sql.*; import javax.naming.*;
InitialContext ctx = new InitialContext();
javax.sql.DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/myDS"); //where jdbc/myDS is the name of the resource reference
java.sql.Connection conn = ds.getConnection();
java.sql.Connection conn = ds.getConnection("user", "password"); //where user and password are the user id and password for the connection
Statement stmt = conn.createStatement(); String query = "Select FirstNme from " + owner.toUpperCase() + ".Employee where LASTNAME = '" + searchName + "'"; Set rs = stmt.executeQuery(query); while (rs.next()) { firstNameList.addElement(rs.getString(1)); }
conn.close();
Related concepts
Data sources
Related tasks
Configuring data access for the Application Client
Creating or changing a resource reference