Home > Database > Mysql Tutorial > 如何调用struts-config.xml中配置的data-source

如何调用struts-config.xml中配置的data-source

WBOY
Release: 2016-06-07 17:03:16
Original
1129 people have browsed it

在struts-config.xml中配置data-source注意事项:lt;data-sourcesgt;lt;!--名称:data-source描述:data-source元素定义了具体

在struts-config.xml中配置data-source注意事项:

  
  
   
        value="Oracle.jdbc.driver.OracleDriver" />
       value="jdbc:oracle:thin:@localhost:1521:orcl" />
   
   
   
   
   
   
   
  

 

在**DAO文件中代码:
首先定义
private DataSource dataSource;
该类的构造方法:
public **DAO(DataSource dataSource)
{
this.dataSource=dataSource;
}
然后在该类的方法中使用dataSource.getConnection()方法获得连接。
一定要注意千万不要使用静态方法


在action类中调用配置的datasource时有一下两种方法:
方法一:ServletContext context = servlet.getServletContext();//获得Struts中的配置文件属性
        DataSource dataSource = (DataSource) context.getAttribute("dataSource");

方法二:DataSource ds=this.getDataSource(request, "dataSource");
**DAO **dao=new **DAO(ds);

getDataSource(HttpServletRequest request,String key)方法就是在action中调用你配置好的data-source而已

当然,方法中的参数key,就是你配置的的key

可以比较方法二比较简洁哦!费了好大功夫才搞出来的,呼呼·····

linux

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template