- <data-sources >
- <data-source key="datasource" type="org.apache.commons.dbcp.BasicDataSource">
- <set-property property="password" value="" />
- <set-property property="minCount" value="1" />
- <set-property property="maxCount" value="10" />
- <set-property property="username" value="root" />
- <set-property property="driverClassName" value="org.gjt.mm.mysql.Driver" />
- <set-property property="description" value="for struts attack aolution" />
- <set-property property="url" value="jdbc:mysql://localhost:3306/pip2DB" />
- <set-property property="readOnly" value="false" />
- <set-property property="autoCommit" value="true" />
- </data-source>
- </data-sources>
- package com.yourcompany.struts.action;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import javax.sql.DataSource;
- import org.apache.struts.action.Action;
- import org.apache.struts.action.ActionForm;
- import org.apache.struts.action.ActionForward;
- import org.apache.struts.action.ActionMapping;
- import com.yourcompany.struts.form.SuccessForm;
- import java.sql.*;
- /**
- * MyEclipse Struts
- * Creation date: 04-17-2010
- *
- * XDoclet definition:
- * @struts.action path="/success" name="successForm" input="/form/success.jsp" scope="request" validate="true"
- * @struts.action-forward name="success" path="/index.jsp"
- */
- public class SuccessAction extends Action {
- /*
- * Generated Methods
- */
- /**
- * Method execute
- * @param mapping
- * @param form
- * @param request
- * @param response
- * @return ActionForward
- */
- public ActionForward execute(ActionMapping mapping, ActionForm form,
- HttpServletRequest request, HttpServletResponse response) {
- SuccessForm successForm = (SuccessForm) form;// TODO Auto-generated method stub
- try {
- DataSource ds=getDataSource(request,"datasource");
- Connection conn=ds.getConnection();
- StringBuilder sb=new StringBuilder();
- ResultSet rs;
- String sql="select * from TransitionInfo where TranName=?&& BelongId=?";
- PreparedStatement ps=conn.prepareStatement(sql);
- ps.setString(1, "T2");
- ps.setInt(2, 1);
- rs=ps.executeQuery();
- while(rs.next())
- {
- sb.append("变迁名:"+rs.getString("TranName")+"\n\n");
- sb.append("执行人:"+rs.getString("TranAgent")+"\n\n");
- sb.append("执行时间:"+rs.getInt("TranTime")+"\n\n");
- sb.append("任务内容:"+rs.getString("TranTask")+"\n\n");
- sb.append("详细信息:"+rs.getString("TranMore")+"\n\n");
- }
- request.setAttribute("rs", sb.toString());
- return mapping.findForward("success");
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- return null;
- }
- }
- }
直接在struts-config.xml中右键data-sources 然后New。 默认的type是: org.apache.struts.util.GenericDataSource ,struts自带的一个数据库连接池。 然后正确输入各种property的value。 测试,好用。
如果将type选择为: org.apache.commons.dbcp.BasicDataSource 这个DBCP的数据库连接池。 然后键入正确的value。 测试,提示各种异常找不到Driver。
查看DBCP的文档,发现MYECLIPSE生成的配置XML元素有问题。
DBCP需要的是driverClassName和username这两个properties。
而Myeclipse自动生成的是driverClass和user这两个properties。
改过来之后发现DBCP的数据库连接池也好用了,这应该算是MYECLIPSE开发struts的一个BUG吧,数据库连接池的配置是按照struts默认的GenericDataSource来的。