Oracle修改有規則的序列

來源:互聯網
上載者:User

需求/介紹

這是一個簡單的依據使用序列的表的行數修改序列的值使其不會在業務發生時首先衝突,通常在對錶進行大量操作後需要此功能。

約束

  1. 序列名稱是有規則的,其中包含有使用此序列的表名稱。
  1. 序列的值不會影響與商務邏輯的關聯關係。

實現機制

 

代碼

package org.ybygjy.example;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;import java.util.ArrayList;import java.util.List;import org.ybygjy.util.DBUtils;/** * 依據表的行數更新序列,有如下步驟: * <p>1、查資料庫中的序列 * <p>2、分析序列名稱提取表名稱 * <p>3、查詢表行數 * <p>4、更新序列值 * <h4>有如下約束:</h4> * <p>1、要求序列名稱是有規則的 * @author WangYanCheng * @version 2012-5-31 */public class AutoIncrementSEQ2 {    /** 與資料庫的串連 */    private Connection conn;    /** 首碼 */    private String[] seqREGPrefix = { "^S_", "^SEQ_" };    /** 尾碼 */    private String[] seqREGSuffix = { "_SEQ$" };    /**     * Constructor     * @param conn 與資料庫的串連     */    public AutoIncrementSEQ2(Connection conn) {        this.conn = conn;    }    /**     * 查表行數     * @param tableName 表名稱     * @return rtnNums:表的行數/-1:表不存在或其它情況     */    public int qryTableNums(String tableName) {        String tmplSQL = "SELECT COUNT(1) CC FROM ".concat(tableName);        int rtnNums = -1;        Statement stmt = null;        ResultSet rs = null;        try {            stmt = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);            rs = stmt.executeQuery(tmplSQL);            if (rs.next()) {                rtnNums = rs.getInt(1);            }        } catch (Exception e) {            rtnNums = -1;        } finally {            if (null != rs) {                try {                    rs.close();                } catch (SQLException e) {                    e.printStackTrace();                }            }            if (null != stmt) {                try {                    stmt.close();                } catch (SQLException e) {                    e.printStackTrace();                }            }        }        return rtnNums;    }    /**     * 查序列     * @return rtnSEQArr/null     */    public String[] qrySEQ() {        String qrySEQ = "SELECT SEQUENCE_NAME,INCREMENT_BY FROM USER_SEQUENCES";        PreparedStatement pstmt = null;        ResultSet rs = null;        String[] rtnArray = null;        try {            pstmt = conn.prepareStatement(qrySEQ);            rs = pstmt.executeQuery();            List<String> tmpList = new ArrayList<String>();            while (rs.next()) {                tmpList.add(rs.getString(1));                tmpList.add(rs.getString(2));            }            rtnArray = tmpList.toArray(new String[1]);        } catch (SQLException e) {            e.printStackTrace();        } finally {            if (null != rs) {                try {                    rs.close();                } catch (SQLException e) {                    e.printStackTrace();                }            }            if (null != pstmt) {                try {                    pstmt.close();                } catch (SQLException e) {                    e.printStackTrace();                }            }        }        return rtnArray;    }    /**     * 分析提取表名稱     * @param seqArray 序列集     * @return tableNameArray 表名稱集合     */    public String[] analyseTableName(String[] seqArray) {        String[] tableNameArray = new String[seqArray.length];        for (int index = tableNameArray.length; index >= 0; index--) {            String seqName = seqArray[index];            tableNameArray[index] = analyseTableName(seqName);            System.out.println("表:".concat(tableNameArray[index]).concat(":").concat(seqName));        }        return tableNameArray;    }    /**     * 重設序列     * @param seqName 序列名稱     * @param newSeqValue 新序列值     * @param oldSeqValue <strong>原始序列自增值</strong>     */    public void resetSEQ(String seqName, int newSeqValue, int oldSeqValue) {        String tmpSql1 = "ALTER SEQUENCE @SEQ INCREMENT BY @V";        String tmpSql2 = "SELECT @SEQ.nextval from dual";        Statement stmt = null;        try {            stmt = conn.createStatement();            stmt.execute(tmpSql1.replaceFirst("@SEQ", seqName).replaceFirst("@V",                String.valueOf(newSeqValue - 1)));            stmt.execute(tmpSql2.replaceFirst("@SEQ", seqName));            stmt.execute(tmpSql1.replaceFirst("@SEQ", seqName).replaceFirst("@V",                String.valueOf(oldSeqValue)));        } catch (Exception e) {            System.err.println("序列重設失敗:".concat(seqName).concat(":").concat(String.valueOf(newSeqValue))                .concat(":").concat(String.valueOf(oldSeqValue)));        } finally {            if (null != stmt) {                try {                    stmt.close();                } catch (Exception e) {                    e.printStackTrace();                }            }        }    }    /**     * 提取表名稱     * @param seqName 序列名稱     * @return tableName 表名稱     */    private String analyseTableName(String seqName) {        String tableName = null;        for (String tmpStr : seqREGPrefix) {            tableName = tableName == null ? seqName.replaceFirst(tmpStr, "") : tableName.replaceFirst(                tmpStr, "");        }        for (String tmpStr : seqREGSuffix) {            tableName = tableName == null ? seqName.replaceFirst(tmpStr, "") : tableName.replaceFirst(                tmpStr, "");        }        return tableName;    }    /**     * 程式調用入口     */    public void doWork() {        String[] seqArray = qrySEQ();        int index = seqArray.length - 1;        for (; index >= 0; index--) {            if (index % 2 == 0) {                String seqName = seqArray[index];                String tableName = analyseTableName(seqName);                int tableNums = qryTableNums(tableName);                tableNums = tableNums <= 0 ? 1 : tableNums;                resetSEQ(seqName, tableNums * 2, Integer.parseInt(seqArray[index + 1]));            }        }    }    /**     * 測試入口     * @param args 參數列表     */    public static void main(String[] args) {        String connURL = "jdbc:oracle:thin:NSTCSA4002/6316380@192.168.3.232:1521/NSDEV";        Connection conn = DBUtils.createConn4Oracle(connURL);        try {            AutoIncrementSEQ2 ais2 = new AutoIncrementSEQ2(conn);            ais2.doWork();        } catch (Exception e) {            e.printStackTrace();        } finally {            DBUtils.close(conn);        }    }}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.