日期:2014-05-18  浏览次数:20757 次

请教struts框架中连接postgresql数据库的问题
我想在struts框架中连接postgresql数据库,在网上找到了两种对数据源的配置,一种是:
struts-config.xml:

......
<data-sources>
<data-source   key   =   "PostgresqlDB "   type= "javax.sql.DataSource "    factory= "org.apache.commons.dbcp.BasicDataSourceFactory ">      
<set-property       value= "true "       property= "autoCommit "/>      
<set-property       value= "Example       Data       Source            Configuration "       property= "description "/>      
<set-property       value= "org.postgresql.Driver "   property= "driverClass "/>      
<set-property       value= "5 "       property= "maxCount "/>      
<set-property       value= "1 "       property= "minCount "/>      
<set-property       value= "jdbc:postgresql://127.0.0.1:5432/postgres "       property= "url "/>      
<set-property       value= "postgres "       property= "user "/>      
<set-property       value= "postgres "       property= "password "/>      
</data-source>      
</data-sources>
......


Action:
......
import   javax.servlet.ServletContext;
......
ServletContext   context   =   servlet.getServletContext();
DataSource   dataSource   =   (DataSource)   context.getAttribute( "PostgresqlDB ");
......

还有一种是:

struts-config.xml:

......
<data-sources>
<data-source>
<set-property   property= "description "   value= "Example   Data   Source   Configuration "/>
<set-property   property= "driverClass "   value= "org.postgresql.Driver "/>
<set-property   property= "maxCount "   value= "4 "/>
<set-property   property= "minCount "   value= "2 "/>
<set-property   property= "user "   value= "postgres "/>
<set-property   property= "password "   value= "postgres "/>
<set-property   property= "url "   value= "jdbc:postgresql://localhost/postgres "/>
</data-source>
</data-sources>    
......

Action:
......
DataSource   dataSource   =   this.getDataSource(request);
......

不过都连不上DB,得到的dataSource是null.请问问题出在哪里呢?谢谢。

------解决方案--------------------
有人知道吗……