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

📄 jdbchandler.java

📁 jConfig,JAVA读取XML的开源项目
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
        
        String [] variableNameArray = getStringArray(configuration.getVariables());
        Hashtable oldVariables = readOldVariables(confoid);
        for (int i = 0; i < variableNameArray.length; i++) {
            String variableName = variableNameArray[i];
            long varoid= storeVariable(confoid, variableName, configuration.getVariable(variableName));
            oldVariables.remove(new Long(varoid));
        }
        Enumeration enumVar = oldVariables.keys();
        while (enumVar.hasMoreElements()) {
            Long varoid = (Long)enumVar.nextElement();
            deleteVariable(varoid.longValue(),oldVariables.get(varoid).toString());
        }
        
        closeConnection();
    }
    
    
    private void closeConnection() throws ConfigurationManagerException {
        try {
            con.close();
        } catch (SQLException e) {
            ErrorReporter.getErrorHandler().reportError("Problem closing database connection.",e);
            throw new ConfigurationManagerException("Problem closing database connection. " + e.getMessage());
        }
    }
    
    
    /**
     * @param catid
     * @param category
     */
    private void storeProperties(long catid, Category category) throws ConfigurationManagerException {
        Enumeration enm = category.getProperties().keys();
        Hashtable oldProperties = readOldProperties(catid);
        while(enm.hasMoreElements()) {
            String propertyName = (String)enm.nextElement();
            long propoid = storeProperty(catid, propertyName, category.getProperty(propertyName));
            oldProperties.remove(new Long(propoid));
        }
        Enumeration enumeration = oldProperties.keys();
        
        while (enumeration.hasMoreElements()) {
            Long propoid = (Long)enumeration.nextElement();
            deleteProperty(propoid.longValue(),oldProperties.get(propoid).toString());
        }
        
    }
    
    
    /**
     * @param propertyName
     * @param string
     */
    private long storeProperty(long catoid, String propertyName, String propertyValue) throws ConfigurationManagerException {
        long propoid = Long.MIN_VALUE;
        try {
            PreparedStatement pstmt = con.prepareStatement("SELECT OID FROM T_PROPERTY WHERE CATEGORY_OID = ? AND NAME= ?");
            pstmt.setLong(1, catoid);
            pstmt.setString(2, propertyName);
            ResultSet result = pstmt.executeQuery();
            while(result.next()) {
                propoid = result.getLong("OID");
                updateProperty(propoid, propertyValue);
            }
            result.close();
            pstmt.close();
        } catch (SQLException e) {
            ErrorReporter.getErrorHandler().reportError("Problem storing configuration in database.",e);
            throw new ConfigurationManagerException("Problem storing configuration in database. " + e.getMessage());
        }
        if(propoid == Long.MIN_VALUE) {
            createNewProperty(catoid, propertyName, propertyValue);
            return storeProperty(catoid, propertyName, propertyValue);
        }
        return propoid;
    }
    
    
    /**
     * @param propoid
     * @param propertyValue
     */
    private void updateProperty(long propoid, String propertyValue) throws ConfigurationManagerException {
        try {
            PreparedStatement pstmt = con.prepareStatement("UPDATE T_PROPERTY SET VALUE=? WHERE OID=?");
            pstmt.setString(1, propertyValue);
            pstmt.setLong(2, propoid);
            pstmt.execute();
            pstmt.close();
        } catch (SQLException e) {
            ErrorReporter.getErrorHandler().reportError(propertyValue + " variable value could not be stored.",e);
            throw new ConfigurationManagerException(propertyValue + " variable value could not be stored." + e.getMessage());
        }
        
    }
    
    
    /**
     * @param catoid
     * @param propertyName
     * @param propertyValue
     */
    private void createNewProperty(long catoid, String propertyName, String propertyValue) throws ConfigurationManagerException {
        try {
            PreparedStatement pstmt = con.prepareStatement("INSERT INTO T_PROPERTY (NAME, VALUE, CATEGORY_OID) VALUES (?, ?, ?)");
            pstmt.setString(1, propertyName);
            pstmt.setString(2, propertyValue);
            pstmt.setLong(3, catoid);
            pstmt.execute();
            pstmt.close();
        } catch (SQLException e) {
            ErrorReporter.getErrorHandler().reportError(propertyValue + " property value could not be stored.",e);
            throw new ConfigurationManagerException(propertyName + " property could not be stored." + e.getMessage());
        }
    }
    
    
    /**
     * @param confoid
     * @param variableName
     */
    private long storeVariable(long confoid, String variableName, String variableValue) throws ConfigurationManagerException {
        long varoid = Long.MIN_VALUE;
        try {
            PreparedStatement pstmt = con.prepareStatement("SELECT OID FROM T_VARIABLE WHERE CONFIGURATION_OID = ? AND NAME= ?");
            pstmt.setLong(1, confoid);
            pstmt.setString(2, variableName);
            ResultSet result = pstmt.executeQuery();
            while(result.next()) {
                varoid = result.getLong("OID");
                updateVariable(varoid, variableValue);
            }
            result.close();
            pstmt.close();
        } catch (SQLException e) {
            ErrorReporter.getErrorHandler().reportError(variableName + " variable value could not be stored.",e);
            throw new ConfigurationManagerException("Problem sotring variable in database. " + e.getMessage());
        }
        if(varoid == Long.MIN_VALUE) {
            createNewVariable(confoid, variableName, variableValue);
            return storeVariable(confoid, variableName, variableValue);
        }
        return varoid;
    }
    
    
    /**
     * @param catoid
     * @param variableValue
     */
    private void updateVariable(long varoid, String variableValue) throws ConfigurationManagerException {
        try {
            PreparedStatement pstmt = con.prepareStatement("UPDATE T_VARIABLE SET VALUE=? WHERE OID=?");
            pstmt.setString(1, variableValue);
            pstmt.setLong(2, varoid);
            pstmt.execute();
            pstmt.close();
        } catch (SQLException e) {
            ErrorReporter.getErrorHandler().reportError(variableValue + " variable value could not be stored.",e);
            throw new ConfigurationManagerException(variableValue + " variable value could not be stored." + e.getMessage());
        }
    }
    
    
    /**
     * @param confoid
     * @param variableName
     */
    private void createNewVariable(long confoid, String variableName, String variableValue) throws ConfigurationManagerException {
        try {
            PreparedStatement pstmt = con.prepareStatement("INSERT INTO T_VARIABLE (NAME, VALUE, CONFIGURATION_OID) VALUES (?, ?, ?)");
            pstmt.setString(1, variableName);
            pstmt.setString(2, variableValue);
            pstmt.setLong(3, confoid);
            pstmt.execute();
            pstmt.close();
        } catch (SQLException e) {
            ErrorReporter.getErrorHandler().reportError(variableValue + " variable value could not be stored.",e);
            throw new ConfigurationManagerException(variableName + " variable could not be stored." + e.getMessage());
        }
    }
    
    
    /**
     * @param confoid
     * @param categoryName
     */
    private long storeCategory(long confoid, String categoryName) throws ConfigurationManagerException {
        long catoid = Long.MIN_VALUE;
        try {
            PreparedStatement pstmt = con.prepareStatement("SELECT OID FROM T_CATEGORY WHERE CONFIGURATION_OID = ? AND NAME = ?");
            pstmt.setLong(1, confoid);
            pstmt.setString(2, categoryName);
            ResultSet result = pstmt.executeQuery();
            while(result.next()) {
                catoid = result.getLong("OID");
            }
            result.close();
            pstmt.close();
        } catch (SQLException e) {
            ErrorReporter.getErrorHandler().reportError("Problem storing category " + categoryName + ". ",e);
            throw new ConfigurationManagerException("Problem storing category " + categoryName + ". " + e.getMessage());
        }
        if(catoid == Long.MIN_VALUE) {
            createNewCategory(confoid, categoryName);
            return storeCategory(confoid, categoryName);
        }
        return catoid;
    }
    /**
     * @param confoid
     * @param categoryName
     */
    private void deleteCategory(long confoid, String categoryName) throws ConfigurationManagerException {
        try {
            PreparedStatement pstmt = con.prepareStatement("DELETE FROM T_CATEGORY WHERE CONFIGURATION_OID = ? AND NAME = ?");
            pstmt.setLong(1, confoid);
            pstmt.setString(2, categoryName);
            pstmt.executeUpdate();
            pstmt.close();
        } catch (SQLException e) {
            ErrorReporter.getErrorHandler().reportError("Problem deleting category " + categoryName + ". ",e);
            throw new ConfigurationManagerException("Problem deleting category " + categoryName + ". " + e.getMessage());
        }
    }
    /**
     * @param confoid
     * @param categoryName
     */
    private void deleteProperty(long propoid, String propertyName) throws ConfigurationManagerException {
        try {
            PreparedStatement pstmt = con.prepareStatement("DELETE FROM T_PROPERTY WHERE OID = ? AND NAME = ?");
            pstmt.setLong(1, propoid);
            pstmt.setString(2, propertyName);
            pstmt.executeUpdate();
            pstmt.close();
        } catch (SQLException e) {
            ErrorReporter.getErrorHandler().reportError("Problem deleting property " + propertyName + ". ",e);
            throw new ConfigurationManagerException("Problem deleting property " + propertyName + ". " + e.getMessage());
        }
    }
    /**
     *
     * @param varoid
     * @param variableName
     * @throws ConfigurationManagerException
     */
    private void deleteVariable(long varoid, String variableName) throws ConfigurationManagerException {
        try {
            PreparedStatement pstmt = con.prepareStatement("DELETE FROM T_VARIABLE WHERE OID = ? ");
            pstmt.setLong(1, varoid);
            pstmt.executeUpdate();
            pstmt.close();
        } catch (SQLException e) {
            ErrorReporter.getErrorHandler().reportError("Problem deleting variable " + variableName + ". ",e);
            throw new ConfigurationManagerException("Problem deleting variable " + variableName + ". " + e.getMessage());
        }
    }
    
    
    /**
     * @param confoid
     * @param categoryName
     */
    private void createNewCategory(long confoid, String categoryName) throws ConfigurationManagerException {
        try {
            PreparedStatement pstmt = con.prepareStatement("INSERT INTO T_CATEGORY (NAME, CONFIGURATION_OID) VALUES (?, ?)");
            pstmt.setString(1, categoryName);
            pstmt.setLong(2, confoid);
            pstmt.execute();
            pstmt.close();
        } catch (SQLException e) {
            ErrorReporter.getErrorHandler().reportError("Problem storing category " + categoryName + ". ",e);
            throw new ConfigurationManagerException(categoryName + " category could not be stored." + e.getMessage());
        }
    }
    
    
    /**
     * @param string
     */
    private long storeConfiguration(String configurationName) throws ConfigurationManagerException {
        long oid = Long.MIN_VALUE;
        try {
            PreparedStatement pstmt = con.prepareStatement("SELECT OID FROM T_CONFIGURATION WHERE NAME = ?");
            pstmt.setString(1, configurationName);
            ResultSet result = pstmt.executeQuery();
            while(result.next()) {
                oid = result.getLong("OID");
            }
            result.close();
            pstmt.close();
        } catch (SQLException e) {
            ErrorReporter.getErrorHandler().reportError("Problem storing configuration in database.",e);
            throw new ConfigurationManagerException("Problem storing configuration in database. " + e.getMessage());
        }
        if(oid == Long.MIN_VALUE) {
            createNewConfiguration(configurationName);
            return storeConfiguration(configurationName);
        }
        return oid;
    }
    
    
    /**
     * @param configurationName
     */
    private void createNewConfiguration(String configurationName) throws ConfigurationManagerException {
        try {
            PreparedStatement pstmt = con.prepareStatement("INSERT INTO T_CONFIGURATION (NAME) VALUES (?)");
            pstmt.setString(1, configurationName);
            pstmt.execute();
            pstmt.close();
        } catch (SQLException e) {
            ErrorReporter.getErrorHandler().reportError("Problem storing configuration in database.",e);
            throw new ConfigurationManagerException(configurationName + " configuration could not be stored." + e.getMessage());
        }
    }
    
    private String [] getStringArray(Map categories) {
        Set allCategories = categories.keySet();
        return (String[]) allCategories.toArray(new String[0]);
    }


	public Configuration load(String configurationName, ConfigurationParser parser) throws ConfigurationManagerException {
		throw new ConfigurationManagerException("Using a specific parser with this handler is not supported");
	}
    
}

⌨️ 快捷键说明

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