




已阅读5页,还剩1页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
在struts中以无参数的javabeans的方式调用struts-config.xml中我是从JSP转来学STRUTS的,一开始的时候有很多地方不习惯。比如对数据库的操作,在JSP中,一般是写一个javabeans来封装对数据库的连接与操作,如:Conn.javapublic 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中配置了数据源,如: 我们要访问这个数据源,必须要有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当作参数传过去,但是还是有一点麻烦)。所以我写了一个struts的plugin类Conn.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(); elsereturn 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中得到这个数据源,并不只是可以从requ
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年人机交流考试题及答案
- 五金电器家电购置合同3篇
- 小区沥青路面摊铺合同3篇
- DB15-T 3320-2024 电解原铝液连铸连轧电工圆铝杆工艺规范
- XX公司与XX公司承揽合同纠纷6篇
- 2025年美妆课程考试题及答案
- 民航专业面试题及答案
- 2025年特种作业考试-金属非金属矿山考试历年参考题库含答案解析
- 学校传染病防控制度和措施
- 福州网约车考试报名流程和题库及答案
- 中职高教版(2023)语文职业模块-第一单元1.2宁夏闽宁镇:昔日干沙滩今日金沙滩【课件】
- 高考数学压轴题专项训练:集合、常用逻辑用语、不等式(新定义高数观点压轴题)含答案及解析
- 呼吸道合胞病毒护理查房
- 铭记历史缅怀先烈-珍爱和平开创未来
- 2023-2024学年广东省深圳市红岭中学高一上学期第一次段考化学试题及答案
- 项目施工单位与当地政府及村民的协调措施
- 2024澳大利亚技术移民与留学服务协议
- 名著阅读《童年》导读课课件
- 《无人机测绘技术(微课版)》全套教学课件
- 2024年四川省成都市中考作文“赢”与“迎”写作指导
- GB/T 21220-2024软磁金属材料
评论
0/150
提交评论