<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    Java進行時
          把握現在,成就未來!
    posts - 23,comments - 30,trackbacks - 0
    配置Struts數據源有很多種方法,我們這里講解最實用的一種。

    配置數據源所需要的JAR包:commons-dbcp-1.2.2.jar、commons-pool-1.4.jar、struts-legacy.jar
    下載地址:
    http://m.tkk7.com/Files/biiau/struts-dataSource.rar

    1.配置struts-config.xml文件,加入數據源

    <!-- ============ Data Source Start ================== -->
     <data-sources>
      <data-source key="org.apache.struts.action.DATA_SOURCE"
       type="org.apache.commons.dbcp.BasicDataSource">
       <set-property property="autoCommit" value="true" />
       <set-property property="description"  value="SQL2000 Data Source" />
       <set-property property="driverClassName"
        value="net.sourceforge.jtds.jdbc.Driver" />
       <set-property property="maxCount" value="10" />
       <set-property property="minCount" value="2" />
       <set-property property="username" value="username" />
       <set-property property="password" value="password" />
       <set-property property="url"
        value="jdbc:jtds:sqlserver://localhost:1433/databaseName" />
      </data-source>
     </data-sources>
    <!-- ============ Data Source End ================== -->

    2.添加STRUTS插件;添加插件的目的是:任何文件中都可以用到此數據源


    首先創建插件,它繼承自org.apache.struts.action.PlugIn     

     

    package com.xxx.db;

    import java.sql.Connection;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;

    import javax.sql.DataSource;

    import org.apache.struts.action.ActionServlet;
    import org.apache.struts.action.PlugIn;
    import org.apache.struts.config.ModuleConfig;

    public class DBConn implements PlugIn {

     
    private static DataSource dataSource = null;
     
    private Connection conn = null;
     
    private PreparedStatement preStmt = null;
     
    private Statement stmt = null;

     
    // 得到數據源
     public void init(ActionServlet servlet, ModuleConfig config) {
      dataSource 
    = (DataSource) servlet.getServletContext().getAttribute(
        
    "org.apache.struts.action.DATA_SOURCE");
     }


     
    public DBConn() throws SQLException {
      
    if (dataSource != null{
       conn 
    = dataSource.getConnection();
      }

     }


     
    public ResultSet executeQuery(String sql) {
      ResultSet rs 
    = null;

      
    try {
       
    if (stmt == null{
        stmt 
    = conn.createStatement();
       }


       rs 
    = stmt.executeQuery(sql);
      }
     catch (SQLException e) {
       e.printStackTrace();
      }


      
    return rs;
     }


     
    public void executeUpdate(String sql) throws SQLException {
      
    if (stmt == null{
       stmt 
    = conn.createStatement();
      }


      stmt.executeUpdate(sql);
     }


     
    public Connection getConnection() {
      
    return conn;
     }


     
    public void prepareStatement(String sqlStr) throws SQLException {
      preStmt 
    = conn.prepareStatement(sqlStr);
     }


     
    public void setString(int index, String value) throws SQLException {
      preStmt.setString(index, value);
     }


     
    public void setInt(int index, int value) throws SQLException {
      preStmt.setInt(index, value);
     }


     
    public void setBoolean(int index, boolean value) throws SQLException {
      preStmt.setBoolean(index, value);
     }


     
    public void setLong(int index, long value) throws SQLException {
      preStmt.setLong(index, value);
     }


     
    public void setFloat(int index, float value) throws SQLException {
      preStmt.setFloat(index, value);
     }


     
    public void setBytes(int index, byte[] value) throws SQLException {
      preStmt.setBytes(index, value);
     }


     
    public void clearPreStmt() throws SQLException {
      preStmt.clearParameters();
      preStmt 
    = null;
     }


     
    public ResultSet executeQuery() throws SQLException {
      
    if (preStmt != null{
       
    return preStmt.executeQuery();
      }
     else {
       
    return null;
      }

     }


     
    public void executeUpdate() throws SQLException {
      
    if (preStmt != null{
       preStmt.executeUpdate();
      }

     }


     
    public void close() {
      
    try {
       
    if (stmt != null{
        stmt.close();
        stmt 
    = null;
       }


       
    if (preStmt != null{
        preStmt.close();
        preStmt 
    = null;
       }


       
    if (conn != null{
        conn.close();
        conn 
    = null;
        System.out.println(
    "**** a connection is closed ****");
       }

      }
     catch (Exception e) {
       System.err.println(e.getMessage());
      }

     }


     
    public void destroy() {
     }

    }


    然后配置struts-config.xml文件:
             <plug-in className="com.xxx.db.DBConn"></plug-in>

    3.到此,數據源完成。用法示例:
            Connection conn =  new DBCon().getConnection();

    本配置只適合初學者。。。。。。

    posted on 2008-04-16 17:21 biiau 閱讀(1178) 評論(0)  編輯  收藏

    只有注冊用戶登錄后才能發表評論。


    網站導航:
     
    主站蜘蛛池模板: 久久综合九九亚洲一区| 亚洲区小说区图片区| 亚洲综合免费视频| 久久国产乱子免费精品| 色欲A∨无码蜜臀AV免费播| 日本红怡院亚洲红怡院最新| 中文字幕无线码免费人妻| 中文亚洲AV片在线观看不卡| 久久九九免费高清视频| 欧亚精品一区三区免费| 亚洲桃色AV无码| 亚洲免费综合色在线视频| a级毛片免费全部播放| 午夜精品在线免费观看| 亚洲自偷自偷在线成人网站传媒 | 亚洲AV日韩AV天堂久久| 久久久久成人片免费观看蜜芽 | 久久精品国产99国产精品亚洲| 中国人xxxxx69免费视频| 国产午夜亚洲精品午夜鲁丝片| 亚洲免费人成视频观看| 久草免费福利视频| www国产亚洲精品久久久| 一级做α爱过程免费视频| 亚洲国产日韩在线视频| 30岁的女人韩剧免费观看| 中文字幕无码精品亚洲资源网久久 | 亚洲国产a级视频| a毛片全部播放免费视频完整18| 亚洲精品天天影视综合网| 一道本在线免费视频| 狼友av永久网站免费观看| 一级毛片免费视频网站| 免费看国产精品麻豆| 97超高清在线观看免费视频| 亚洲 小说区 图片区 都市| 国产永久免费高清在线| 亚洲精品无码久久久久去q | 色老头综合免费视频| 国产精品国产自线拍免费软件| 久青草视频97国内免费影视|