在struts中以无参数的javabeans的方式调用struts-config.xml中配置的数据源

 我是从JSP转来学STRUTS的,一开始的时候有很多地方不习惯。

比如对数据库的操作,在JSP中,一般是写一个javabeans来封装对数据库的连接与操作,如:


Conn.java


public class Conn {

private Connection conn = null;

private Statement stmt = null;

private ResultSet rs = null;

private String dataSource = "java:comp/env/jdbc/CpDB";


public Conn() {

try {

Context ctx = new InitialContext();

DataSource ds = (DataSource) ctx.lookup(dataSource);

conn = ds.getConnection();

} catch (Exception e) {

System.err.println(e.getMessage());

}

}


public Statement createStatement() throws Exception {

stmt = conn.createStatement();

return stmt;

}


public ResultSet executeQuery(String sql) throws Exception {

createStatement();

return stmt.executeQuery(sql);

}


public int executeUpdate(String sql) throws Exception {

createStatement();

return stmt.executeUpdate(sql);

}


public void close() {

}

}


然后在JSP页面中用这样的代码:


Conn myConn = new Conn();

String sqlStr = “....”;

ResultSet rs = myConn.exeuteQuery(sqlStr);

.....



但是在STRUTS中,假设我们在struts-config.xml中配置了数据源,如:


<data-sources >

<data-source key="org.apache.struts.action.DATA_SOURCE" type="org.apache.commons.dbcp.BasicDataSource">

<set-property property="password" value="nowind" />

<set-property property="minCount" value="2" />

<set-property property="maxCount" value="10" />

<set-property property="user" value="nowind" />

<set-property property="driverClass" value="com.microsoft.jdbc.sqlserver.SQLServerDriver" />

<set-property property="description" value="microsoft sql server" />

<set-property property="url" value="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=CpDB" />

<set-property property="readOnly" value="false" />

<set-property property="autoCommit" value="false" />

<set-property property="loginTimeout" value="" />

</data-source>

</data-sources>


我们要访问这个数据源,必须要有request对象才行。


假设我们在action中访问数据库,则:


public ActionForward execute(.....) {

try {

DataSource = getDataSource(request,"org.apache.struts.action.DATA_SOURCE");

Connection myConnection = dataSource.getConnection();

Statement myStmt = myConnection.createStatement();

String sqlStr = "................";

ResultSet rs = myStmt.executeQuery(sqlStr);

.....

}


我觉得这样,一是要写多写很多代码,二是在没有request的地方就不好操作数据库了(当然可以把request当作参数传过去,但是还是有一点麻烦)


所以我写了一个strutspluginConn.java


可以在系统启动的时候,找到数据源,实现与以前在JSP下调用JavaBeans相同的结果。


package com.strutsLogin2.util;


import java.sql.Connection;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Statement;

import java.util.Enumeration;


import javax.sql.DataSource;


import org.apache.struts.action.ActionServlet;

import org.apache.struts.action.PlugIn;

import org.apache.struts.config.ModuleConfig;


public class Conn implements PlugIn {


private static DataSource dataSource = null;


private Connection conn = null;


private PreparedStatement preStmt = null;


private Statement stmt = null;


// 得到数据源

public void init(ActionServlet servlet, ModuleConfig config) {

dataSource = (DataSource) servlet.getServletContext().getAttribute(

"org.apache.struts.action.DATA_SOURCE");

}


public Conn() throws SQLException {

if (dataSource != null)

conn = dataSource.getConnection();

}


public ResultSet executeQuery(String sql) {

ResultSet rs = null;

try {

if (stmt == null) {

stmt = conn.createStatement();

}

rs = stmt.executeQuery(sql);

} catch (SQLException e) {

e.printStackTrace();

}

return rs;

}


public void executeUpdate(String sql) throws SQLException {

if (stmt == null) {

stmt = conn.createStatement();

}

stmt.executeUpdate(sql);

}


public Connection getConn() {

return conn;

}


public void prepareStatement(String sqlStr) throws SQLException {

preStmt = conn.prepareStatement(sqlStr);

}


public void setString(int index, String value) throws SQLException {

preStmt.setString(index, value);

}


public void setInt(int index, int value) throws SQLException {

preStmt.setInt(index, value);

}


public void setBoolean(int index, boolean value) throws SQLException {

preStmt.setBoolean(index, value);

}


public void setLong(int index, long value) throws SQLException {

preStmt.setLong(index, value);

}


public void setFloat(int index, float value) throws SQLException {

preStmt.setFloat(index, value);

}


public void setBytes(int index, byte[] value) throws SQLException {

preStmt.setBytes(index, value);

}


public void clearPreStmt() throws SQLException {

preStmt.clearParameters();

preStmt = null;

}


public ResultSet executeQuery() throws SQLException {

if (preStmt != null) {

return preStmt.executeQuery();

} else

return null;

}


public void executeUpdate() throws SQLException {

if (preStmt != null)

preStmt.executeUpdate();

}


public void close() {

try {

if (stmt != null) {

stmt.close();

stmt = null;

}

if (preStmt != null) {

preStmt.close();

preStmt = null;

}

if (conn != null) {

conn.close();

conn = null;

System.out.println("***************** a connection is closed");

}

} catch (Exception e) {

System.err.println(e.getMessage());

}

}


public void destroy() { }

}


其实很简单,关键就在于


dataSource = (DataSource) servlet.getServletContext().getAttribute(

"org.apache.struts.action.DATA_SOURCE");


我们原来也可以从servlet中得到这个数据源,并不只是可以从request中得到。


我们可以用下面的代码看到:

Enumeration en = servlet.getServletContext().getAttributeNames();

while (en.hasMoreElements()) {

System.out.println(en.nextElement().toString());

}


它的显示结果里肯定有

org.apache.struts.action.DATA_SOURCE

这一行。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值