Java tutorial
/* * Copyright 2009 by pactera.edg.am Corporation. Address:HePingLi East Street No.11 * 5-5, BeiJing, * * All rights reserved. * * This software is the confidential and proprietary information of pactera.edg.am * Corporation ("Confidential Information"). You shall not disclose such * Confidential Information and shall use it only in accordance with the terms * of the license agreement you entered into with pactera.edg.am. */ package com.pactera.edg.am.metamanager.extractor.adapter.extract.jicai.db.impl; import java.sql.Connection; import java.sql.DatabaseMetaData; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import java.util.Properties; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.jdbc.BadSqlGrammarException; import org.springframework.jdbc.core.JdbcTemplate; import com.pactera.edg.am.metamanager.extractor.adapter.extract.jicai.db.JiCaiHSqlDBDao; import com.pactera.edg.am.metamanager.extractor.adapter.extract.jicai.db.IJiCaiDBExtractService; import com.pactera.edg.am.metamanager.extractor.adapter.extract.jicai.vo.ColumnVO; import com.pactera.edg.am.metamanager.extractor.adapter.extract.jicai.vo.SchemaVO; import com.pactera.edg.am.metamanager.extractor.adapter.extract.jicai.vo.SystemVO; import com.pactera.edg.am.metamanager.extractor.adapter.extract.jicai.vo.TableVO; import com.pactera.edg.am.metamanager.extractor.bo.ExtractorLogLevel; import com.pactera.edg.am.metamanager.extractor.util.AdapterExtractorContext; public abstract class AbstractJiCaiDBExtractService implements IJiCaiDBExtractService { private Log log = LogFactory.getLog(AbstractJiCaiDBExtractService.class); /** * ?????DB? */ protected Properties properties = AdapterExtractorContext.getInstance().getParameters(); private JiCaiHSqlDBDao hsqlDao; protected JiCaiHSqlDBDao getHsqlJdbcTemplate() { if (hsqlDao == null) { hsqlDao = new JiCaiHSqlDBDaoImpl(); return hsqlDao; } else { return hsqlDao; } } /** * ?ODS?SQL?? */ protected String SRC_NME = null; private JdbcTemplate jdbcTemplate; private DatabaseMetaData metaData; protected JdbcTemplate getJdbcTemplate() { return jdbcTemplate; } public void setJdbcTemplate(JdbcTemplate jdbcTemplate) { this.jdbcTemplate = jdbcTemplate; } protected DatabaseMetaData getMetadata() { return metaData; } public final SystemVO getSysInfos(String schema, int start, int limit, boolean flag) throws Exception { Connection conn = null; try { conn = jdbcTemplate.getDataSource().getConnection(); metaData = conn.getMetaData(); log.info("???!"); return internalGetSchemas(schema, start, limit, flag); } catch (Exception e) { log.error("DB??SQL!DB????!", e); AdapterExtractorContext.addExtractorLog(ExtractorLogLevel.ERROR, "DB??SQL!DB????!"); if (conn != null) { conn.close(); } throw e; } finally { // ??,??? if (conn != null) { conn.close(); } } } /** * ?? */ private SystemVO internalGetSchemas(String schema, int start, int limit, boolean flag) throws Exception { try { //?ODS? SRC_NME = properties.getProperty("SRC_NME"); SystemVO sysInfo = new SystemVO(); List<SchemaVO> schemasVO = new ArrayList<SchemaVO>(); //??? sysInfo.setSysName(properties.getProperty("sysName")); //??? sysInfo.seteSysName(properties.getProperty("ESysName")); //?schemaName???? SchemaVO schemasvo = new SchemaVO(); List<TableVO> tables = null; if (flag) { tables = getTables(schema, start, limit); } else { tables = new ArrayList<TableVO>(); } log.info("?:" + tables.size()); //?? List<ColumnVO> fields = getFields(schema, start, limit); log.info("?:" + fields.size()); schemasvo.setTables(tables); schemasvo.setFields(fields); schemasvo.setSchName(schema); schemasVO.add(schemasvo); sysInfo.setSchemas(schemasVO); return sysInfo; } catch (BadSqlGrammarException e) { if (e.getMessage().indexOf(signOfNoPrivilege()) > -1) { log.warn("???????:" + getSystemTableList().toString(), e); AdapterExtractorContext.addExtractorLog(ExtractorLogLevel.ERROR, "???????:" + getSystemTableList().toString()); } throw e; } } /** * ???? * * @return */ protected abstract String signOfNoPrivilege(); /** * ?? * * @return */ protected abstract List<String> getSystemTableList(); /** * ??? * @Title: getFields * @Description: TODO * @param @param schName * @param @return * @param @throws SQLException * @return List<FieldVO> * @throws */ protected abstract List<ColumnVO> getFields(String schName, int start, int limit) throws SQLException; /** * ??? * @Title: getTables * @Description: TODO * @param @param schName * @param @return * @param @throws SQLException * @return List<TableVO> * @throws */ protected abstract List<TableVO> getTables(String schName, int start, int limit) throws SQLException; }