struts1.3.10项目中mysql数据源配置

来源:互联网 发布:电机控制算法的书籍 编辑:程序博客网 时间:2024/04/29 17:28
在Struts1.3中已经取消了<data-sources>标签,Action中也没有getDataSource(),因此不能够在struts-config.xml中配置数据源。如果使用了hibernate或者spring可以再hibernate或者spring的配置文件中配置数据源。本文是没有使用hibernate和spring情况下的配置方法。
1.在Java web项目的META-INF目录下新建context.xml文件,在其中添加定义JNDI资源的代码:
<Context reloadable="true" >     <Resource          name="jdbc/mysql"         auth="Container"          type="javax.sql.DataSource"         maxActive="100" maxIdle="30" maxWait="10000"         username="root" password="root"          driverClassName="com.mysql.jdbc.Driver"         url="jdbc:mysql://localhost:3306/databasename;     /> </Context> 

name:指定resource资源的JNDI名称;auth:指定resource的管理者,取值为Container或Application;type:resource资源的Java类名;username:指定数据库的用户名;password:指定数据库的密码;driverClassName:指定数据端口的JDBC驱动器的Driver实现类的名称;url:连接数据库的url。(其中databasename即为要连接的数据库的名称)
2.在web.xml中配置对该JNDI资源的引用,在<web-app></web-app>标签中添加如下内容:
<resource-ref>         <description>DB Connection</description>         <res-ref-name>jdbc/mysql </res-ref-name>         <res-type>javax.sql.DataSource</res-type>         <res-auth>Container</res-auth> </resource-ref> 
3.在web应用里使用数据源
Context ct=new InitialContext();DataSource ds=(DataSource) ct.lookup("java:comp/env/jdbc/mysql");Connection conn=ds.getConnection();

0 0
原创粉丝点击