Struts DataSource

2008-02-23 10:14:37来源:互联网 阅读 ()

新老客户大回馈,云服务器低至5折

1 、在Struts-config.XML下

<data-sources >
<data-source key="DataSource">
<set-property property="minCount" value="2" />
<set-property property="password" value="spring" />
<set-property property="maxCount" value="10" />
<set-property property="user" value="spring" />
<set-property property="driverClass" value="Oracle.JDBC.driver.OracleDriver" />
<set-property property="description" value="datasource" />
<set-property property="url" value="jdbc:oracle:thin:@it10:1521:SP" />
<set-property property="autoCommit" value="false" />
<set-property property="readOnly" value="false" />
<set-property property="loginTimeout" value="" />
</data-source>

2、在Action下
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) throws Exception {

DataSource dataSource=this.getDataSource(request,Constants.DATASOURCE_KEY);
Connection connection=dataSource.getConnection(); //取得Connection

HelPBean helpBean=new HelpBean();
Vector infovv=helpBean.getItemInfos(connection);

}

3、Constants.Java

package com.tek.david.dataBase.pool;

/**
* @author qianzh
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class Constants {

public static String DATASOURCE_KEY="DataSource";

}

4、建立HelpBean

加入方法

public Vector getItemInfos(Connection conn) throws Exception{
Vector ret=new Vector();
Statement stmt=conn.createStatement();
String sql="select * from test";
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()){
ItemBean itemBean=new ItemBean();
InfoBean infoBean=new InfoBean();
infoBean.setUser_id(rs.getString(1));
infoBean.setUsername(rs.getString(2));
infoBean.setPassword(rs.getString(3));
infoBean.setNeedqty(rs.getString(4));
itemBean.setInfoBean(infoBean);
itemBean.setIsqty("");
ret.add(itemBean);

}
rs.close();
stmt.close();
conn.close();

return ret;

}

上一篇: Java的建造设计模式
下一篇: FIFO与LRU 算法实现(java)

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:Spring--超简单利用quartz实现定时作业

下一篇:简析Java开发中的XML编程