Contents Implementation & Administration Guide 1.1 Previous Next

Application Sample 1: Create a JDBC Connection

A Java Server Page (JSP) called AppSample1.jsp is shown below. This JSP is shipped with the SAS Information Delivery Portal, and it can be found in the jsp\html\samples directory in the portal Web application.

The application creates a Java DataBase Connectivity (JDBC) connection with a SAS table. The table is identified to the application through Global Unqiue Identifier (GUID) that is passed into the application as a parameter.

JDBC is a programming interface that allows Java applications to access a database using Standard Query Language (SQL) statements.

   
<!-- Copyright (c) 2000 by SAS Institute Inc., Cary, NC 27513 -->
<%@ page
  language="java"
  import="com.sas.edir.Debug,
          com.sas.edir.TrackedObject,
          com.sas.edir.datasource.Library,
          com.sas.edir.datasource.Table,
          com.sas.edir.util.StringOp,
          com.sas.edir.webapp.Application,
          com.sas.edir.webapp.portal.PortalEnterpriseDirectory,
          com.sas.edir.webapp.portal.PortalJdbcConnector,
          com.sas.edir.webapp.portal.LogicBean,
          com.sas.edir.webapp.portal.RequestId,
          com.sas.edir.webapp.portal.samples.RB,
          java.sql.Connection,
          java.sql.ResultSet,
          java.sql.Statement,
          java.util.Iterator,
          java.util.Locale,
          java.util.Map,
          java.util.Properties"
  errorPage="PortalError.jsp"
%>
The following code uses logicBean convenience methods to process the request that has been passed from the portal session. These methods use the information in the request to locate the portal user's enterprise directory and the requested application.
<%
LogicBean logicBean = new LogicBean();
PortalEnterpriseDirectory edir = logicBean.getEnterpriseDirectory(request);
Application app = logicBean.getApplication(request);
The following code checks for the existence of the EnterpriseDirectory and Application objects. If they exist, this means the user has accessed the application from within the portal.
// authorization check
if ((edir != null) && (app != null))
{
The following code uses logicBean convenience methods to obtain the active locale setting and the names of currently active style sheets. It also obtains the application's name attribute from the enterprise directory, and it obtains the application's title text from the the samples resource bundle.
    // user is authorized to view this content
    Locale locale = logicBean.getLocale(request);
    Map model = logicBean.getModel(request);
    String sasStyleSheet = logicBean.getSASStyleSheet(request);
    String styleSheet = logicBean.getStyleSheet(request);
    String name = app.getName();
    String title = RB.getString(locale,"appsample1.title.txt");
The following code uses a logicBean convenience method to obtain application parameters from the portal session. In this case, the parameter is a Global Unique Identifier (GUID) that identifies a SAS table. Note: The portal's Personalization process assigns GUIDs to content items when they are added to the portal. To determine a content item's GUID, display the item's Properties window.
    // check for application parameters which may get passed in as a request
    // parameter or attribute
    String tableGuid = logicBean.getValueFromRequest("com.sas.portal.AppSample1.Table", request);
The following code uses the PortalTitle.jsp file to display the title at the top of the page. The appropriate style sheets and the application's title text are incorporated into the page.
    // display the title using PortalTitle.jsp
    request.setAttribute(RequestId.PortalTitle, title);
    request.setAttribute(RequestId.PortalTitleFragment, "true");

%>
<HTML>
  <HEAD>
    <TITLE><%=  title%></TITLE>
    <LINK rel=stylesheet href="<%= sasStyleSheet %>" type="text/css">
    <LINK rel=stylesheet href="<%= styleSheet %>" type="text/css">
  </HEAD>
  <BODY>
  <jsp:include page="/jsp/html/portal/PortalTitle.jsp" flush="true" />
<%
The following code uses the GUID, which was passed in as a parameter, to locate the SAS table.
    // use the GUID passed in to locate the table
    Table table = (Table)logicBean.getEntityFromGuid(tableGuid, request);
    Library library = table.getLibrary();

    if (table != null)
If the table is found, the following code creates a JDBC connection to the table. It then submits an SQL statement that selects all of the table's rows and columns. Results of the query are placed in the "rs" variable. In this example, the result set is not actually used in the output; instead, the table name is assigned to the xArgs variable.
    {
        // create a JDBC connection
        PortalJdbcConnector connector = new PortalJdbcConnector(model, table);
        Connection con = connector.getConnection();
        Statement select = con.createStatement();

        // execute an SQL query
        StringBuffer query = new StringBuffer();
        query.append("select * from ");
        query.append(library.getLibref());
        query.append(".");
        query.append(table.getMemberName());
        ResultSet rs = select.executeQuery(query.toString());

        // This is where you use the result set
        Object xArgs[] = new Object[1];
        xArgs[0] = table.getName();
The following code writes a message to the page, using the table name as an argument. The message text is obtained from the samples resource bundle.
%>
    <p><%= RB.formatString(locale, "appsample1.message.txt", xArgs) %>
<%
        // cleanup
        select.close();
        con.close();
        connector.close();
    }
}
else
{

The following code writes an error message if the authorization test failed. The message text is obtained from the samples resource bundle.
    // user is not authorized to view this content
%>
<HTML>
  <HEAD>
  </HEAD>
  <BODY>
<p align=center><%= RB.getString(logicBean.getLocale(request), "content.notauthorized.txt") %>
<%
}
%>
  </BODY>
</HTML>


Contents Implementation & Administration Guide 1.1 Previous Next