首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > 其他数据库 >

[总结]Java数据库连接 - 配置 Struts 数据源

2012-11-07 
[小结]Java数据库连接 - 配置 Struts 数据源?7.配置 Struts 数据源?一般步骤:7.1配置JNDI数据源在struts-c

[小结]Java数据库连接 - 配置 Struts 数据源

?

7.配置 Struts 数据源

?

一般步骤:

7.1配置JNDI数据源

在struts-config.xml配置文件<struts-config>标签下加入类似如下配置内容

<data-source>属性type用来定义数据源javax.sql.DataSource实现类:org.apache.commons.dbcp.BasicDataSource

<set-property>标签各属性设置可参考实现类:org.apache.commons.dbcp.BasicDataSource中各数据库连接相关属性的定义.<set-property>的作用就是根据实现类中的setXXX()方法,设置其内容.这就好像<jsp:setProperty>用来设置bean属性一样.

??? <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/mydb"/>

?????????? <set-property property="username" value="root"/>

?????????? <set-property property="password" value="root"/>

?????????? <set-property property="maxActive" value="30"/>

?????????? <set-property property="maxIdle" value="10"/>

?????????? <set-property property="maxWait" value="1000"/>

?????? </data-source>

??? </data-sources>

?

7.2获取数据源实例

通过org.apache.struts.action.Action类中定义的二个方法:

protected DataSource getDataSource(HttpServletRequest request, String key)

protected DataSource getDataSource(HttpServletRequest request)

?

关键代码:

??? DataSource ds = this.getDataSource(request);?

?

查看org.apache.struts.action.Action类源代码:

??? /**

???? * <p>Return the specified data source for the current module.</p>

???? *

???? * @param request The servlet request we are processing

???? * @param key???? The key specified in the <code>&lt;data-sources&gt;</code>

???? *??????????????? element.

???? *

???? * @since Struts 1.1

???? */

??? protected DataSource getDataSource(HttpServletRequest request, Stringkey) {

?

??????? // Identify the current module

??????? ServletContext context =getServlet().getServletContext();

??????? ModuleConfig moduleConfig =

??????????? ModuleUtils.getInstance().getModuleConfig(request,context);

?

??????? return (DataSource)context.getAttribute(key + moduleConfig.getPrefix());

??? }

?

??? protected DataSourcegetDataSource(HttpServletRequest request) {

?

??????? return (getDataSource(request,Globals.DATA_SOURCE_KEY));

?

??? }

?

由此可知,数据源需要在自定义的Action处理类(继承自Action)中获取数据源.

参数说明:

request??? 一次用户请求

key??? <data-source>标签设置的key属性.未设置key属性时,默认值为Globals.DATA_SOURCE_KEY

?????? 通过protected DataSource getDataSource(HttpServletRequest request)方法获取数据源

?????? 默认调用key值为Globals.DATA_SOURCE_KEY的数据源

?

引用自struts-config_1_2.dtd

???? key???????????? Servlet context attributekey under which this data source

???????????????????? will be stored.? Default is the value specified by string

???????????????????? constant defined byGlobals.DATA_SOURCE_KEY. The application

???????????????????? module prefix (if any) isappended to the key

???????????????????? (${key}$prefix}).

????????????????????[org.apache.struts.Globals.DATA_SOURCE_KEY]

?

注: 该方法权限是protected,因为可以在不同包的子类中使用,无法在不同包非子类中被调用.另外,自定义的Action处理类构造函数只在第一次请求时,被调用.因此,自定义的Action处理类的实例化很可能用到单例模式.

?

7.3数据库操作

?

一般步骤:

a) 通过数据源实例建立数据库连接

b) 创建Statement对象

c) 获取结果集

d) 关闭数据库连接资源(包括ResultSet,Statement,Connection实例)

?

关键代码:

??? Connection conn = ds.getConnection();

??? PreparedStatement stmt = conn.prepareStatement(sql);

?

??? stmt.setString(1, username);

??? stmt.setString(2, password);

??? ResultSet rs = stmt.executeQuery();

?

??? if(rs.next())

??? {

?????? request.setAttribute("hint", bundle.getString("login.check.hint.success"));

?????? return mapping.findForward("success");

??? }

?

热点排行