
The JDBC Connector for the SAP NetWeaver Portal is a connector framework implementation based on JDBC.
There are two basic ways to get a connection using the JDBC Connector:
Via the Connector Service-this approach being faster, simpler, and benefits from the portal's user mapping from the system
Direct connection using the J2EE JNDI
Connecting via the Connector Service
Example for a connection to a JDBC system using the connector service:
IConnection connection = null;
// get the Connector Gateway Service
String jdbcSystem = "pubs";
IConnectorGatewayService cgService = (IConnectorGatewayService)
PortalRuntime.getRuntimeResources().getService(IConnectorService.KEY);
ConnectionProperties cp = new ConnectionProperties(portalRequest.getLocale(),
portalRequest.getUser());
try {
connection = cgService.getConnection(jdbcsystem, cp);
} catch (Exception e) {
} finally {
try {
if (connection != null) {
connection.close();
}
} catch (ResourceException re) {
}
}
Connecting via J2EE
Example for a connection to a JDBC system using the J2EE JNDI:
// physical connection
IConnection connection = null;
//connection factory of the JDBC connector to get the connection
IConnectionFactory connectionFactory;
Context initctx = null;
try {
//get the initial JNDI context
Hashtable env = new Hashtable();
env.put("domain", "true");
initctx = new com.sapportals.portal.prt.jndisupport.InitialContext(env);
// perform JNDI lookup to get the connection factory
connectionFactory = (IConnectionFactory)
initctx.lookup("deployedAdapters/JDBCFactory/shareable/JDBCFactory");
// retrieve the ConnectionSpec and set the values
IConnectionSpec spec = connectionFactory.getConnectionSpec();
// set properties
spec.setPropertyValue("UserName", "sa");
spec.setPropertyValue("Password", "admin");
spec.setPropertyValue("driver", "com.sap.portals.jdbc.sqlserver.SQLServerDriver");
spec.setPropertyValue("url","jdbc:sap:sqlserver://localhost:1433;DatabaseName=Northwind");
// Retrieve the connection handler
connection = connectionFactory.getConnectionEx(spec);
} catch (Exception e) {
} finally {
try {
if (connection != null) {
connection.close();
}
} catch (ResourceException re) {
}
}
The following sections provide code samples illustrating the use of the JDBC Connector: