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

📄 pkcache.java

📁 利用java反射机制
💻 JAVA
字号:
// FIXME CONCURRENT ACCESS TO DRIVER, URL ETC...??package org.julp;import java.util.*;    /**      * Retrieves and caches PrimaryKeys form DatabaseMetaData               */public class PKCache implements java.io.Serializable, Cloneable {          private PKCache() {        if (Boolean.getBoolean("debug-julp") || Boolean.getBoolean("debug-" + getClass().getName())){            debug = true;        }            }        private static PKCache instance = null;    /*PK for each table */    protected Map pkCache = new HashMap();        protected DBServices dbServices = null;    protected String dataSourceName = null;    protected String dbUrl = null;    private String userName = null;    private String password = null;    private String driverName = null;    private Properties optionalConnectionProp = null;    protected boolean useDataSource = true;    protected boolean debug = false;        public static PKCache getInstance() {        // Not completely thread-safe, but good enough?        if (instance == null) {            instance = new PKCache();        }        return instance;    }        /*     *This method allows to set columns for a table PrimaryKey without Database connection, etc.     *     * Make sure to use format:  catalog + "." + schema + "." + table + "." + column     *   or schema + "." + table + "." + column          */    public synchronized void setPrimaryKey(String catalog, String schema, String table, List pk){        String tableId = null;        if (catalog != null && !catalog.trim().equals("")){            tableId = catalog + "." + schema + "." + table;        }else if (schema != null && !schema.trim().equals("")){            tableId = schema + "." + table;        }else if (table != null && !table.trim().equals("")){            tableId = table;        }else{            throw new IllegalArgumentException("setPrimaryKey(): missing all table identifiers");        }        if (pk == null || pk.size() == 0){            throw new IllegalArgumentException("setPrimaryKey(): PK list is empty");        }        if (debug) System.out.println("julp ============= " + new java.util.Date() + " " + this.getClass() + "::setPrimaryKey()::tableId: " + tableId + " pk: " + pk);               pkCache.put(tableId, pk);    }        public synchronized List getPrimaryKey(String catalog, String schema, String table){        String tableId = null;        if (catalog != null && !catalog.trim().equals("")){            tableId = catalog + "." + schema + "." + table;        }else if (schema != null && !schema.trim().equals("")){            tableId = schema + "." + table;        }else if (table != null && !table.trim().equals("")){            tableId = table;        }else{            throw new IllegalArgumentException("setPrimaryKey(): missing all table identifiers");        }                List pkFields = (List) this.pkCache.get(tableId);        if (debug) System.out.println("julp ============= " + new java.util.Date() + " " + this.getClass() + "::getPrimaryKey()::tableId: " + tableId + " pkFields: " + pkFields);        if (pkFields != null) {            return pkFields; //it's already cached        }else{            pkFields = new ArrayList();        }        java.sql.ResultSet pkInfo = null;        try{            if (dbServices == null){                dbServices = new DBServices();            }                        if (this.isUseDataSource()){                if (dbServices.getDataSourceName() == null || !dbServices.getDataSourceName().equals(this.dataSourceName)){                    dbServices.release(true);                    dbServices.setDataSourceName(this.dataSourceName);                }            }else{                dbServices.setUseDataSource(false);                if (dbServices.getDriverName() == null || !dbServices.getDriverName().equals(this.driverName) ||                    dbServices.getDbUrl() == null || !dbServices.getDbUrl().equals(this.dbUrl) ||                    dbServices.getUserName() == null || !dbServices.getUserName().equals(this.userName) ||                    dbServices.getPassword() == null || !dbServices.getPassword().equals(this.password)){                    dbServices.release(true);                    dbServices.setConnectionProperties(this.optionalConnectionProp);                    dbServices.setDriverName(this.driverName);                    dbServices.setDbUrl(this.dbUrl);                    dbServices.setUserName(this.userName);                    dbServices.setPassword(this.password);                }            }            java.sql.DatabaseMetaData dbmd = dbServices.getConnection().getMetaData();            if (debug) System.out.println("julp ============= " + new java.util.Date() + " " + this.getClass() + "::getPrimaryKey()::DatabaseMetaData: " + dbmd);            if (debug) System.out.println("julp ============= " + new java.util.Date() + " " + this.getClass() + "::getPrimaryKey()::catalog: " + catalog);            if (debug) System.out.println("julp ============= " + new java.util.Date() + " " + this.getClass() + "::getPrimaryKey()::schema: " + schema);            if (debug) System.out.println("julp ============= " + new java.util.Date() + " " + this.getClass() + "::getPrimaryKey()::table: " + table);            pkInfo = dbmd.getPrimaryKeys(catalog, schema, table);            if (debug) System.out.println("julp ============= " + new java.util.Date() + " " + this.getClass() + "::getPrimaryKey()::pkInfo: " + pkInfo);            while(pkInfo.next()){                if (debug) System.out.println("julp ============= " + new java.util.Date() + " " + this.getClass() + "::getPrimaryKey()::pkInfo.next()");                String columnName = pkInfo.getString(4);                pkFields.add(tableId + "." + columnName);            }            if (debug) System.out.println("julp ============= " + new java.util.Date() + " " + this.getClass() + "::getPrimaryKey()::pkFields: " + pkFields);            if (pkFields.isEmpty()){                throw new java.sql.SQLException("Cannot retrieve PrimaryKey info from DatabaseMetaData");            }        }catch (java.sql.SQLException sqle){            throw new RuntimeException(sqle);        }catch (Exception e){            throw new RuntimeException(e);        }finally{            try{                if (pkInfo != null) pkInfo.close();                dbServices.release(true);            }catch (java.sql.SQLException sqle){                sqle.printStackTrace();                throw new RuntimeException(sqle);            }        }        pkCache.put(tableId, pkFields);        return pkFields;    }    //    public void setMetaData(String objectClassName, MetaData metaData){//        metaDataMap.put(objectClassName, metaData);//    }//    //    public MetaData getMetaData(String objectClassName){//        return (MetaData) metaDataMap.get(objectClassName);//    }        /** Getter for property dbServices.     * @return Value of property dbServices.     *     */    public org.julp.DBServices getDbServices() {        return dbServices;    }        /** Setter for property dbServices.     * @param dbServices New value of property dbServices.     *     */    public void setDbServices(org.julp.DBServices dbServices) {        this.dbServices = dbServices;    }        /** Getter for property dataSourceName.     * @return Value of property dataSourceName.     *     */    public java.lang.String getDataSourceName() {        return dataSourceName;    }        /** Setter for property dataSourceName.     * @param dataSourceName New value of property dataSourceName.     *     */    public void setDataSourceName(java.lang.String dataSourceName) {        this.dataSourceName = dataSourceName;    }        /** Getter for property dbUrl.     * @return Value of property dbUrl.     *     */    public java.lang.String getDbUrl() {        return dbUrl;    }        /** Setter for property dbUrl.     * @param dbUrl New value of property dbUrl.     *     */    public void setDbUrl(java.lang.String dbUrl) {        this.dbUrl = dbUrl;    }        /** Getter for property userName.     * @return Value of property userName.     *     */    public java.lang.String getUserName() {        return userName;    }        /** Setter for property userName.     * @param userName New value of property userName.     *     */    public void setUserName(java.lang.String userName) {        this.userName = userName;    }        /** Getter for property password.     * @return Value of property password.     *     */    public java.lang.String getPassword() {        return password;    }        /** Setter for property password.     * @param password New value of property password.     *     */    public void setPassword(java.lang.String password) {        this.password = password;    }        /** Getter for property useDataSource.     * @return Value of property useDataSource.     *     */    public boolean isUseDataSource() {        return useDataSource;    }        /** Setter for property useDataSource.     * @param useDataSource New value of property useDataSource.     *     */    public void setUseDataSource(boolean useDataSource) {        this.useDataSource = useDataSource;    }        /** Getter for property driverName.     * @return Value of property driverName.     *     */    public java.lang.String getDriverName() {        return driverName;    }        /** Setter for property driverName.     * @param driverName New value of property driverName.     *     */    public void setDriverName(java.lang.String driverName) {        this.driverName = driverName;    }            /** Getter for property optionalConnectionProp.     * @return Value of property optionalConnectionProp.     *     */    public java.util.Properties getOptionalConnectionProp() {        return optionalConnectionProp;    }        /** Setter for property optionalConnectionProp.     * @param optionalConnectionProp New value of property optionalConnectionProp.     *     */    public void setOptionalConnectionProp(java.util.Properties optionalConnectionProp) {        this.optionalConnectionProp = optionalConnectionProp;    }    }

⌨️ 快捷键说明

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