在struts-config.xml中配置数据源

和题目的题意一样。struts-config.xml中配置 那肯定是在struts 中写配置代码了。在写之前我们要用到两个jar包;

分别是commons-dbcp-1.2.1.jar,commons-pool-1.4.jar:

在struts-config.xml中配置如下代码:我在这边用的数据库是mysql的配置

<data-sources>
<data-source type="org.apache.commons.dbcp.BasicDataSource">
    <set-property
      property="driverClassName"
      value="com.mysql.jdbc.Driver" />
    <set-property
      property="url"
      value="jdbc:mysql://localhost:3306/test" />
    <set-property
      property="username"
      value="root" />
    <set-property
      property="password"
      value="root" />
    <set-property
      property="maxActive"
      value="10" />
    <set-property
      property="maxWait"
      value="5000" />
    <set-property
      property="defaultAutoCommit"
      value="false" />
    <set-property
      property="defaultReadOnly"
      value="false" />
</data-source>
</data-sources>

 在上面的代码中红色的字中就是我们以前写的连接属性是一样的。

maxActive :代表最大的连接数

maxWait :最大的等待时间

defaultAutoCommit :自动提交方式

defaultReadOnly :只读方式;

 

有如上的配置:那我的数据库是mysql。我的用户名和密

 码是root

 

配完了那下一步当然是到用这个数据源了哦:

在Action中可通过getDataSource(request)得到这个数据源:如下代码:

 

                               UserManager manager=new UserManager((BasicDataSource) getDataSource(request));
		List<BlackInfo> list=manager.findAll();
		request.setAttribute("list", list);
		return mapping.findForward("success");

 

写dao也是和以前一样了啦;

package com.web.services;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.dbcp.BasicDataSource;

import com.web.model.BlackInfo;

public class UserManager {
	private static final String BALCK_ALL="select * from black_tab";
	private BasicDataSource datasource;
	public UserManager(BasicDataSource datasource){
		try {
			this.datasource = datasource;
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public List<BlackInfo> findAll(){
		List<BlackInfo> list=new ArrayList<BlackInfo>();
		try {
			Connection conn=this.datasource.getConnection();
			PreparedStatement ps=conn.prepareStatement(this.BALCK_ALL);
			ResultSet rs=ps.executeQuery();
			BlackInfo black=new BlackInfo();
			while(rs.next()){
				black.setBlackId(rs.getInt("blackId"));
				black.setBlackIp(rs.getString("blackIp"));
				black.setBlackTel(rs.getString("blackTel"));
				list.add(black);
			}
			conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return list;
	}
}

 

 

写到最还是老样子了。如果不懂还是看看实例吧:

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值