来源: http://maping930883.blogspot.jp/2009/03/wls009weblogic-serverdata-source.html

[domain_name]-> Services-> JDBC-> Data Sources
Data Source的作用是从数据库连接池中获取数据库连接,使用完毕后,再放回数据库连接池。

1. 配置DataSource:pointbase数据库


配置成功后,应该能在DataSource所target的Server上的JNDI Tree中,能看到DataSource的JNDI值。

2. 在testdatasource.jsp中访问DataSource,获取数据库连接。


<HTML>
<HEAD><TITLE>Testing Connection Pools</TITLE></HEAD>

<BODY>

<%@ page import="java.sql.*,javax.naming.*,javax.sql.*" %>
<FONT SIZE="5" COLOR="navy">
<table>
<tr><td align="left"><%@ include file="pages/includes/DWRHeader1.jspf" %></td></tr>
<tr><td><CENTER><b>Dizzyworld Testing Center</b></CENTER></td></tr>
</table>

<FORM NAME="testCP" ACTION="testdatasource.jsp" METHOD="POST">
<TABLE WIDTH="50%" ALIGN="left" BGCOLOR="silver">
 <TR><TD ALIGN="right" WIDTH="50%">Data Source Name</TD>
  <TD WIDTH="50%"><INPUT TYPE="text" NAME="txtDataSource" VALUE="dizzyworldDS"></TD>
 </TR>
 <TR><TD ALIGN="right">Table Name</TD>
  <TD><INPUT TYPE="text" NAME="txtTableName" VALUE="EMPLOYEE"></TD>
 </TR>
 <TR><TD ALIGN="right">Username</TD>
  <TD><INPUT TYPE="text" NAME="txtUsername" VALUE="system"></TD>
 </TR>
 <TR><TD ALIGN="right">Password</TD>
  <TD><INPUT TYPE="text" NAME="txtPassword" VALUE="weblogic"></TD>
 </TR>
 <TR><TD ALIGN="center" COLSPAN="2">
   <INPUT TYPE="submit" NAME="btnSubmit" VALUE="Test Data Source">
  </TD>
 </TR>
 
</TABLE>
</FORM>

<%
 String datasource = request.getParameter("txtDataSource");
 String table = request.getParameter("txtTableName");
 String username = request.getParameter("txtUsername");
 String password = request.getParameter("txtPassword");

 if ((datasource != null) && (!datasource.equals(""))) {
  if ((table != null) && (!table.equals(""))) {
   try {

    InitialContext ic = new InitialContext();
    DataSource ds = (DataSource) ic.lookup(datasource);
    System.out.println("Looking up the " + datasource + " data source.");
    Connection connection = ds.getConnection(username, password);
    System.out.println("Getting the connection from the database.");

    Statement statement = connection.createStatement();
    String sql = "SELECT * FROM " + table;

    ResultSet rs = statement.executeQuery(sql);
    System.out.println("Querying the database.");
    ResultSetMetaData meta = rs.getMetaData();
    int numColumns = meta.getColumnCount();
%>
    
    <BR>
    <BR>
    <BR>
    <BR>
    <BR>
    <BR>
    <p>
    <TABLE WIDTH="670" BGCOLOR="wheat" ALIGN="left">
    
<%
    out.print("<TR>");
    for (int j=1; j<numColumns; j++) {
     out.print("<TD>" + meta.getColumnName(j) + "</TD>");
    }
    out.print("</TR>");
    while (rs.next()) {
     out.print("<TR>");
     for (int i=1; i<numColumns; i++) {
      out.print("<TD>" + rs.getString(i) + "</TD>");
     }
     out.print("</TR>");
    }
%>
    </TABLE>
    </p>
<%
    if (statement != null)
     statement.close();
    if (connection != null)
     connection.close();

   } catch (SQLException sqle) {
%>

    </FONT><FONT SIZE="4" COLOR="navy">
    Error: <%= sqle.getMessage() %><BR><BR>
<%
   }catch (Exception e) {out.print("error: " + e); }

  }
 }

%>
</FONT>

<BR>
<BR>
<BR>
<BR>
<BR>
<BR>
<BR>
<BR>
<BR>
<BR>
<BR>
<BR>
<BR>
<BR>
<BR>
<BR>
<BR>
<BR>
<BR>
<BR>
<BR>
<BR>
<BR>
<BR>
<p>
<%@ include file="pages/includes/DWRFooter1.jspf" %></td></tr>
</p>

</BODY>
</HTML>

转载请注明:WebLogic Android 博客 » WLS_009:WebLogic Server基本管理之六:配置Data Source (1)[转]