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

📄 sqlclt.java

📁 好像是一个SQL执行器的设计实现
💻 JAVA
字号:
/**
 文件名:SqlClt.java
 功能:sql调用的函数库
 编写:lhg
 日期:2002.3.15
 说明:
 */
package mytools.sqlclient;

//import java.util.*;
//import javax.naming.*;
//import java.lang.reflect.*;
import com.borland.dx.dataset.*;

import mytools.sqlbean.*;
import mytools.util.*;

/**
 * <p>Title: sql调用的函数库</p>
 * <p>Description: sql调用的函数库</p>
 * <p>Copyright: Copyright (c) 2001</p>
 * <p>Company: jhtop</p>
 * @author lhg
 * @version 1.0
 */

public class SqlClt
{
  private static ConnectManager conMng = null; //连接管理器

  private SqlClt()
  {
  }

  /**
   功能:取得数据库连接对象
   @param 无
   @return 成功 数据库连接对象 失败 null
   */
  private static IConnection getConnection()
  {
    conMng = ConnectManager.getInstance();
    return conMng.getConnection();
  }

  /**
  功能:更新连接的状态
  @param pb_isSelectSql select语句标志
  @param ps_sql sql语句标志
  @return 无
   */
  private static void updateConnectInfo(boolean pb_isSelectSql, String ps_sql)
  {
    if (pb_isSelectSql)
    {
      if (ps_sql.indexOf("for update") >= 0)
      {
        conMng.setInTransaction(); //启动事务
      }
      else
      {
        conMng.releaseConnection();
      }
    }
    else
    {
      conMng.setInTransaction(); //启动事务
    }
  }

  /**
   功能:执行sql语句
   @param ps_sql  sql语句
   @return  成功  查询结果  失败  null
   */
  public static ExecInfo execSql(String ps_sql)
  {
    boolean lb_isSelectSql = false;

    IConnection lo_conn = getConnection();
    if (lo_conn == null)
    {
      Util.logError("execSql:连接数据库失败!");
      return null;
    }
    try
    {
      lb_isSelectSql = Util.isSelectSql(ps_sql);
      ExecInfo lo_ret = lo_conn.execSql(ps_sql);

      if (lo_ret.getSqlCode() != 0)
      {
        return null;
      }
      if (lb_isSelectSql)
      {
        //产生查询数据集
        if (!lo_ret.generateDataSet())
        {
          return null;
        }
      }
      return lo_ret;
    }
    catch (Exception ex)
    {
      Util.logError("execSql:执行语句:" + ps_sql + "失败:" + ex);
      return null;
    }
    finally
    {
      updateConnectInfo(lb_isSelectSql, ps_sql); //更新连接状态信息
    }
  }

  /**
      功能:取得select count(..)类型sql语句的返回值
      @param ps_sql  select count() 型sql语句
      @return  成功 count(...)的返回值   失败 -1
   */
  public static long getRecCount(String ps_sql)
  {
    long ll_rec;
    ExecInfo lo_ret = null;
    try
    {
      lo_ret = execSql(ps_sql);
      if (lo_ret == null)
      {
        return -1;
      }
      if (lo_ret.getSqlCode() != 0)
      {
        return -1;
      }
      if (lo_ret.getRecNum() != 1)
      {
        return -1;
      }

      ll_rec = Long.parseLong(lo_ret.getColValue(1, 1));
      return ll_rec;
    }
    catch (Exception ex)
    {
      Util.logError("getSqlCount:" + ex);
      return -1;
    }
    finally
    {
      if (lo_ret != null)
      {
        lo_ret.close();
      }
    }
  }

  /**
    功能:取得查询结果数据集数据
    @param 无
    @return  成功 查询结果 失败 null
   */
  public static DataSetData getDataSetData(String ps_sql)
  {
    IConnection lo_conn = getConnection();

    try
    {
      if (lo_conn == null)
      {
        Util.logError("getDataSetData:连接数据库失败!");
        return null;
      }
      return lo_conn.getDataSetData(ps_sql);
    }
    catch (Exception ex)
    {
      Util.logError("getDataSetData:执行语句:" + ps_sql + "失败:" + ex);
      return null;
    }
    finally
    {
      updateConnectInfo(true, ps_sql); //更新连接状态信息
    }
  }

  /**
    功能:取得查询结果数据集数据
    @param pi_page 页号
    @param pi_pageRec 每页记录数
    @param ps_sql  sql语句
    @return  成功 查询结果 失败 null
   */
  public static DataSetData getDataSetData(int pi_page, int pi_pageRec, String ps_sql)
  {
    IConnection lo_conn = getConnection();

    try
    {
      if (lo_conn == null)
      {
        Util.logError("getDataSetData:连接数据库失败!");
        return null;
      }
      return lo_conn.getDataSetData(pi_page, pi_pageRec, ps_sql);
    }
    catch (Exception ex)
    {
      Util.logError("getDataSetData:执行语句:" + ps_sql + "失败:" + ex);
      return null;
    }
    finally
    {
      updateConnectInfo(true, ps_sql); //更新连接状态信息
    }
  }

  /**
    功能:取得查询结果数据集
    @param 无
    @return  成功 数据集 失败 null
   */
  public static TableDataSet getTableDataSet(String ps_sql)
  {
    TableDataSet lo_dataset = new TableDataSet(); //数据集

    //取得数据集数据
    DataSetData lo_data = getDataSetData(ps_sql);
    if (lo_data == null)
    {
      return null;
    }
    lo_dataset.empty();
    lo_data.loadDataSet(lo_dataset); //将数据装入数据集
    return lo_dataset;
  }

  /**
    功能:取得查询结果数据集
    @param pi_page 页号
    @param pi_pageRec 每页记录数
    @param ps_sql sql语句
    @return  成功 数据集 失败 null
   */
  public static TableDataSet getTableDataSet(int pi_page, int pi_pageRec, String ps_sql)
  {
    TableDataSet lo_dataset = new TableDataSet(); //数据集

    //取得数据集数据
    DataSetData lo_data = getDataSetData(pi_page, pi_pageRec, ps_sql);

    if (lo_data == null)
    {
      return null;
    }
    lo_dataset.empty();
    lo_data.loadDataSet(lo_dataset); //将数据装入数据集
    return lo_dataset;
  }

  /**
   功能:设置指定字段的下拉列表属性
   @param po_dataset          数据集
   @param ps_colname          要设置的数据窗口字段名
   @param ps_fetch_dict_sql   提取数据字典的sql语句
   @param ps_listcols         下拉数据窗口的显示字段名列表
   @param ps_dict_code_col         字典的代码字段
   @param ps_dict_name_col         字典的说明字段
   @return  成功 true  失败 false
   */
  public static boolean setColPickList(TableDataSet po_dataset, String ps_colname,
                                       String ps_fetch_dict_sql, String[] ps_listcols,
                                       String ps_dict_code_col, String ps_dict_name_col)
  {
    boolean lb_open = false;
    TableDataSet pickListDataSet;

    try
    {

      //数据字典数据集
      pickListDataSet = getTableDataSet(ps_fetch_dict_sql);
      pickListDataSet.open();

      //数据集先打开,后关闭,否则以下设置无法进行
      if (!po_dataset.isOpen())
      {
        if (po_dataset.getColumn(0) == null) //数据集还未从数据库提取过,则提取以取得字段信息
        {
          po_dataset.open();
        }
      }
      lb_open = po_dataset.isOpen();
      if (lb_open)
      {
        po_dataset.close();

        //设置字段的下拉属性
      }
      po_dataset.getColumn(ps_colname).setPickList(new PickListDescriptor(pickListDataSet,
          new String[]
          {ps_dict_code_col}
          , ps_listcols,
          new String[]
          {ps_colname}
          , ps_dict_name_col, false));

      //恢复原数据集的状态
      if (lb_open)
      {
        po_dataset.open();
      }
      return true;
    }
    catch (Exception ex)
    {
      Util.logError("setColPickList:error:" + ex);
      if (lb_open)
      {
        po_dataset.open();
      }
      return false;
    }
  }

  /**
     功能:保存数据窗口变动的数据
     @param  ps_sql 查询sql语句
     @param  po_changes  变动的数据集数据
     @return  可能产生错误的数据集
   */
  public static DataSetData saveDataChanges(String ps_sql, DataSetData po_changes) throws
      DataSetException
  {
    IConnection lo_conn = getConnection();
    try
    {
      if (lo_conn == null)
      {
        Util.logError("saveDataChanges:连接数据库失败!");
        throw new DataSetException("saveDataChanges:连接数据库失败!");
      }

      return lo_conn.saveDataChanges(ps_sql, po_changes);
    }
    catch (Exception ex)
    {
      throw new DataSetException(ex.toString());
    }
    finally
    {
      conMng.releaseConnection();
    }
  }

  /**
    功能:提交数据
    @param 无
    @return  1 成功 0 失败
   */
  public static int commit()
  {
    int li_ok = 0;
    try
    {
      IConnection lo_conn = getConnection();
      if (lo_conn == null)
      {
        Util.logError("commit:连接数据库失败!");
        return 0;
      }
      li_ok = lo_conn.commit();

      //释放连接
      conMng.setNotInTransaction();
      conMng.releaseConnection();
      return li_ok;
    }
    catch (Exception ex)
    {
      Util.logError("commit:" + ex.toString());
      return 0;
    }
  }

  /**
    功能:回滚事务,同时关闭数据库连接
    @param 无
    @return  1 成功  0 失败
   */
  public static int rollback()
  {
    int li_ok = 0;
    try
    {
      IConnection lo_conn = getConnection();
      if (lo_conn == null)
      {
        Util.logError("rollback:连接数据库失败!");
        return 0;
      }

      li_ok = lo_conn.rollback();

      //释放连接
      conMng.setNotInTransaction();
      conMng.releaseConnection();
      return li_ok;
    }
    catch (Exception ex)
    {
      Util.logError("rollback:" + ex.toString());
      return 0;
    }
  }

  /**
  功能:取得一个序列的值
  @param  ps_seqname 序列名
  @return   成功 序列值   失败 -1
   */
  public static long getSeq(String ps_seqname)
  {
    String ls_sql;
    long ll_seqvalue;
    ExecInfo lo_ret = null;
    try
    {
      ls_sql = "select " + ps_seqname + ".nextval from dual";
      lo_ret = execSql(ls_sql);
      if (lo_ret == null)
      {
        return -1;
      }
      if (lo_ret.getSqlCode() != 0)
      {
        return -1;
      }
      if (lo_ret.getRecNum() != 1)
      {
        return -1;
      }

      //序列值
      ll_seqvalue = Long.parseLong(lo_ret.getColValue(1, 1));
      return ll_seqvalue;
    }
    catch (Exception ex)
    {
      Util.logError("getSeq:取序列:" + ps_seqname + "失败:" + ex);
      return -1;
    }
    finally
    {
      if (lo_ret != null)
      {
        lo_ret.close();
      }
    }
  }

  /**
   功能:从sql语句中取得一条记录
   @param ps_sql select型sql语句
   @return  成功 查询结果 失败 null
   */
  public static ExecInfo fetch(String ps_sql)
  {
    long ll_recnum;
    try
    {
      ExecInfo lo_ret = execSql(ps_sql);
      if (lo_ret == null)
      {
        return null;
      }
      if (lo_ret.getSqlCode() != 0)
      {
        return null;
      }
      ll_recnum = lo_ret.getRecNum();
      if (ll_recnum == 0)
      {
        Util.logError("fetch:找不到记录!sql:" + ps_sql);
        return null;
      }
      if (ll_recnum > 1)
      {
        Util.logError("fetch:记录数太多!sql:" + ps_sql);
        return null;
      }
      return lo_ret;
    }
    catch (Exception ex)
    {
      Util.logError("fetch:" + ex);
      return null;
    }
  }

  /**
   功能:从sql语句中取得0条或多条记录
   @param ps_sql select型sql语句
   @return  成功 查询结果 失败 null
   */
  public static ExecInfo fetches(String ps_sql)
  {
    try
    {
      ExecInfo lo_ret = execSql(ps_sql);
      if (lo_ret == null)
      {
        return null;
      }
      if (lo_ret.getSqlCode() != 0)
      {
        return null;
      }
      return lo_ret;
    }
    catch (Exception ex)
    {
      Util.logError("fetches:" + ex);
      return null;
    }
  }

  /**
   功能:取得当前连接数
   @param 无
   @return 当前连接数
   */
  public static int getConnectionCount()
  {
    return conMng.getConnectionCount();
  }

  /**
   功能:关闭所有连接
   @param 无
   @return 无
   */
  public static void closeAllConnection()
  {
    if (conMng != null)
    {
      conMng.closeAllConnection();
      conMng = null;
    }
  }

  /**
   功能: 显示连接信息
   @param 无
   @return 无
   */
  public static void displayConnectInfo()
  {
    conMng = ConnectManager.getInstance();
    conMng.displayConnectInfo();
  }
}

⌨️ 快捷键说明

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