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

📄 dboperator.java

📁 基于Java的地图数据管理软件。使用MySQL数据库管理系统。
💻 JAVA
字号:
package com.tongtu.comm.sql;

/**
 * Created by IntelliJ IDEA.
 * User: Administrator
 * Date: 2003-8-22
 * Time: 14:39:52
 * To change this template use Options | File Templates.
 */
import java.sql.*;
import java.util.Date;

/**
 * 数据库操作类。<p>
 * 所有的数据库操作都通过此类完成,本类使用PreparedStatement,以提高数据库操作性能和安全性。基本用法如下:
 *<pre>
 *   DBOperator dbo;
 *   final String sql = "SELECT * FROM Tbl_Example WHERE Exam_ID = (?) AND Exam_Name = (?)";
 *   try{
 *       dbo = new DBOperator();//新建对象,完成初始化,如取数据库连接等
 *       dbo.setPrepareStatement(sql);//根据SQL语句设置PreparedStatement
 *       dbo.setInt(1,2);//设置各参数
 *       dbo.setString(2,"a");
 *       final ResultSet rs = dbo.executeQurey();//执行查询
 *       //do something with rs 处理查询结果
 *       rs.close; //关闭结果集合
 *   }catch(SQLException ex){//SQL异常处理
 *   }finally{
 *       dbo.close();//释放资源,归还数据库连接
 *   }//end try...catch...finally...
 *</pre>
 * <p>
 * @author irevin
 * @version 1.0
 */
public class DBOperator {

    /**日志处理对象*/
    //protected static Logger log = Logger.getInstance("DBOperator");
    /**数据库连接对象*/
    public Connection conn = null;
    /**Statement*/
    //protected Statement stmt = null;
    /**PreparedStatement*/
    protected PreparedStatement pstmt = null;
    /**CallableStatement*/
    protected CallableStatement cstmt = null;
    /**数据库连接池管理对象*/
    protected DBConnectionManager dbmanager = null;

    /**
     * 构造方法。
     * <p>
     * 取数据库连接并生产Statement。
     *
     */
    public DBOperator(){
        try{
            dbmanager = DBConnectionManager.getInstance();
            //todo pool name */
            this.conn = dbmanager.getConnection("DB");
            //this.stmt = this.conn.createStatement();
        } catch(Exception e){
            //log.error(e.getMessage());
        }
    }

    /**
     * 构造方法。
     * <p>
     * 取数据库连接,生成Statement,根据sql参数生成PrepareStatement。
     *
     * @param sql
     */

    public DBOperator(String sql){
        try{
            dbmanager = DBConnectionManager.getInstance();
            this.conn = dbmanager.getConnection("DB");
            //this.stmt = this.conn.createStatement();
            this.pstmt = this.conn.prepareStatement(sql);
        } catch(Exception e){
            //log.error(e.getMessage());
        }
    }//end DBOperator()

    /**
     * 设置PrepareStatement,并同时其清空参数列表。
     *
     * @param sql SQL语句
     * @throws SQLException SQL异常
     */
    public void setPrepareStatement(String sql) throws SQLException{
    	 //this.clearParameters();
        this.pstmt = this.conn.prepareStatement(sql);
    }

    /**
     * 设置字符串值。
     *
     * @param index 索引
     * @param value 字符串值
     * @throws SQLException SQL异常
     */
    public void setString(int index, String value) throws SQLException{
        //pstmt.setString(index, value);
        pstmt.setObject(index,value,java.sql.Types.VARCHAR);
    }

    /**
     * 设置整形值。
     *
     * @param index 索引
     * @param value 整形值
     * @throws SQLException SQL异常
     */
    public void setInt(int index, int value) throws SQLException{
        pstmt.setInt(index, value);
    }

    /**
     * 设置布尔值。
     *
     * @param index 索引
     * @param value 布尔值
     * @throws SQLException SQL异常
     */
    public void setBoolean(int index, boolean value) throws SQLException{
        pstmt.setBoolean(index, value);
    }

    /**
     * 设置时间值。
     *
     * @param index 索引
     * @param value 时间值
     * @throws SQLException SQL异常
     */
    public void setDate(int index, Date value) throws SQLException{
        //pstmt.setDate(index,value);
        //todo dateformat and insert... */
        pstmt.setString(index, value.toString());
    }

    /**
     * 设置Long值。
     *
     * @param index 索引
     * @param value Long值
     * @throws SQLException SQL异常
     */
    public void setLong(int index, long value) throws SQLException{
        pstmt.setLong(index, value);
    }

    /**
     * 设置浮点值。
     *
     * @param index 索引
     * @param value 浮点值
     * @throws SQLException SQL异常
     */
    public void setFloat(int index, float value) throws SQLException{
        pstmt.setFloat(index, value);
    }

    /**
     * 清空PrepareStatement中的参数。
     *
     * @throws SQLException SQL异常
     */
    public void clearParameters() throws SQLException{
        if(null != this.pstmt){
            pstmt.clearParameters();
        }
    }

    /**
     * 执行查询。
     * <p>
     * 必须先使用DBOperator(conn,sql)或setPrepareStatement(sql)创建PrepareStatement。
     *
     * @return ResultSet结果集
     * @throws SQLException SQL异常
     */
    public ResultSet executeQuery() throws SQLException{
        if(null != pstmt){
            return pstmt.executeQuery();
        }
        return null;
    }//end executeQuery()

    /**
     * 更新数据库。
     *
     * 必须先使用DBOperator(conn,sql)或setPrepareStatement(sql)创建PrepareStatement。
     *
     * @return 修改的行数
     * @throws SQLException SQL异常
     */
    public int executeUpdate() throws SQLException{
        if(null != pstmt){
            return this.pstmt.executeUpdate();
        }
        return -1;
    }

    /**
     * 关闭数据库操作,释放数据库连接(仅将数据库连接归还连接池)。
     */
    public void close(){
        try{
            /*if(null != stmt){
                stmt.close();
                stmt = null;
            }*/
            if(null != pstmt){
                pstmt.close();
                pstmt = null;
            }
            if(null != cstmt){
                cstmt.close();
                cstmt = null;
            }
        } catch(SQLException e){
            //log.error("DBOperator close() error: " + e.getMessage());
        } finally{
            //try{
                dbmanager.freeConnection(this.conn, "DB");
                //log.debug("free current Connection");
            //} catch(SQLException e){
                //log.error("DBOperator close() error: " + e.getMessage());
            //}//try...freeConnection...
        }//try...catch...finally...
    }//end close()
}

⌨️ 快捷键说明

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