⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 database.html

📁 struts api,学习使用struts必备的文档
💻 HTML
📖 第 1 页 / 共 2 页
字号:
    from JavaBean properties.
    If your DBMS or container provides a connection pool that meets these
    requirements, then that component might be your first choice.
    </p>

    <p>
    The Jakarta Commons dbcp's BasicDataSource
    [<code>org.apache.commons.dbcp.BasicDataSource</code>] also works well
    with the DataSource manager, if a native component is not available.
    DBCP is not supplied as part of the Struts distribution. You can obtain 
    it from <a href="http://jakarta.apache.org/commons/dbcp/downloads.html">
    Commons DBCP</a>.
    </p>
    
    <p>
    <strong>Note:</strong> The Generic DataSource which was included with previous 
    versions of Struts has been removed as of release 1.2.0 in favor of 
    plugging in the BasicDataSource, or another DataSource implementation.
    </p>

    <p>
    This is how you would specify a DBCP BasicDataSource for your application:
    </p>

<pre>
<code>
&lt;data-sources&gt;
&lt;!-- configuration for commons BasicDataSource --&gt;
&lt;data-source type="org.apache.commons.dbcp.BasicDataSource"&gt;
    &lt;set-property
      property="driverClassName"
      value="org.postgresql.Driver" /&gt;
    &lt;set-property
      property="url"
      value="jdbc:postgresql://localhost/mydatabase" /&gt;
    &lt;set-property
      property="username"
      value="me" /&gt;
    &lt;set-property
      property="password"
      value="test" /&gt;
    &lt;set-property
      property="maxActive"
      value="10" /&gt;
    &lt;set-property
      property="maxWait"
      value="5000" /&gt;
    &lt;set-property
      property="defaultAutoCommit"
      value="false" /&gt;
    &lt;set-property
      property="defaultReadOnly"
      value="false" /&gt;
    &lt;set-property
      property="validationQuery"
      value="SELECT COUNT(*) FROM market" /&gt;
&lt;/data-source&gt;
&lt;/data-sources&gt;
</code>
</pre>

    <p>
    Note that you can define as many datasource objects as your application
    requires and refer to each using a logical name.
    This can be useful in providing better security or scalability, or even
    to test datasource implementations against each other.
    </p>

    <p>
    After a DataSource is defined, here is an example of using the
    manager to establish a connection from within an Action's
    <code>execute</code> method.
    </p>

<pre>
<code>
public ActionForward
       execute(ActionMapping mapping,
               ActionForm form,
               HttpServletRequest request,
               HttpServletResponse response) throws Exception
{
 javax.sql.DataSource dataSource;
 java.sql.Connection myConnection;
 try {
  dataSource = getDataSource(request);
  myConnection = dataSource.getConnection();
  // do what you wish with myConnection
 } catch (SQLException sqle) {
    getServlet().log("Connection.process", sqle);
 } finally {
    //enclose this in a finally block to make
    //sure the connection is closed
    try {
       myConnection.close();
    } catch (SQLException e) {
       getServlet().log("Connection.close", e);
    }
   }
}
</code>
</pre>

    <p>
    <em>Note: If you use the Commons BasicDataSource with Struts,
    the query you provide for the pingQuery attribute (if you choose to
    include it) must return at least one row.</em>
    </p>

    <p>
    <strong>Example:</strong> <code>SELECT COUNT(*) FROM VALIDTABLE</code>
    </p>

    <p>
    Just be sure you to replace "VALIDTABLE" with the name of a valid table
    in your database.
    </p>

</div>
<h2 id="multi_dsrc">Using Multiple Datasources</h2>
<div class="indent">
    <p>
    If you need more than one data source in a module, you can include a key
    attribute in the data-source element:
    </p>
<pre>
<code>
&lt;data-sources&gt;
   &lt;data-source key="A" type="org.apache.commons.dbcp.BasicDataSource"&gt;
      ... properties as before ...
   &lt;/data-source&gt;
   &lt;data-source key="B" type="org.apache.commons.dbcp.BasicDataSource"&gt;
      ... properties as before ...
   &lt;/data-source&gt;
   ...
&lt;/data-sources&gt;
</code>
</pre>

    <p>
    Which can then be accessed by including the key ("A" in this case) as an
    additional parameter to the Action.getDataSource() method.
    </p>

<pre>
<code>
   ...
   try {
      dataSourceA = getDataSource(request, "A");
      dataSourceB = getDataSource(request, "B");
   ...
</code>
</pre>

    <p>
    Each module can have as many data sources as it needs. The keys only
    need to be unique within a module since the struts module system maintains
    a name space for the items in each module to protect you from name
    clashes.
    </p>
 </div>
<h2 id="more">See Also</h2>
<div class="indent">

    <p>
    <a href="http://www.mail-archive.com/struts-user@jakarta.apache.org/msg24621.html">http://www.mail-archive.com/struts-user@jakarta.apache.org/msg24621.html</a>
<br />
    <a href="http://www.mail-archive.com/struts-user@jakarta.apache.org/msg24709.html">http://www.mail-archive.com/struts-user@jakarta.apache.org/msg24709.html</a>
<br />
    <a href="http://www.mail-archive.com/struts-user@jakarta.apache.org/msg24626.html">http://www.mail-archive.com/struts-user@jakarta.apache.org/msg24626.html</a>
<br />
    <a href="http://www.mail-archive.com/struts-user@jakarta.apache.org/msg24331.html">http://www.mail-archive.com/struts-user@jakarta.apache.org/msg24331.html</a>
<br />
    <a href="http://www.mail-archive.com/struts-user@jakarta.apache.org/msg24102.html">http://www.mail-archive.com/struts-user@jakarta.apache.org/msg24102.html</a>
<br />
    <a href="http://www.mail-archive.com/struts-user@jakarta.apache.org/msg23501.html">http://www.mail-archive.com/struts-user@jakarta.apache.org/msg23501.html</a>
<br />
    <a href="http://www.mail-archive.com/struts-user@jakarta.apache.org/msg23455.html">http://www.mail-archive.com/struts-user@jakarta.apache.org/msg23455.html</a>
<br />
    <a href="http://www.mail-archive.com/struts-user@jakarta.apache.org/msg23375.html">http://www.mail-archive.com/struts-user@jakarta.apache.org/msg23375.html</a>
<br />
    <a href="http://www.mail-archive.com/struts-user@jakarta.apache.org/msg23321.html">http://www.mail-archive.com/struts-user@jakarta.apache.org/msg23321.html</a>
<br />
    <a href="http://www.mail-archive.com/struts-user@jakarta.apache.org/msg23098.html">http://www.mail-archive.com/struts-user@jakarta.apache.org/msg23098.html</a>
<br />
    <a href="http://www.mail-archive.com/struts-user@jakarta.apache.org/msg22713.html">http://www.mail-archive.com/struts-user@jakarta.apache.org/msg22713.html</a>
<br />
    <a href="http://www.mail-archive.com/struts-user@jakarta.apache.org/msg21974.html">http://www.mail-archive.com/struts-user@jakarta.apache.org/msg21974.html</a>
<br />
    <a href="http://www.mail-archive.com/struts-user@jakarta.apache.org/msg21026.html">http://www.mail-archive.com/struts-user@jakarta.apache.org/msg21026.html</a>
<br />
    <a href="http://www.mail-archive.com/struts-user@jakarta.apache.org/msg19338.html">http://www.mail-archive.com/struts-user@jakarta.apache.org/msg19338.html</a>
<br />
    <a href="http://www.mail-archive.com/struts-user@jakarta.apache.org/msg18323.html">http://www.mail-archive.com/struts-user@jakarta.apache.org/msg18323.html</a>
<br />
    <a href="http://www.mail-archive.com/struts-user@jakarta.apache.org/msg14975.html">http://www.mail-archive.com/struts-user@jakarta.apache.org/msg14975.html</a>
<br />
    <a href="http://www.mail-archive.com/struts-user@jakarta.apache.org/msg14914.html">http://www.mail-archive.com/struts-user@jakarta.apache.org/msg14914.html</a>
<br />
    <a href="http://www.mail-archive.com/struts-user@jakarta.apache.org/msg14435.html">http://www.mail-archive.com/struts-user@jakarta.apache.org/msg14435.html</a>
<br />
    <a href="http://www.mail-archive.com/struts-user@jakarta.apache.org/msg01562.html">http://www.mail-archive.com/struts-user@jakarta.apache.org/msg01562.html</a>
    </p>

    <p>
    Transformation/Data Transfer<br />
    <a href="http://www.mail-archive.com/struts-user@jakarta.apache.org/msg24480.html">http://www.mail-archive.com/struts-user@jakarta.apache.org/msg24480.html</a>
<br />
    <a href="http://www.mail-archive.com/struts-user@jakarta.apache.org/msg23623.html">http://www.mail-archive.com/struts-user@jakarta.apache.org/msg23623.html</a>
<br />
    <a href="http://www.mail-archive.com/struts-user@jakarta.apache.org/msg10195.html">http://www.mail-archive.com/struts-user@jakarta.apache.org/msg10195.html</a>
<br />
    <a href="http://www.mail-archive.com/struts-user@jakarta.apache.org/msg10205.html">http://www.mail-archive.com/struts-user@jakarta.apache.org/msg10205.html</a>
    </p>

</div>
<h2 id="DynaResultSet">Rendering a dynamic result set</h2>
<div class="indent">

    <p>
        The result of most queries will map to the ActionForms you are already using,
        and so you can render the ResultSet as a collection of ActionForms.
        But sometimes there are columns in a ResultSet that are not properties of an ActionForm,
        or even known in advance.
    </p>

    <p>
        Happily, the Struts tags don't care what type of bean you use with them.
        You could even output a ResultSet directly.
        But a ResultSet retains a connection to the database, and passing "all that" directly to a JSP gets messy.
        So what's a developer to do?
    </p>

    <p>
        Since Struts 1.1, the simplest option is to use a
        <a href="http://jakarta.apache.org/commons/beanutils/api/org/apache/commons/beanutils/ResultSetDynaClass.html">ResultSetDynaClass</a>
        to transfer the ResultSet into an ArrayList of DynaBeans.
        The Struts custom tags can use DynaBean properties as easily as they use conventional JavaBean properties.
        (See <strong>DynaActionForm classes</strong> in the Struts User Guide for details.)
    </p>

    <p>
        Since this is in the BeanUtils jar, you already have it on board, and just need to implement the transfer routine
        (see the ResultSetDynaClass link).
    </p>

</div>
</div>
<!--end main-->
</div>
<!--end content-->
<div id="footer">
<img id="powered-logo" alt="Powered by Struts" src="../images/struts-power.gif" />
        Copyright (c) 2000-2005, The Apache Software Foundation <span class="noprint">- 
        <a href="http://wiki.apache.org/struts/StrutsDocComments">Comments?</a>
</span>
</div>
<!--end footer-->
</body>
</html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -