jdbc框架,方便以後使用

package com.zfsoft.setup.dao.impl;


import java.sql.CallableStatement;
import java.sql.Clob;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Vector;


import oracle.jdbc.driver.OracleResultSet;
import oracle.jdbc.driver.OracleTypes;


import com.zfsoft.util.Pages;
import com.zfsoft.util.StringFormat;
import com.zfsoft.util.Tool;


/**
 * 數據庫操作基本類
 * 
 */


public class DAO {


    /**
     * 數據源
     */
    //protected static DataSource db;


    /**
     * 連接
     */
    protected Connection        conn         = null;


    protected PreparedStatement stmt         = null;


    protected Statement         stat         = null;


    protected CallableStatement cstmt        = null;


    protected ResultSet         rs           = null;


    protected PreparedStatement ps           = null;


    private ResultSetMetaData   rsmd         = null;


    // public int print = 0;


    private String              splitSignOne = "!!SplitSignOne!!";


    private String              splitSignTwo = "!!SplitSignTwo!!";


    // /**
    // * 實例化DAO
    // *
    // */
    // public DAO() {
    // this.db = DBPool.getPool();
    // }
    //
    // /**
    // * 實例化DAO
    // *
    // */
    // public DAO(int print) {
    // this.db = DBPool.getPool();
    // this.print = print;
    // }
    
    public DAO(Connection conn){
    this.conn= conn;
    }


    private DAO(){
   
    }
    /**
     * 功能描述:返回數組 裏面包含一列outputValue 作者:張建軍; 日期:2008-6-30 日期:上午08:25:35
     * 方法名:getOneRs 訪問類:DAO
     * 
     * @param sql
     *            語句
     * @param inputValue
     *            傳入參數數組
     * @param outputValue
     *            輸出字段數組
     * @return String[]
     * 
     */
    public String[] getOneRs(String sql, String[] inputValue, String[] outputValue)
    {
        String[] result = new String[outputValue.length];// SIZE
        try
        {
            //this.conn = db.getConnection();
            stmt = conn.prepareStatement(sql);
            for (int i = 0; i < inputValue.length; i++)
            {
                stmt.setString(i + 1, inputValue[i]);
            }
            rs = stmt.executeQuery();
            if (rs.next())
            {
                for (int i = 0; i < outputValue.length; i++)
                {
                    String temp = rs.getString(outputValue[i]);
                    result[i] = Tool.isNull(temp) ? "" : temp;
                }
            }
            else
            {
                result = null;
            }
            closeStmt();
        }
        catch (SQLException e)
        {
            e.printStackTrace();
            Tool.print("sql:" + sql);
            for (int i = 0; i < inputValue.length; i++)
            {
                if (i == inputValue.length - 1)
                {
                    Tool.print(inputValue[i]);
                }
                else
                {
                    System.out.print(inputValue[i] + " ");
                }
            }
            result = null;
        }
        finally
        {
            closeStmt();
            closeConn();
        }
        return result;
    }


    /**
     * 
     * 功能描述:返回字符串 裏面包含一列outputValue 作者:張建軍; 日期:2008-6-30 日期:上午08:32:55
     * 方法名:getOneRs 訪問類:DAO
     * 
     * @param sql
     *            語句
     * @return String
     * 
     */
    public String getOneRs(String sql)
    {
        String result;
        try
        {
            //this.conn = db.getConnection();
            stmt = conn.prepareStatement(sql);
            rs = stmt.executeQuery();
            if (rs.next())
            {
                result = rs.getString(1);
            }
            else
            {
                result = null;
            }
            closeStmt();
        }
        catch (SQLException e)
        {
            e.printStackTrace();
            Tool.print("sql:" + sql);
            result = null;
        }
        finally
        {
            closeStmt();
            closeConn();
        }
        return result;
    }


    /**
     * 
     * 功能描述:關閉Stmt 作者:張建軍; 日期:2008-6-30 日期:上午08:40:16 方法名:closeStmt 訪問類:DAO
     * 
     */
    protected void closeStmt()
    {
        try
        {
            if (rs != null)
            {
                this.rs.close();
            }
            if (ps != null)
            {
                this.ps.close();
            }
            if (stmt != null)
            {
                this.stmt.close();
            }
            if (stat != null)
            {
                this.stat.close();
            }
            if (cstmt != null)
            {
                this.cstmt.close();
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally
        {


        }
    }


    /**
     * 
     * 
     */
    public void closeConn()
    {


    }
    
    public void closeConnManual()
    {
        try
        {
        closeStmt();
            if (conn != null)
            {
                this.conn.close();
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            this.rs = null;
            this.ps = null;
            this.stmt = null;
            this.stat = null;
            this.cstmt = null;
            this.conn = null;
        }
    }
    public String[] getColumnName(String sql)
    {
        String[] tit = null;
        try
        {
            //this.conn = db.getConnection();
            stmt = conn.prepareStatement(sql);
            rs = stmt.executeQuery();
            rsmd = rs.getMetaData();
            tit = new String[rsmd.getColumnCount()];
            for (int i = 0; i < rsmd.getColumnCount(); i++)
            {
                tit[i] = rsmd.getColumnLabel(i + 1);
            }
            closeStmt();
        }
        catch (SQLException e)
        {
            e.printStackTrace();
            Tool.print(sql);
            tit = new String[0];
        }
        finally
        {
            closeStmt();
            closeConn();
        }
        return tit;
    }


    public String[] getColumnNameCN(String[] cName, String tabName, String Dblink)
    {
        String[] tit = new String[cName.length];
        String sql = "";
        sql = "select COMMENTS from user_col_comments where table_name=? and COLUMN_NAME=?";


        for (int i = 0; i < cName.length; i++)
        {
            String[] tmp = getOneRs(sql, new String[] { tabName.toUpperCase(), cName[i].toUpperCase() },
                    new String[] { "COMMENTS" });
            if (tmp == null)
            {
                tit[i] = cName[i];
            }
            else
            {
                tit[i] = tmp[0];
                if ((tit[i] == null) || tit[i].toLowerCase().equalsIgnoreCase("null") || tit[i].equalsIgnoreCase(""))
                {
                    tit[i] = cName[i];
                }
                if (tit[i].indexOf('(') >= 0)
                {
                    tit[i] = tit[i].substring(0, tit[i].indexOf('('));
                }
            }
        }
        return tit;
    }


    /**
     * 
     * 訪問類:DAO
     * 
     * @param sql
     *            語句
     * @param inputValue
     *            傳入參數數組
     * @param outputValue
     *            輸出字段數組
     * @return HashMap
     * 
     */
    public HashMap<String, String> getOneMapByList(String sql, String[] inputValue, String[] outputValue)
    {
        List<HashMap<String, String>> list = this.getList(sql, inputValue, outputValue);
        if (list.size() == 0)
            return null;
        else
            return (HashMap<String, String>) list.get(0);
    }


    /**
     * 
     * 方法名:getList 訪問類:DAO
     * 
     * @param sql
     *            語句
     * @param inputValue
     *            傳入參數數組
     * @param outputValue
     *            輸出字段數組
     * @return List
     * 
     */
    public List<HashMap<String, String>> getList(String sql, String[] inputValue, String[] outputValue)
    {
        ArrayList<HashMap<String, String>> arrayList = new ArrayList<HashMap<String, String>>();
        try
        {
            //this.conn = db.getConnection();
            stmt = conn.prepareStatement(sql);
            for (int i = 0; i < inputValue.length; i++)
            {
                stmt.setString(i + 1, inputValue[i]);
            }
            rs = stmt.executeQuery();
            while (rs.next())
            {
                HashMap<String, String> map = new HashMap<String, String>();
                for (int i = 0; i < outputValue.length; i++)
                {
                    String temp = rs.getString(outputValue[i]);
                    map.put(outputValue[i], Tool.isNull(temp) ? "" : temp);
                }
                arrayList.add(map);
            }
            closeStmt();
        }
        catch (SQLException e)
        {
            Tool.print(e.getMessage());
            Tool.print("sql:" + sql);
            for (int i = 0; i < inputValue.length; i++)
            {
                if (i == inputValue.length - 1)
                {
                    Tool.print(inputValue[i]);
                }
                else
                {
                    System.out.print(inputValue[i] + " ");
                }
            }
            // e.printStackTrace();
            arrayList = null;
        }
        finally
        {
            closeStmt();
            closeConn();
        }
        return arrayList;
    }


    /**
     * 
     * 
     * @param sql
     *            語句
     * @return List
     * @throws SQLException
     * 
     */
    public List<String> getRealRow(String sql) throws SQLException
    {
        List<String> list = new ArrayList<String>();
        try
        {
            //this.conn = db.getConnection();
            stat = conn.createStatement();
            rs = stat.executeQuery(sql);
            java.sql.ResultSetMetaData rsdate = rs.getMetaData();
            int allrows = rsdate.getColumnCount();
            for (int i = 1; i <= allrows; i++)
            {
                // rstring[i-1]=rsdate.getColumnLabel(i);
                list.add(rsdate.getColumnLabel(i));
            }
            closeStmt();
            return list;
        }
        catch (SQLException e)
        {
            Tool.print("sql:" + sql);
            throw e;
        }
        finally
        {
            closeStmt();
            closeConn();
        }


    }


    /**
     * throw error
     * 
     * @param sql
     *            語句
     * @param inputValue
     *            傳入參數數組
     * @param outputValue
     *            輸出字段數組
     * @return List
     * @throws SQLException
     */
    public List<HashMap<String, String>> getnewList(String sql, String[] inputValue, String[] outputValue)
            throws SQLException
    {
        ArrayList<HashMap<String, String>> arrayList = new ArrayList<HashMap<String, String>>();
        try
        {
            //this.conn = db.getConnection();
            stmt = conn.prepareStatement(sql);
            for (int i = 0; i < inputValue.length; i++)
            {
                stmt.setString(i + 1, inputValue[i]);
            }
            rs = stmt.executeQuery();
            while (rs.next())
            {
                HashMap<String, String> map = new HashMap<String, String>();
                for (int i = 0; i < outputValue.length; i++)
                {
                    String temp = rs.getString(outputValue[i]);
                    map.put(outputValue[i], Tool.isNull(temp) ? "" : temp);
                }
                arrayList.add(map);
            }
            closeStmt();
        }
        catch (SQLException e)
        {
            Tool.print("sql:" + sql);
            for (int i = 0; i < inputValue.length; i++)
            {
                if (i == inputValue.length - 1)
                {
                    Tool.print(inputValue[i]);
                }
                else
                {
                    System.out.print(inputValue[i] + " ");
                }
            }
            arrayList = null;
            throw e;
        }
        finally
        {
            closeStmt();
            closeConn();
        }
        return arrayList;
    }


    /**
     * 功能描述:返回一行數據當中一列的值 例如 select * from car where id=?/"123"/name 作者:張建軍;
     * 日期:2008-6-30 日期:上午08:52:30 方法名:getOneRs 訪問類:DAO
     * 
     * @param sql
     *            語句
     * @param inputValue
     *            傳入參數數組
     * @param outputValue
     *            返回字段名
     * @return String
     * 
     */
    public String getOneRs(String sql, String[] inputValue, String outputValue)
    {
        String[] result = getOneRs(sql, inputValue, new String[] { outputValue });// return
                                                                                    // one
                                                                                    // row
        if (result != null && result.length == 1)
        {
            return result[0];
        }
        return "";
    }


    /**
     * 
     * 
     * @param sql
     *            語句
     * @param inputValue
     *            傳入參數數組
     * @param outputValue
     *            輸出字段數組
     * @return Vector<String[]>
     * 
     */
    public Vector<String[]> rsToVator(String sql, String[] inputValue, String[] outputValue)
    {
        Vector<String[]> vector = new Vector<String[]>();
        try
        {
            //this.conn = db.getConnection();
            stmt = conn.prepareStatement(sql);
            for (int i = 0; i < inputValue.length; i++)
            {
                stmt.setString(i + 1, inputValue[i]);
            }
            rs = stmt.executeQuery();
            while (rs.next())
            {
                String[] tmp = new String[outputValue.length];
                for (int i = 0; i < outputValue.length; i++)
                {
                    tmp[i] = rs.getString(outputValue[i]);
                    if ((tmp[i] == null) || tmp[i].equalsIgnoreCase("null"))
                    {
                        tmp[i] = "";
                    }
                }
                vector.add(tmp);
            }
            closeStmt();
        }
        catch (SQLException e)
        {
            Tool.print("sql:" + sql);
            for (int i = 0; i < inputValue.length; i++)
            {
                if (i == inputValue.length - 1)
                {
                    Tool.print(inputValue[i]);
                }
                else
                {
                    System.out.print(inputValue[i] + " ");
                }
            }
            e.printStackTrace();
            vector = null;
        }
        finally
        {
            closeStmt();
            closeConn();
        }
        return vector;
    }


    /**
     * 
     * @param sql
     *            語句
     * @param input
     *            傳參數數組
     * @return boolean
     * 
     */
    public boolean runUpdate(String sql, String[] input)
    {
        boolean result = true;
        try
        {
            //this.conn = db.getConnection();
            conn.setAutoCommit(false);
            stmt = conn.prepareStatement(sql);
            for (int i = 0; i < input.length; i++)
            {
                stmt.setString(i + 1, input[i]);
            }
            stmt.executeUpdate();
            closeStmt();
        }
        catch (SQLException e)
        {
            Tool.print("sql:" + sql);
            e.printStackTrace();
            try
            {
                conn.rollback();
            }
            catch (SQLException e1)
            {
                Tool.print("sql:" + sql);
                e.printStackTrace();
            }
            result = false;
        }
        finally
        {
            try
            {
                conn.commit();
            }
            catch (SQLException e)
            {
                Tool.print("sql:" + sql);
                e.printStackTrace();
            }
            closeStmt();
            closeConn();
        }
        return result;
    }


    /**
     * 
     * 
     * @param sql
     *            語句
     * @return boolean
     * 
     */
    public boolean runUpdate(String sql)
    {
        boolean result = true;
        try
        {
            //this.conn = db.getConnection();
            conn.setAutoCommit(false);
            stat = conn.createStatement();
            stat.execute(sql);
            closeStmt();
        }
        catch (SQLException e)
        {
            Tool.print("sql:" + sql);
            e.printStackTrace();
            try
            {
                conn.rollback();
            }
            catch (SQLException e1)
            {
                Tool.print("sql:" + sql);
                e.printStackTrace();
            }
            result = false;
        }
        finally
        {
            try
            {
                conn.commit();
            }
            catch (SQLException e)
            {
                Tool.print("sql:" + sql);
                e.printStackTrace();
            }
            closeStmt();
            closeConn();
        }
        return result;
    }


    /**
     * 
     * 訪問類:DAO
     * 
     * @param sql
     *            語句
     * @param input
     *            傳參數數組
     * @return String
     * 
     */
    public String runewUpdate(String sql, String[] input)
    {
        String result = "";
        try
        {
            //this.conn = db.getConnection();
            conn.setAutoCommit(false);
            stmt = conn.prepareStatement(sql);
            for (int i = 0; i < input.length; i++)
            {
                stmt.setString(i + 1, input[i]);
            }
            stmt.executeUpdate();
            closeStmt();
        }
        catch (SQLException e)
        {
            result = e.getMessage();
            Tool.print("sql:" + sql);
            // e.printStackTrace();
            try
            {
                conn.rollback();
            }
            catch (SQLException e1)
            {
                result = e.getMessage();
                Tool.print("sql:" + sql);
                // e.printStackTrace();
            }
            // result = false;
        }
        finally
        {
            try
            {
                conn.commit();
            }
            catch (SQLException e)
            {
                result = e.getMessage();
                Tool.print("sql:" + sql);
                // e.printStackTrace();
            }
            closeStmt();
            closeConn();
        }
        return result;
    }


    /**
     * 
     * 功能描述:重寫UPDATE 得到錯誤信息 return==null 說明 執行成功
     * 日期:上午09:03:18 方法名:runewUpdate 訪問類:DAO
     * 
     * @param sql
     *            語句
     * @return String
     * 
     */
    public String runewUpdate(String sql)
    {
        String result = null;
        try
        {
            //this.conn = db.getConnection();
            conn.setAutoCommit(false);
            stat = conn.createStatement();
            stat.execute(sql);
            closeStmt();
        }
        catch (SQLException e)
        {
            Tool.print("sql:" + sql);
            // Tool.print("錯誤信息::::::::::::" + e.getMessage());
            try
            {
                conn.rollback();
            }
            catch (SQLException e1)
            {
                Tool.print("sql:" + sql);
                // Tool.print("錯誤信息::::::::::::" + e.getMessage());
            }
            result = e.getMessage();
        }
        finally
        {
            try
            {
                conn.commit();
            }
            catch (SQLException e)
            {
                Tool.print("sql:" + sql);
                // Tool.print("錯誤信息::::::::::::" + e.getMessage());
            }
            closeStmt();
            closeConn();
        }
        return result;
    }


    /**
     * 
     * 功能描述:執行語句返回真假
     * 
     * @param v
     *            語句集合
     * @return boolean
     * 
     */
    public boolean runUpdate(Vector<HashMap<String, Object>> v)
    {
    String sqlTxt = "";
    String sqlVal = "";
        boolean result = true;
        try
        {
            //this.conn = db.getConnection();
            conn.setAutoCommit(false);
            for (int i = 0; i < v.size(); i++)
            {
                HashMap<String, Object> map = new HashMap<String, Object>();
                map = (HashMap<String, Object>) v.get(i);
                String[] inVal = (String[]) map.get("sqlVal");
                stmt = conn.prepareStatement(map.get("sqlTxt").toString());
                for (int j = 0; j < inVal.length; j++)
                {
                sqlTxt = map.get("sqlTxt").toString();
                sqlVal +=inVal[j]+",";
                    stmt.setString(j + 1, inVal[j]);
                }
                stmt.executeUpdate();
                sqlVal = "";
                closeStmt();
            }
        }
        catch (SQLException e)
        {
            e.printStackTrace();
            try
            {
            System.out.println("錯誤的語句爲:");
            System.out.println(sqlTxt);
            System.out.println(sqlVal);
                conn.rollback();
            }
            catch (SQLException e1)
            {
                e.printStackTrace();
            }
            result = false;
        }
        finally
        {
            try
            {
                conn.commit();
            }
            catch (SQLException e)
            {
                e.printStackTrace();
            }
            closeStmt();
            closeConn();
        }
        return result;
    }


    public PreparedStatement geStatement(String sql)
    {
        return stmt;
    }


    /**
     * 
     * 功能描述:執行存儲過程 
     * 
     * @param sql
     *            語句
     * @param input
     *            傳參數數組
     * @return boolean
     * 
     */
    public boolean runProcuder(String sql, String[] input)
    {
        boolean result = true;
        try
        {
            //this.conn = db.getConnection();
            cstmt = conn.prepareCall(sql);
            for (int i = 0; i < input.length; i++)
            {
                cstmt.setString(i + 1, input[i]);
            }
            cstmt.executeUpdate();


            closeStmt();
        }
        catch (SQLException e)
        {
            e.printStackTrace();
            try
            {
                conn.rollback();
            }
            catch (SQLException e1)
            {
                e.printStackTrace();
            }
            result = false;
        }
        finally
        {
            try
            {
                conn.commit();
            }
            catch (SQLException e)
            {
                e.printStackTrace();
            }
            closeStmt();
            closeConn();
        }
        return result;
    }


    /**
     * 功能描述:實現分頁查詢,需要存儲過程
     * 訪問類:DAO 返回類型:List<HashMap<String,String>>
     * 
     * @param sqlselect
     *            查詢語句
     * @param outputValue
     *            輸出字段數組
     * @param pages
     *            分頁對象
     * @return List
     */
    public List<HashMap<String, String>> runProcuderPage(String sqlselect, String[] outputValue, Pages pages)
    {
        ArrayList<HashMap<String, String>> arrayList = new ArrayList<HashMap<String, String>>();
        try
        {
            //this.conn = db.getConnection();
            String sql = "{ call prc_query(?,?,?,?,?,?)}";
            cstmt = conn.prepareCall(sql);
            cstmt.setString(1, sqlselect);
            cstmt.setInt(2, pages.getCurrentPage());
            cstmt.setInt(3, pages.getPageSize());
            cstmt.registerOutParameter(4, OracleTypes.INTEGER);
            cstmt.registerOutParameter(5, OracleTypes.INTEGER);
            cstmt.registerOutParameter(6, OracleTypes.CURSOR);
            cstmt.execute();
            pages.setMaxRecord(cstmt.getInt(4));
            pages.setMaxPage(cstmt.getInt(5));
            rs = (ResultSet) cstmt.getObject(6);
            while (rs.next())
            {
                HashMap<String, String> map = new HashMap<String, String>();
                for (int i = 0; i < outputValue.length; i++)
                {
                    map.put(outputValue[i], rs.getString(outputValue[i]) == null ? "" : rs.getString(outputValue[i]));
                }
                arrayList.add(map);
            }
            closeStmt();
        }
        catch (SQLException e)
        {
            Tool.print("sql:" + sqlselect);
            e.printStackTrace();
            try
            {
                conn.rollback();
            }
            catch (SQLException e1)
            {
                Tool.print("sql:" + sqlselect);
                e.printStackTrace();
            }


        }
        finally
        {
            try
            {
                conn.commit();
            }
            catch (SQLException e)
            {
                Tool.print("sql:" + sqlselect);
                e.printStackTrace();
            }
            closeStmt();
            closeConn();
        }
        return arrayList;


    }


    /**
     * 
     * 功能描述:讀取Clob類型列 返回Clob類型 
     * 訪問類:DAO
     * 
     * @param sql
     *            語句
     * @param inputValue
     *            傳入參數數組
     * @param colName
     *            列名
     * @return Clob
     * 
     */
    public Clob getOneClob(String sql, String[] inputValue, String colName)
    {
        Clob clob = null;
        try
        {
            //this.conn = db.getConnection();
            stmt = conn.prepareStatement(sql);
            for (int i = 0; i < inputValue.length; i++)
            {
                stmt.setString(i + 1, inputValue[i]);
            }
            rs = stmt.executeQuery();
            rs.next();
            clob = rs.getClob(colName);
            closeStmt();
        }
        catch (SQLException e)
        {
            Tool.print("sql:" + sql);
            e.printStackTrace();
        }
        finally
        {
            closeStmt();
            closeConn();
        }
        return clob;
    }


    /**
     * 
     * 功能描述:返回Clob類型列,返回二進制流byte[] 
     * 方法名:getOneBlob 訪問類:DAO
     * 
     * @param sql
     *            語句
     * @param inputValue
     *            傳入參數數組
     * @param colName
     *            列名
     * @return byte[]
     * 
     */
    public byte[] getOneBlob(String sql, String[] inputValue, String colName)
    {
        byte[] blob = null;
        try
        {
            //this.conn = db.getConnection();
            stmt = conn.prepareStatement(sql);
            for (int i = 0; i < inputValue.length; i++)
            {
                stmt.setString(i + 1, inputValue[i]);
            }
            rs = stmt.executeQuery();
            while (rs.next())
            {
                blob = ((OracleResultSet) rs).getBytes(colName);
                if (rs.wasNull())
                {
                    blob = null;
                }
            }
            closeStmt();
        }
        catch (SQLException e)
        {
            Tool.print("sql:" + sql);
            e.printStackTrace();
        }
        finally
        {
            closeStmt();
            closeConn();
        }


        return blob;


    }


    /**
     * 
     * 功能描述:得到連接池 
     * 
     * @return Connection
     */
    public Connection getConn()
    {
       
        return conn;
    }
    
    public Connection getDBConn()
    {
 
        return conn;
    }


    public String getSplitSignOne()
    {
        return splitSignOne;
    }


    public String getSplitSignTwo()
    {
        return splitSignTwo;
    }


    /**
     * 
     * 功能描述:得到sql語句總數 
     * 
     * @param sql
     *            語句
     * @return String
     * 
     */
    public String getSQLCount(String sql)
    {
        String sql_temp = "select count(*) count from ( " + sql + " )";
        return sql_temp;
    }


    /**
     * 獲得表table的row列的最大值 to_number alreadly row列沒有string 值通常爲自動增長的int int
     * max=dao.getTableMaxCountRow("car_use","c_id");
     * 
     * @param table
     *            表名
     * @param row
     *            字段名
     * @return int
     */
    public int getTableMaxCountRow(String table, String row)
    {
        String sql = "select max(to_number(" + row + ")) max from " + table;
        String max = this.getOneRs(sql);
        return Integer.parseInt(max);
    }


    /**
     * 查詢在table表裏 row列中 value值是否存在 存在返回true 不存在返回false
     * boolean=dao.checkBeing("car_info","c_no","AA"); *
     * 
     * @param table
     *            表名
     * @param row
     *            字段
     * @param value
     *            參數
     * @return boolean
     */
    public boolean checkBeing(String table, String row, String value)
    {
        String sql = "select * from " + table + " where " + row + "=?";
        boolean bool = true;
        // ResultSet rs = this.getRS(sql, new String[] { value });// 查詢出結果
        List<HashMap<String, String>> list = this.getList(sql, new String[] { value }, new String[0]);
        if (list.size() > 0)// 有next說明存在這一列
            bool = true;
        else
            bool = false;


        return bool;
    }


    /**
     * 刪除表table中 row列值爲value的列 WARNING:注意此列是否是唯一(del all)}
     * dao.deleteTableRow("car_info","c_no","AA");
     * 
     * @param table
     *            表名
     * @param row
     *            字段名
     * @param value
     *            參數
     * @return boolean
     */
    public boolean deleteTableRow(String table, String row, String value)
    {
        boolean bool = true;
        String sql = "delete " + table + " where " + row + "=?";
        bool = this.runUpdate(sql, new String[] { value });
        return bool;
    }


    /**
     * 插入表數據 已經處理危險字符 直接傳入字符串即可
     * 
     * String table = "car_info"; String[] row = new String[] { "c_no",
     * "c_name", "c_MASS" }; String[] value = new String[] { "vvvvvvAAAA",
     * "sssss", "52" }; int [] length=new int[]{20,50,80};
     * 
     * dao.insert(table, row, value,length);
     * 
     * @param table
     *            表名字
     * @param row
     *            待插入的列 into(c_no,c_name)
     * @param value
     *            待插入的值 "AAA" "VVV"
     * @param length
     *            length 字符串長度
     * @return boolean
     */
    public boolean insert(String table, String[] row, String[] value, int[] length)
    {
        boolean bool = true;
        if (row.length != value.length || row.length != length.length || value.length != length.length)
        {
            bool = false;
            Tool.print("參數錯誤:插入表數據的時候列和值不對應!!!");
            return false;
        }
        String sql1 = "insert into " + table + "(";
        String sql2 = "";
        for (int i = 0; i < row.length; i++)
        {// 循環設置插入列
            if (i != 0)
                sql2 = sql2 + ",";
            sql2 = sql2 + row[i];
        }
        String sql3 = ") values(";
        String sql4 = "";
        String sql5 = ")";
        for (int i = 0; i < value.length; i++)// 循環設置值
        {
            value[i] = StringFormat.cleanInputText(value[i], length[i]);// 字長等於輸入的長度


            if (i != 0)
                sql4 = sql4 + ",";
            sql4 = sql4 + "?";
        }
        String consequence = sql1 + sql2 + sql3 + sql4 + sql5;
        bool = this.runUpdate(consequence, value);


        return bool;
    }


    /**
     * 重寫 have自動增長列 由自動增長放在row("第一個")第一個 已經處理危險字符 直接傳入字符串即可 插入表數據
     * 
     * 
     * String table = "car_info"; String[] row = new String[] { "c_id",
     * "c_name", "c_MASS" }; String[] value = new String[] { "aaa.net", "sssss",
     * "52" }; int [] length=new int[]{20,50,80};
     * 
     * dao.insert(table, row, value,length);
     * 
     * @param table
     *            表名字
     * @param row
     *            待插入的列 into(c_no,c_name)
     * @param value
     *            待插入的值 "AAA" "VVV"
     * @param length
     *            length 字符串長度
     * @return boolean
     */
    public boolean insertAuto(String table, String[] row, String[] value, int[] length)
    {
        boolean bool = true;
        if (row.length != value.length || row.length != length.length || value.length != length.length)
        {
            bool = false;
            Tool.print("參數錯誤:插入表數據的時候列和值不對應!!!");
            return false;
        }
        String sql1 = "insert into " + table + "(";
        String sql2 = "";
        for (int i = 0; i < row.length; i++)
        {// 循環設置插入列
            if (i != 0)
                sql2 = sql2 + ",";
            sql2 = sql2 + row[i];
        }
        String sql3 = ") values(";
        String sql4 = "";
        String sql5 = ")";
        for (int i = 0; i < value.length; i++)// 循環設置值
        {
            value[i] = StringFormat.cleanInputText(value[i], length[i]);// 字長等於輸入的長度


            if (i != 0)
                sql4 = sql4 + ",";
            sql4 = sql4 + "?";
        }
        String consequence = sql1 + sql2 + sql3 + sql4 + sql5;
        consequence = consequence.replaceFirst("\\?", value[0]);
        String newvalue[] = new String[(value.length - 1)];
        for (int i = 1; i < value.length; i++)
        {
            newvalue[(i - 1)] = value[i];
        }
        // Tool.print(consequence);
        bool = this.runUpdate(consequence, newvalue);


        return bool;
    }


    /**
     * 重寫插入
     * 
     * @param table
     *            表名
     * @param row
     *            字段數組
     * @param value
     *            參數數組
     * @return boolean
     */
    public boolean insert(String table, String[] row, String value[])
    {
        boolean bool = true;
        if (row.length != value.length)
        {
            bool = false;
            Tool.print("參數錯誤:插入表數據的時候列和值不對應!!!");
            return false;
        }
        String sql1 = "insert into " + table + "(";
        String sql2 = "";
        for (int i = 0; i < row.length; i++)
        {// 循環設置插入列
            if (i != 0)
                sql2 = sql2 + ",";
            sql2 = sql2 + row[i];
        }
        String sql3 = ") values(";
        String sql4 = "";
        String sql5 = ")";
        for (int i = 0; i < value.length; i++)// 循環設置值
        {
            value[i] = StringFormat.cleanInputText(value[i], value[i].length());// 字長等於輸入的長度
            if (i != 0)
                sql4 = sql4 + ",";
            sql4 = sql4 + "?";
        }
        String consequence = sql1 + sql2 + sql3 + sql4 + sql5;
        bool = this.runUpdate(consequence, value);
        // Tool.print(consequence);
        return bool;
    }


    /**
     * 
     * 功能描述:檢測一個表是否存在 
     * 訪問類:BasicDAO 返回類型:boolean
     * 
     * @param tableName
     * @return boolean
     */
    public boolean tableExist(String tableName)
    {
        // Tool.print("檢測表" + tableName + "是否存在...");
        String sql = "select tname from tab where tname =?";
        boolean res = false;
        String tName = getOneRs(sql, new String[] { tableName.toUpperCase() }, "tname");
        if (!Tool.isNull(tName))
        {
            res = true;
            // Tool.print("表" + tableName + "已存在");
        }
        else
        {
            Tool.print("表" + tableName + "不存在");
        }
        // Tool.print("檢測表" + tableName + "是否存在完畢!");
        return res;
    }


    /**
     * 
     * 功能描述:檢測表中的一個列 
     * 訪問類:BasicDAO 返回類型:void
     * 
     * @param tableName
     * @param columName
     * @param addColumSql
     * @param addColumCommentSql
     * @param index
     */
    public void checkColumn(String tableName, String columName, String addColumSql, String addCommentSql, int index)
    {
        String sql = "select count(*) count from col where tname=? and cname=? ";
        int lxidCount = Integer.parseInt(getOneRs(sql,
                new String[] { tableName.toUpperCase(), columName.toUpperCase() }, "count"));
        if (lxidCount == 0)
        {
            Tool.print("表" + tableName + "缺少列:" + columName + ",開始添加...");
            runUpdate(addColumSql);
            runUpdate(addCommentSql);
            Tool.print("表" + tableName + "的列:" + columName + "添加完畢!");
        }
    }


    /**
     * 
     * 功能描述:檢測表中的一個列
     * 訪問類:BasicDAO 返回類型:void
     * 
     * @param tableName
     * @param columName
     * @param addColumSql
     * @param addColumCommentSql
     * @param index
     */
    public void checkColumn(String tableName, String columName, String addColumSql, String addCommentSql)
    {
        String sql = "select count(*) count from col where tname=? and cname=? ";
        int lxidCount = Integer.parseInt(getOneRs(sql,
                new String[] { tableName.toUpperCase(), columName.toUpperCase() }, "count"));
        if (lxidCount == 0)
        {
            Tool.print("表" + tableName + "缺少列:" + columName + ",開始添加...");
            runUpdate(addColumSql);
            runUpdate(addCommentSql);
            Tool.print("表" + tableName + "的列:" + columName + "添加完畢!");
        }
    }


    /**
     * 
     * 功能描述:檢測表中的一個列 
     * 訪問類:BasicDAO 返回類型:void
     * 
     * @param tableName
     * @param columName
     * @param addColumSql
     * @param addColumCommentSql
     * @param index
     */
    public void checkColumn(String tableName, String columName, String addColumSql)
    {
        String sql = "select count(*) count from col where tname=? and cname=? ";
        int lxidCount = Integer.parseInt(getOneRs(sql,
                new String[] { tableName.toUpperCase(), columName.toUpperCase() }, "count"));
        if (lxidCount == 0)
        {
            Tool.print("表" + tableName + "缺少列:" + columName + ",開始添加...");
            if (!Tool.isNull(addColumSql))
            {
                runUpdate(addColumSql);
            }
            else
            {
                Tool.print("找不到列" + columName + "的添加語句!");
            }
            Tool.print("表" + tableName + "的列:" + columName + "添加完畢!");
        }
    }


    /**
     * 
     * 功能描述:創建表 
     * 訪問類:BasicDAO 返回類型:void
     * 
     * @param createTableSql
     * @param addColumCommentSql
     */
    public void createTable(String tableName, String createTableSql, String[] addCommentSql)
    {
        Tool.print("表" + tableName + "不存在,開始創建...");
        runUpdate(createTableSql);
        Tool.print("執行表" + tableName + "的創建完畢!");
        Tool.print("開始添加表" + tableName + "的comment");
        for (int i = 0; i < addCommentSql.length; i++)
        {
            runUpdate(addCommentSql[i]);
        }
        Tool.print("添加表" + tableName + "的comment完畢");
    }


    /**
     * 
     * 功能描述:檢測表中的所有列 
     * 訪問類:BasicDAO 返回類型:void
     * 
     * @param tableName
     * @param column
     * @param addColumSql
     * @param addColumCommentSql
     * @param index
     */
    public void checkAllColumn(String tableName, String[] column, String[] addColumSql, String[] addCommentSql,
            String[] primaryKey, String primarySql, int[] index)
    {
        Tool.print("開始檢測" + tableName + "的列信息...");
        for (int i = 0; i < column.length; i++)
        {
            checkColumn(tableName.toUpperCase(), column[i].toUpperCase(), addColumSql[i], addCommentSql[i + 1]);
        }
        checkPrimaryKey(tableName.toUpperCase(), primaryKey, primarySql);
        Tool.print("表" + tableName + "的列信息檢測完畢!");
    }


    /**
     * 
     * 功能描述:檢測表中的所有列 
     * 訪問類:BasicDAO 返回類型:void
     * 
     * @param tableName
     * @param column
     * @param addColumSql
     * @param addColumCommentSql
     * @param index
     */
    public void checkAllColumn(String tableName, String[] column, String[] addColumSql, String[] addCommentSql,
            String[] primaryKey, String primarySql)
    {
        // Tool.print("開始檢測" + tableName + "的列信息...");
        if (addCommentSql.length == column.length + 1)
        {
            for (int i = 0; i < column.length; i++)
            {
                if (addColumSql.length > i && addCommentSql.length >= (i + 1))
                {
                    checkColumn(tableName.toUpperCase(), column[i].toUpperCase(), addColumSql[i], addCommentSql[i + 1]);
                }
            }
        }
        else
        {
            Tool.print(tableName + "的comment語句信息不正確,所以檢測列信息而不執行comment 語句");
            if (addColumSql != null && addColumSql.length == column.length)
            {
                for (int i = 0; i < column.length; i++)
                {
                    checkColumn(tableName.toUpperCase(), column[i].toUpperCase(), addColumSql[i]);
                }
            }
            else
            {
                Tool.print("增加列的語句信息不正確,所以檢測列信息而不執行增加列的語句");
                for (int i = 0; i < column.length; i++)
                {
                    checkColumn(tableName.toUpperCase(), column[i].toUpperCase(), "");
                }
            }
        }
        checkPrimaryKey(tableName, primaryKey, primarySql);
        // Tool.print("表" + tableName + "的列信息檢測完畢!");
    }


    public void checkPrimaryKey(String tableName, String[] primaryKey, String primarySql)
    {
        // Tool.print("開始檢測" + tableName + "的主鍵信息...");
        String sql = "select col.column_name column_name from user_constraints con,user_cons_columns col where "
                + "con.constraint_name=col.constraint_name and con.constraint_type='P' and col.table_name=?";
        List<HashMap<String, String>> list = getList(sql, new String[] { tableName.toUpperCase() },
                new String[] { "column_name" });
        String[] tempPrimary = Tool.listToArray(list, "column_name");
        if (primaryKey == null || primaryKey.length == 0)
        {
            // Tool.print("表" + tableName + "被設計爲沒有主鍵!");
        }
        else
        {
            if (tempPrimary != null && (tempPrimary.length != primaryKey.length))
            {
                Tool.print("表" + tableName + "的主鍵信息不正確!下面開始修正...");
                if (tempPrimary != null && tempPrimary.length > 0)
                {
                    runUpdate("alter table " + tableName + " drop primary key ");
                }
                if (!Tool.isNull(primarySql))
                {
                    Tool.print("給表" + tableName + "添加主鍵...");
                    runUpdate(primarySql);
                    Tool.print("表" + tableName + "添加主鍵完畢!");
                }
                // Tool.print("表" + tableName + "的主鍵修正完畢");
            }
            else
            {
                // Tool.print("表" + tableName + "的主鍵信息完整!");
            }
        }
        // Tool.print("檢測" + tableName + "的主鍵信息完畢");
    }


    /**
     * 
     * 功能描述:判斷一個實體是否存在例如觸發器等 
     * 訪問類:BasicDAO 返回類型:boolean
     * 
     * @param objType
     * @param objName
     * @return boolean
     */
    public boolean objExist(String objType, String objName)
    {
        boolean flag = false;
        String sql = "select count(*)count from user_objects where object_type = ? and object_name = ?";
        int count = Integer.parseInt(getOneRs(sql, new String[] { objType.toUpperCase(), objName.toUpperCase() },
                "count"));
        if (count > 0)
        {
            flag = true;
        }
        return flag;
    }




    public PreparedStatement getStmt()
    {
        return stmt;
    }


    public void setStmt(PreparedStatement stmt)
    {
        this.stmt = stmt;
    }


}
發佈了62 篇原創文章 · 獲贊 8 · 訪問量 24萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章