07-Struts1.3 数据库连接池技术
一、Tomcat数据库连接池技术
首先在Eclipse中的server的Tomcat v6.0 Server at localhost-config/context.xml下配置:
<!-- <Resource name="jdbc/mysql" //数据源名字 type="javax.sql.DataSource" //数据源类 username="root" password="root" driverClassName="com.mysql.jdbc.Driver" maxIdle="2" //指定数据库连接池中处于空闲状态的数据库连接的最大数目 maxWait="50" //指定数据库连接池中数据库连接处于空闲状态的最大时间,设置为-1时,可以表现为无限制等待 url="jdbc:mysql://localhost:3306/db_dbsource" maxActive="4" //指定数据库连接池中处于激活状态的数据库连接的最大数目 />
再将数据库驱动包加入到tomcat的lib文件夹下
然后程序获取数据库池的接口方式如下:即编写UserPersistent类:
package org.lxh.persistent; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingException; import javax.sql.DataSource; /** * @author xudongwang 2011-5-3 * */ //这里是针对tomcat中配置的数据源 public class UserPersistent { private DataSource dataSource; public UserPersistent() throws NamingException{ Context ctx=null; ctx = new InitialContext(); // Context数据源对象与名字的绑定的集合(通过数据源名字找到数据源对象) // JNDI // DataSource data=text.lookup("java:comp/env/oracle"); // (java:comp/env)是java虚拟机,通过java虚拟机获取数据源的名字【重要】 通过text.lookup("java:comp/env/oracle")找到数据源 dataSource = (DataSource) ctx.lookup("java:comp/env/jdbc/mysql"); } public boolean addUser(String name, String password) { Connection conn = null; String sql = "insert into t_user(name,password) values(?,?)"; try { conn = this.dataSource.getConnection(); PreparedStatement ps = conn.prepareStatement(sql); ps.setString(1, name); ps.setString(2, password); int result = ps.executeUpdate(); if(result>0){ return true; } } catch (Exception e) { e.printStackTrace(); } return false; } }
最后在UserAction.java中;
package org.lxh.action; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; import org.apache.struts.action.DynaActionForm; import org.apache.struts.actions.DispatchAction; import org.lxh.persistent.UserPersistent; /** * @author xudongwang 2011-5-3 * */ public class UserAction extends DispatchAction { public ActionForward add(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { DynaActionForm dynaActionForm = (DynaActionForm) form; String username = (String) dynaActionForm.get("username"); String password = (String) dynaActionForm.get("password"); UserPersistent userPersistent = new UserPersistent(); if (userPersistent.addUser(username, password)) { return mapping.findForward("success"); } else { return mapping.findForward("error"); } } }
二、Struts1.2数据库连接池技术
注意:关于Struts的数据源配置,只能在Struts1.2及其以下版本使用,Struts1.3不支持数据源配置
Struts1.2框架下配置数据库连接池:
在struts-config.xml配置文件中配置;
<data-sources> <data-source key="mysql" type="org.apache.commons.dbcp.BasicDataSource"> <set-property property="driverClassName" value="com.mysql.jdbc.Driver" /> <set-property property="autoCommit" value="true" /> <set-property value="root" property="username" /> <set-property value="root" property="password" /> <set-property value="jdbc:mysql://localhost:3306/db_dbsource" property="url" /> </data-source> </data-sources>
对Struts数据源配置的说明:
dirverClassName:所用JDBC的驱动类
url:所用的JDBC的url
MaxCount:同时打开的最大连接数,缺省值为2
MinCount:同时打开的最小连接数,缺省值为1
AutoCommit:如果为true,则每次execute之后会强制回滚
LoginTimeout:创建连接的最大允许时间,以秒为单位
ReadOnly:默认为false,表示该连接不是只读
如果有多个数据源,则可以在配置文件(data-sources)下配置多个data-source,不同的数据源通过指定不同的key来区分;
最后直接在UserAction.java中:
package org.lxh.action; import java.sql.Connection; import java.sql.PreparedStatement; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.sql.DataSource; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; import org.apache.struts.action.DynaActionForm; import org.apache.struts.actions.DispatchAction; import org.lxh.persistent.UserPersistent; /** * @author xudongwang 2011-5-3 * */ public class UserAction extends DispatchAction { /** * 这里的方法是针对struts中配置数据源的方法,而上面的是针对tomcat中的 */ public ActionForward add(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { DataSource datasource = getDataSource(request, "mysql");//通过key来取得不同的数据源 DynaActionForm dynaForm = (DynaActionForm) form; String username = (String) dynaForm.get("username"); String password = (String) dynaForm.get("password"); Connection conn = null; try { conn = datasource.getConnection(); String sql = "insert into t_user(name,password) values (?,?)"; PreparedStatement pst = conn.prepareStatement(sql); pst.setString(1, username); pst.setString(2, password); int result = pst.executeUpdate(); System.out.println(sql); if (result > 0) {// 如果结果集大于0说明添加成功,返回为true return mapping.findForward("success"); } try { conn = datasource.getConnection(); } catch (Exception e) { e.printStackTrace(); } pst.close(); conn.close(); } finally { } return mapping.findForward("error"); } }
相关推荐
yangkang 2020-11-09
lbyd0 2020-11-17
sushuanglei 2020-11-12
85477104 2020-11-17
KANSYOUKYOU 2020-11-16
wushengyong 2020-10-28
lizhengjava 2020-11-13
星月情缘 2020-11-13
huangxiaoyun00 2020-11-13
luyong0 2020-11-08
腾讯soso团队 2020-11-06
Apsaravod 2020-11-05
PeterChangyb 2020-11-05
gaobudong 2020-11-04
wwwjun 2020-11-02
gyunwh 2020-11-02
EchoYY 2020-10-31
dingyahui 2020-10-30