struts1.2 与struts1.1数据源的配置

原文链接 http://club.yc.nx.cn/blog/user1/xxyipt/archives/2007/1780.html

  一、下载必要的jar包。有四个包要下载(网上有贴子说只要下面前三个包就可以了,但我在配置的时候如果没有第四个包会报错)。
      http://apache.linuxforum.net/dist/jakarta/commons/dbcp/binaries/commons-dbcp-1.2.zip
     http://apache.linuxforum.net/dist/jakarta/commons/pool/binaries/commons-pool-1.2.zip
     http://apache.linuxforum.net/dist/jakarta/struts/struts-legacy/struts-legacy-1.0.zip
      http://apache.linuxforum.net/dist/jakarta/commons/collections/binaries/commons-collections-3.1.zip
    把这四个jar包放到web应用的WEB-INF/lib目录下,还有要把mySQL的驱动程序包也放到这里(也可以把数据库的驱动包放到%Tomcat_Home%/common/lib目录下或%Resin_Home%/lib目录下,这样在启动Web服务器的时候就会加载这个包,服务器和Web应用都可以使用驱动程序类)

二、在struts-config.xml中添加Data Source

xml 代码
  1. < data-sources >   
  2.      < data-source   key = "datasource"   type = "org.apache.commons.dbcp.BasicDataSource" >   
  3.        < set-property   property = "password"   value = "admin"   />   
  4.        < set-property   property = "minCount"   value = "3"   />   
  5.        < set-property   property = "maxCount"   value = "10"   />   
  6.        < set-property   property = "username"   value = "admin"   />   
  7.        < set-property   property = "driverClassName"   value = "/blog/com.mysql.jdbc.Driver"   />   
  8.        < set-property   property = "description"   value = "test"   />   
  9.        < set-property   property = "url"   value = "jdbc:mysql://localhost/test"   />   
  10.        < set-property   property = "readOnly"   value = "false"   />   
  11.        < set-property   property = "autoCommit"   value = "true"   />   
  12.      data-source >   
  13.    data-sources >   
  14.   

如果要配置多个data source,可以给每个data source指定不同的key,然后在程序中根据key指定要引用的数据源。

三、测试数据源。配置好以后就可以在ActionServlet中使用了

java 代码
  1. DataSource ds = getDataSource(request, "datasource" );   
  2.   Connection conn = ds.getConnection();   
  3.   Statement stmt = conn.createStatement();   
  4.   ResultSet rs = stmt.executeQuery( "SQL String" );   
附: Struts1.1中struts-config.xml的配置:
xml 代码
  1. < data-sources   >   
  2.      < data-source   key = "datasource"   type = "org.apache.struts.util.GenericDataSource" >   
  3.        < set-property   property = "password"   value = "admin"   />   
  4.        < set-property   property = "minCount"   value = "2"   />   
  5.        < set-property   property = "maxCount"   value = "10"   />   
  6.        < set-property   property = "user"   value = "admin"   />   
  7.        < set-property   property = "driverClass"   value = "/blog/com.mysql.jdbc.Driver"   />   
  8.        < set-property   property = "description"   value = "test"   />   
  9.        < set-property   property = "url"   value = "jdbc:mysql://localhost/test"   />   
  10.        < set-property   property = "readOnly"   value = "false"   />   
  11.        < set-property   property = "autoCommit"   value = "true"   />   
  12.      data-source >   
  13.    data-sources >   
注意:1.2和1.1在配置上不一样的地方
  1. < data-source   key = "datasource"   type = "org.apache.commons.dbcp.BasicDataSource" >   
  2. < set-property   property = "username"   value = "admin"   />   
  3. < set-property   property = "driverClassName"   value = "/blog/com.mysql.jdbc.Driver"   />   

你可能感兴趣的:(apache,mysql,Web,struts,jdbc)