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

📄 jmdboconntioncall.java

📁 梦界家园程序开发基底框架
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
package jm.dbo;

import java.math.BigDecimal;
import java.sql.BatchUpdateException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp;
import java.sql.Types;

import jm.dbo.fieldtype.others.DBFieldTypes;
import jm.util.JM2DArray;
import jm.util.JMCheck;
import jm.util.JMUtility;
import jm.framework.gui.JMEntity;
import java.util.ArrayList;

/**
 * <p>Title: DB数据库连接调用</p>
 *
 * <p>Copyright: Copyright (c) 2004-2007</p>
 *
 * <p>Company: 1SHome</p>
 *
 * <p>@author Spook</p>
 *
 * @since 1.3.3
 * @see JDK 1.5.0.6
 */
public class JMDBOConntionCall implements JMDBOCall, JMDBOCommandKeys, DBFieldTypes {
    protected final String CLASS_NAME = "JMDBOConntionCall";

    protected Connection conn_ = null;

    protected Statement stmt_ = null;

    protected ResultSet resultData_ = null;

    protected JMDBOConnectionManager oDBConMan_ = null;

    protected PreparedStatement pstmt_ = null;

    protected static JMDBOConntionCall instance_ = null;

    protected static JMDBOConntionParameterInfoKeyValue oDBSQLBean_ = null;

    protected static boolean isSQLFree_ = false;

    protected static String SQLFromType_ = "";

    protected JMDBOConntionCall () {
    }

    /**
     * 使用完整得SQL文
     * @param isSQLFree boolean
     */
    public synchronized void setSQLFree (boolean isSQLFree) {
        isSQLFree_ = isSQLFree;
        oDBSQLBean_.setSQLFree_(isSQLFree);
    }

    /**
     * 获得连接实例
     * @return JMDBOConntionCall
     */
    public static synchronized JMDBOConntionCall getInstance () {
        if (instance_ == null) {
            instance_ = new JMDBOConntionCall();
        }
        oDBSQLBean_ = new JMDBOConntionParameterInfoKeyValue();
        return instance_;
    }

    /**
     * 运行SQL文
     */
    public synchronized JMDBOOutKeyValue inVoker (JMDBOInKeyValue inKeyValue) throws Exception {

        JMDBOOutKeyValue oDbReturn = new JMDBOOutKeyValue();

        // call db
        if (inKeyValue == null) {
            oDbReturn.setResult(false);
            oDbReturn.setResultMessage("运行参数为NULL");
            return oDbReturn;
        }

        // check db name
        if (JMCheck.isNull(inKeyValue.getDBName())) {
            oDbReturn.setResult(false);
            oDbReturn.setResultMessage("数据库名称没有正确定义");
            return oDbReturn;
        }
        oDBSQLBean_.setSQLUsedDataBeseName(inKeyValue.getDBName());

        //  check db type
        if (JMCheck.isNull(inKeyValue.getDBType())) {
            oDbReturn.setResult(false);
            oDbReturn.setResultMessage("数据库种类没有正确定义");
            return oDbReturn;
        }
        oDBSQLBean_.setDataBaseType(inKeyValue.getDBType());

        // SQL ID
        String sUSqlId = inKeyValue.getSQLID();
        if (JMCheck.isNull(sUSqlId) && !isSQLFree_) {
            oDbReturn.setResult(false);
            oDbReturn.setResultMessage("SQL文没有正确定义");
            return oDbReturn;
        }

        //保存参数
        oDBSQLBean_.setWhere(inKeyValue.getWhereParameter());
        oDBSQLBean_.setData(inKeyValue.getUpParameter());
        oDBSQLBean_.setStrType(inKeyValue.getStrType());
        oDBSQLBean_.setUseEntity(inKeyValue.isUseEntity());
        oDBSQLBean_.setEntityName(inKeyValue.getEntityName());

        // SQL get
        if (!mGetSQL(sUSqlId)) {
            oDbReturn.setResult(false);
            oDbReturn.setResultMessage("SQL文没有正确定义");
            return oDbReturn;
        }

        // 張棟敾掕偡傞
        String stype = oDBSQLBean_.getStrType();

        if (stype.equals(SVC_SELECT)) {
            oDbReturn = executeQuery();
        } else if (stype.equals(SVC_INSERT)) {
            oDbReturn = executeInsert();
        } else if (stype.equals(SVC_UPDATE)) {
            oDbReturn = executeOthers();
        } else if (stype.equals(SVC_DELETE)) {
            oDbReturn = executeOthers();
        } else if (stype.equals(SVC_PIC_SEL)) {
            oDbReturn = mDBasePicSelect();
        } else if (stype.equals(SVC_PIC_CON)) {
            oDbReturn = mDBasePicCon(); ;
        } else if (stype.equals(SVC_TABLE_SELECT)) {
            int iPageRowNo = 0; // aoDataForm.getIntValue(SVC_TB_PAGE_ROW);

            int iCurrentPageNo = 0; // aoDataForm.getIntValue(SVC_TB_PAGENO);

            oDbReturn = mDBaseSelectTable(iPageRowNo, iCurrentPageNo);
        } else {
            oDbReturn.setResult(false);
        }

        return oDbReturn;

    }

    /*
     * 偙偺 PreparedStatement 僆僽僕僃僋僩偺 SQL 僋僄儕乕傪幚峴偟丄偦偺僋僄儕乕偵傛偭偰惗惉偝傟偨 ResultSet
     * 僆僽僕僃僋僩傪曉偟傑偡丅 栠傝抣: 僋僄儕乕偵傛偭偰嶌惉偝傟偨僨乕僞傪娷傓 ResultSet 僆僽僕僃僋僩丅null 偵偼側傜側偄 椺奜:
     * SQLException - 僨乕僞儀乕僗傾僋僙僗僄儔乕偑敪惗偟偨応崌丄傑偨偼 SQL 暥偑 ResultSet 僆僽僕僃僋僩傪曉偝側偄応崌
     */
    protected synchronized JMDBOOutKeyValue executeQuery () throws Exception {

        JMDBOOutKeyValue oDataOut = new JMDBOOutKeyValue();
        boolean pbresult = true;
        // data select
        if (!mDbConnSelect(oDBSQLBean_.getSQL())) {
            pbresult = false;
        } else {
            if (oDBSQLBean_.isUseEntity()) {
                ArrayList<JMEntity> oOut = JMDBOResultSetApc.getResultListData(resultData_, oDBSQLBean_.getEntityName());
                oDataOut.setList(oOut);
                oDataOut.setResultCount(oOut.size());
            } else {
                JM2DArray oOut = JMDBOResultSetApc.getResultData(resultData_);
                oDataOut.setResultData(oOut);
                oDataOut.setResultCount(oOut.rowCount());
            }
            if (!commit() || !close()) {
                pbresult = false;
            }
        }

        oDataOut.setResult(pbresult);
        return oDataOut;

    }

    /*
     * 僐儅儞僪偺僶僢僠傪僨乕僞儀乕僗偵憲怣偟偰幚峴偟丄偡傋偰偺僐儅儞僪偑惓忢偵幚峴偝傟傞偲丄峏怴僇僂儞僩偺攝楍傪曉偟傑偡丅曉偝傟傞攝楍偺 int
     * 梫慺偼丄僶僢僠撪偺僐儅儞僪偵懳墳偟偰曉偝傟弴彉晅偗傜傟傑偡丅偮傑傝丄僶僢僠偵捛壛偝傟偨弴彉偵廬偭偰弴彉晅偗傜傟傑偡丅儊僜僢僪
     * executeBatch 偵傛偭偰曉偝傟傞攝楍撪偺梫慺偼丄師偺偳傟偐 1 偮偵奩摉偟傑偡丅 僛儘埲忋偺悢抣 --
     * 僐儅儞僪偑惓忢偵張棟偝傟偨偙偲傪帵偟丄僐儅儞僪偺幚峴偵傛偭偰塭嬁傪庴偗偨僨乕僞儀乕僗撪偺峴悢傪巜掕偡傞峏怴僇僂儞僩 SUCCESS_NO_INFO
     * 偺抣 -- 僐儅儞僪偑惓忢偵張棟偝傟偨偑丄塭嬁傪庴偗偨峴悢偑晄柧側偙偲傪帵偡 僶僢僠峏怴撪偺僐儅儞僪偺 1
     * 偮偑惓忢偵幚峴偱偒側偐偭偨応崌丄偙偺儊僜僢僪偼 BatchUpdateException 傪僗儘乕偟丄JDBC
     * 僪儔僀僶偼僶僢僠撪偺巆傝偺僐儅儞僪偺張棟傪宲懕偡傞偙偲傕丄拞巭偡傞偙偲傕偱偒傑偡丅偨偩偟丄僪儔僀僶偺摦嶌偼摿掕偺 DBMS
     * 偲堦娧惈傪曐偮昁梫偑偁傝丄忢偵僐儅儞僪偺張棟傪宲懕偡傞偐丄偁傞偄偼宲懕偟側偄偐偺偳偪傜偐偵側傝傑偡丅幐攕屻丄僪儔僀僶偑張棟傪宲懕偡傞応崌偼丄BatchUpdateException.getUpdateCounts
     * 儊僜僢僪偱曉偝傟傞攝楍偵偼丄僶僢僠撪偺僐儅儞僪偲摨偠悢偺梫慺偑奿擺偝傟丄偦傟傜偺梫慺偺 1 偮埲忋偑師偵奩摉偟傑偡丅
     *
     * EXECUTE_FAILED 偺抣 --
     * 幐攕偟偨僐儅儞僪偑惓忢偵幚峴偝傟偨偙偲傪帵偟丄僐儅儞僪偑幐攕偟偨偁偲偵丄僪儔僀僶偑僐儅儞僪偺張棟傪宲懕偟偨応崌偵尷傝敪惗偡傞
     * 僪儔僀僶偼偙偺儊僜僢僪傪幚憰偡傞昁梫偼偁傝傑偣傫丅Java 2 SDK, Standard Edition, 僶乕僕儑儞 1.3
     * 偱偼丄巊梡偱偒傞幚憰偍傛傃栠傝抣偑廋惓偝傟偰丄BatchUpdateException
     * 僆僽僕僃僋僩偑僗儘乕偝傟偨偁偲偵僶僢僠峏怴撪偺僐儅儞僪偺張棟傪宲懕偱偒傞僆僾僔儑儞偑梡堄偝傟偰偄傑偡丅
     *
     * 栠傝抣: 僶僢僠撪偺僐儅儞僪偛偲偵 1 偮偺梫慺偑奿擺偝傟偰偄傞峏怴僇僂儞僩偺攝楍丅攝楍偺梫慺偼僐儅儞僪偑僶僢僠偵捛壛偝傟偨弴彉偱暲傋傜傟傞
     * 椺奜: SQLException -
     * 僨乕僞儀乕僗傾僋僙僗僄儔乕偑敪惗偟偨応崌丄傑偨偼僪儔僀僶偑僶僢僠暥傪僒億乕僩偟側偄応崌丅僨乕僞儀乕僗偵憲怣偝傟偨僐儅儞僪偺 1
     * 偮偑惓忢偵幚峴偝傟側偐偭偨応崌傗丄寢壥僙僢僩傪曉偦偆偲偡傞偲 BatchUpdateException (SQLException 偺僒僽僋儔僗)
     * 偑僗儘乕偝傟傞 摫擖偝傟偨僶乕僕儑儞: 1.3
     */
    protected synchronized JMDBOOutKeyValue executeInsert () throws Exception {
//        final String sMethod = "executeInsert";
        JMDBOOutKeyValue oDataOut = new JMDBOOutKeyValue();
        try {
            boolean pbresult = true;

            if (!mGetDbCon()) {
                throw new Exception("DB Connection get err");
            }

            pstmt_ = conn_.prepareStatement(oDBSQLBean_.getSQL());

            // 栠傝抣
            int[] r = null;
            JM2DArray oIn = oDBSQLBean_.getData();
            if (oIn == null) {
                oIn = new JM2DArray();
            }

            ////////////////////////////////////////////
            {
                for (int i = 0; i < oIn.rowCount(); i++) {
                    for (int m = 0; m < oIn.colCount(); m++) {
                        // /////////////////////////////////////////////
                        pstmt_.setString(m + 1, JMCheck.isNull(oIn.getStringValue(i, m)) ? "" : oIn.getStringValue(i, m));
                        // /////////////////////////////////////////////
                    }
                    pstmt_.addBatch();
                }
                r = pstmt_.executeBatch();
            }

            // import oracle.jdbc.driver.OracleConnection;
            // import oracle.jdbc.driver.OraclePreparedStatement;
            // 僶僢僠暥偑惓忢偵幚峴偝傟偨偑丄塭嬁傪庴偗偨峴悢偑晄柧側偙偲傪帵偡掕悢偱偡丅
            // SUCCESS_NO_INFO;
            JM2DArray oOut = new JM2DArray("DBResult");
            oOut.setAutoNext(true);
            for (int i = 0; i < r.length; i++) {
                oOut.addItem(SVC_DB_PARAM, r[i]);
            }

            if (!commit() || !close()) {
                pbresult = false;
            }

            oDataOut.setResult(pbresult);
            oDataOut.setResultData(oOut);
            oDataOut.setResultCount(oOut.rowCount());
            return oDataOut;
        } catch (BatchUpdateException ex) {
            try {
                rollback();
            } catch (Exception ez) {
                throw ez;
            }
            /*

⌨️ 快捷键说明

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