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

📄 basedao.java

📁 一个javaweb开的小例子
💻 JAVA
字号:
package cn.com.pkusoft.entity.dao;

import cn.com.pkusoft.entity.BaseEntityData;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.SQLException;
import java.util.List;

/**
 * 一个表对应一个DAO类,用来进行表的操作,表的一条记录由对应的ENTITY对象表示。
 *   主要有一些方法,对数据库中的表进行操作。
 *
 *   1、INSERT  把对象中数据插入到对应的表中
 *   2、UPDATE  把对象中的数据在对应的表中更新
 *   3、DELETE  把对象中的数据在对应的表中删除
 *   4、SELECT  根据查询条件取得相对应的记录
 *
 * <p>Title: BASEDAO</p>
 * <p>Description: 所有的DAO类的基类</p>
 * <p>Copyright: Copyright (c) 2004</p>
 * <p>Company: pku-soft</p>
 * @author weiming
 * @version 1.0
 */
public abstract class BaseDao
{
    /**
     * 使用的连接,通常在事务处理的时候需要使用
     */
    private Connection m_conn = null;

    /**
     * 是否这个对象的操作在一个事务当中,在特殊情况下需要对事务做出处理的
     * 默认为 false: 不在事务中
     *        true: 在一个事务中。
     */
    private boolean bInTransact = false;

    public BaseDao()
    {
    }

    /**
     * 把对象中数据插入到对应的表中,使用成员变量m_conn来指定连接
     * @throws SQLException
     * @return int 0: SUCCESS  -1: FAILER
     */
    public abstract int insert(BaseEntityData bd) throws SQLException;

    /**
     * 把对象中数据插入到对应的表中,使用conn来指定连接
     * @param conn Connection  指定连接
     * @throws SQLException
     * @return int 0: SUCCESS  -1 FAILER
     */
    public int insert(BaseEntityData bd, Connection conn) throws SQLException
    {
        this.setConn(conn);

        return insert(bd);
    }

    /**
     * 把对象中数据在对应的表中更新,使用成员变量m_conn来指定连接
     * @throws SQLException
     * @return int 0: SUCCESS  -1: FAILER
     */
    public abstract int update(BaseEntityData bd) throws SQLException;

    /**
     * 把对象中数据在对应的表中更新,使用conn来指定连接
     * @param conn Connection 指定连接
     * @throws SQLException
     * @return int
     */
    public int update(BaseEntityData bd , Connection conn) throws SQLException
    {
        this.setConn(conn);

        return update(bd);
    }

    /**
     * 把对象中数据在对应的表中删除,使用成员变量m_conn来指定连接
     * @throws SQLException
     * @return int 0: SUCCESS  -1: FAILER
     */
    public abstract int delete(BaseEntityData bd) throws SQLException;

    /**
     * 把对象中数据在对应的表中删除,使用conn来指定连接
     * @param conn Connection 指定连接
     * @throws SQLException
     * @return int 0: SUCCESS  -1: FAILER
     */
    public int delete(BaseEntityData bd, Connection conn) throws SQLException
    {
        this.setConn(conn);

        return delete(bd);
    }

    /**
     * 根据检索条件strFilter来检索出符合条件的记录出来,使用成员变量m_conn来指定连接
     *
     * @param strFilter String 查询的条件
     * @throws SQLException
     * @return List 返回的记录集
     */
    public abstract List select(String strFilter) throws SQLException;

    /**
     * 根据检索条件strFilter来检索出符合条件的记录出来,使用conn来指定连接
     * @param conn Connection 指定连接
     * @param strFilter String 查询的条件
     * @throws SQLException
     * @return List 返回的记录集
     */
    public List select(Connection conn, String strFilter) throws SQLException
    {
        this.setConn(conn);

        return this.select(strFilter);
    }

    /**
     * 取得表对应的SEQENCE的CURRENT VALUE值。
     * @throws SQLException
     * @return String
     */
    public String getSeqCurrValue() throws SQLException
    {
        //1、取得表的SEQUENCE名称。
        String strSequenceName = this.getSequenceName();
        //2、构造出SQL语句。
        String strSql = "select " + strSequenceName + ".currval from dual";
        //3、执行SQL语句,得到查询结果。
        ResultSet rs  = this.executeQuery(strSql);
        //4、取出当前的CURRENT VALUE。
        String strNumber = rs.getString(1);
        //5、返回
        return strNumber;
    }

    /**
     * 取得表对应的SEQENCE的CURRENT VALUE值。
     * @param conn Connection 数据库连接
     * @throws SQLException
     * @return String
     */
    public String getSeqCurrValue(Connection conn) throws SQLException
    {
        //1、保存数据库连接。
        this.setConn(conn);

        //2、调用getSeqCurrValue()方法得到结果然后返回。
        return this.getSeqCurrValue();
    }

    /**
     * 取得表对应的SEQENCE的NEXT VALUE值。
     * @throws SQLException
     * @return String
     */
    public String getSeqNextValue() throws SQLException
    {
        //1、取得表的SEQUENCE名称。
        String strSequenceName = this.getSequenceName();
        //2、构造出SQL语句。
        String strSql = "select " + strSequenceName + ".nextval from dual";
        //3、执行SQL语句,得到查询结果。
        ResultSet rs  = this.executeQuery(strSql);
        //4、取出当前的CURRENT VALUE。
        String strNumber = rs.getString(1);
        //5、返回
        return strNumber;

    }

    /**
     * 取得表对应的SEQENCE的NEXT VALUE值。
     * @param conn Connection 数据库连接
     * @throws SQLException
     * @return String
     */
    public String getSeqNextValue(Connection conn) throws SQLException
    {
        this.setConn(conn);
        return this.getSeqNextValue();
    }

    /**
     * 取得DAO对应的TABLE 的相关的SEQUENCE名字。
     * @throws SQLException
     * @return String
     */
    protected abstract String getSequenceName() throws SQLException;

    /**
     * 设置一个连接
     * @param conn Connection
     */
    public void setConn(Connection conn)
    {
        this.m_conn = conn;
    }

    /**
     * 取得使用的连接
     * @return Connection
     */
    public Connection getConn()
    {
        return m_conn;
    }

    /**
     * 设置事务标志
     * @param bInTransact boolean
     */
    public void setBInTransact(boolean bInTransact)
    {
        this.bInTransact = bInTransact;
    }

    /**
     * 判断是否在事务中
     * @return boolean
     */
    public boolean isBInTransact()
    {
        return bInTransact;
    }

    /**
     * 查询数据库,执行数据库查询语句
     * @param strSql String
     * @throws SQLException
     * @return ResultSet 查询出来的结果
     */
    protected ResultSet executeQuery(String strSql) throws SQLException
    {
        ResultSet rs = null;
        try
        {
            Statement stat = m_conn.createStatement();
			System.out.println(stat);
			System.out.println(m_conn);
            rs = stat.executeQuery(strSql);
        }
        catch (Exception ex)
        {
            throw new SQLException(ex.getMessage());
        }

        return rs;
    }

    /**
     * 执行一条数据库语句的操作。
     *
     * @param strSql String
     * @throws SQLException
     * @return int  执行的结果, -1是异常
     */
    protected int execute(String strSql) throws SQLException
    {
        int nReturn = -1;

        try
        {
            Statement stat = m_conn.createStatement();

            nReturn = stat.executeUpdate(strSql);
        }
        catch (SQLException ex)
        {
            throw new SQLException(ex.getMessage());
        }

        return nReturn ;
    }

}

⌨️ 快捷键说明

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