⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sqlcommandbean.java

📁 eclipse java/jsp 航空管理系统
💻 JAVA
字号:
/** 
 * @(#)SQLCommandBean.java 2.2 2005/04/26  
 * <p>copyright:    Copyright 东软 国际合作事业部版权所有</p>
 * <p>company:      neusoft</p>
 * <p>time:         2005.04.26</p>
 */

package qujl.bean;

import java.io.Serializable;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;
import javax.servlet.jsp.jstl.sql.Result;
import javax.servlet.jsp.jstl.sql.ResultSupport;

import zhangchunliang.model.AppMode;

/**
 * 执行SQL语句的Bean.它包括3个属性connection, sqlValue, values.
 * connection和sqlValue属性必须在调用execute方法之前设置. 如果设置了values属性,
 * sqlValue属性必须是包含以问号做为占位符语句, 来对应values属性中每一个value对象.
 * 
 * @author 曲金龙 qujl@neusoft.com
 * @version 2.2 2005/04/26
 */
class SQLCommandBean implements Serializable {
    /** 保存要使用的Connection */
    private Connection conn;

    /** 要执行的SQL语句 */
    private String sqlValue;

    /** 对应占符为位的值 */
    private List values;

    /**
     * 设置要使用的Connection.
     */
    protected void setConnection(Connection conn) {
        this.conn = conn;
    }

    /**
     * 设置SQL语句, 可以使用?做为占符位对应values的每个对象
     */
    protected void setSqlValue(String sqlValue) {
        this.sqlValue = sqlValue;
    }

    /**
     * 设置占符位对应的值
     */
    protected void setValues(List values) {
        this.values = values;
    }
    
    /** SQLCommandBean的构造方法 */
    protected SQLCommandBean() {
    }
    
    /**
     * 执行executeQuery()方法,返回一个ResultSet对象.
     * 
     * @return ResultSet
     * @exception SQLException
     */
    protected Result executeQuery() throws SQLException {
    	
    	AppMode.registerUser();
    	
        Result result = null;
        ResultSet rs = null;
        PreparedStatement pstmt = null;
        Statement stmt = null;
        try {
            if (values != null && values.size() > 0) {
                /** 执行带占符位的语句 */
                pstmt = conn.prepareStatement(sqlValue);
                setValues(pstmt, values);
                rs = pstmt.executeQuery();
            } else {
                /** 执行普通语句 */
                stmt = conn.createStatement();
                rs = stmt.executeQuery(sqlValue);
            }
            result = ResultSupport.toResult(rs);
        }

        /** 释放资源 */
        finally {
            if (rs != null) {
                try {
                    rs.close();
                } catch (SQLException e) {
                }
            }
            if (stmt != null) {
                try {
                    stmt.close();
                } catch (SQLException e) {
                }
            }
            if (pstmt != null) {
                try {
                    pstmt.close();
                } catch (SQLException e) {
                }
            }
            
            AppMode.loginoutUser();
            
            
            
        }
        return result;
    }

    /**
     * 执行execute方法(除了SELECT语句, 可以是任何SQL语句) 返回执行后影响的行数
     * 
     * @return 受到影响的行数
     * @exception SQLException
     */
    protected int executeUpdate() throws SQLException {
    	
		AppMode.registerUser();
    	
        int noOfRows = 0;
        ResultSet rs = null;
        PreparedStatement pstmt = null;
        Statement stmt = null;
        try {
            if (values != null && values.size() > 0) {
                /** 执行带占符位的语句 */
                pstmt = conn.prepareStatement(sqlValue);
                setValues(pstmt, values);
                noOfRows = pstmt.executeUpdate();
            } else {
                /** 执行普通语句 */
                stmt = conn.createStatement();
                noOfRows = stmt.executeUpdate(sqlValue);
            }
        }

        /** 释放资源 */
        finally {
            if (rs != null) {
                try {
                    rs.close();
                } catch (SQLException e) {
                }
            }
            if (stmt != null) {
                try {
                    stmt.close();
                } catch (SQLException e) {
                }
            }
            if (pstmt != null) {
                try {
                    pstmt.close();
                } catch (SQLException e) {
                }
            }
            
            AppMode.loginoutUser();
            
        }
        return noOfRows;
    }

    /**
     * 调用setObject()方法给PreparedStatement的所有对象赋值.
     * 
     * @param pstmt PreparedStatement
     * @param values 一组对象
     * @exception SQLException
     */
    private void setValues(PreparedStatement pstmt, List values)
            throws SQLException {
        for (int i = 0; i < values.size(); i++) {
            Object v = values.get(i);

            /** 使用对应的方法设置值 */
            /** 注意! 设置方法从1开始, 因此对i加1 */
            pstmt.setObject(i + 1, v);
        }
    }

    /** 调用releaseConn()方法释放Connection资源 */
    protected void releaseConn() throws SQLException {
        this.conn.close();
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -