標籤:style blog java color 檔案 資料
import java.io.IOException;import java.io.InputStream;import java.sql.Connection;import java.sql.DatabaseMetaData;import java.sql.ResultSet;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.loushang.persistent.jdbc.datasource.PropertyDataSourceFactoryImpl;public class DataBaseUtil { private static Log logger = LogFactory.getLog(DataBaseUtil.class); public static final String DATASOURCE_FILENAME = "datasource.properties"; //串連資料庫的資料來源檔案 public static final String DATASOURCE_URL = "dataSource.url"; //資料來源檔案裡url的key public static final String DATASOURCE_USERNAME = "dataSource.username"; //資料來源檔案裡使用者名稱的key /** * 讀取設定檔資訊 * @return Properties 設定檔資訊 */ public static Properties getProperties() { //InputStream in = ClassLoader.getSystemResourceAsStream(DATASOURCE_FILENAME); InputStream in = PropertyDataSourceFactoryImpl.class.getClassLoader() .getResourceAsStream(DATASOURCE_FILENAME); if (in == null) { in = Thread.currentThread().getContextClassLoader().getResourceAsStream(DATASOURCE_FILENAME); if(in == null){ logger.warn("Can not find the datasource config file ‘datasource.properties‘."); } } Properties properties = new Properties(); try { properties.load(in); } catch (IOException e) { logger.error("Error occurred when loading datasource config file.", e); } return properties; } /** * 讀取設定檔擷取串連資料庫的資料庫名 * @return String 資料庫名 */ public static String getDatabaseName() { String databaseName = ""; Properties p = getProperties(); String database = p.getProperty(DATASOURCE_URL); int startIndex = database.lastIndexOf(":"); databaseName = database.substring(startIndex+1, database.length()); return databaseName; } /** * 讀取設定檔擷取串連資料庫的使用者名稱 * @return String 使用者名稱 */ public static String getUserOfDatabase() { String user = ""; Properties p = getProperties(); user = p.getProperty(DATASOURCE_USERNAME); return user; } /** * 擷取指定資料庫和使用者的所有表名 * @param conn 串連資料庫物件 * @param user 使用者 * @param database 資料庫名 * @return */ public static List getAllTableNames(Connection conn, String user, String database) { List tableNames = new ArrayList(); if (conn != null) { try { DatabaseMetaData dbmd = conn.getMetaData(); // 表名列表 ResultSet rest = dbmd.getTables(database, null, null, new String[] { "TABLE" }); // 輸出 table_name while (rest.next()) { String tableSchem = rest.getString("TABLE_SCHEM"); if (user.equalsIgnoreCase(tableSchem)) { tableNames.add(rest.getString("TABLE_NAME")); } } } catch (SQLException e) { e.printStackTrace(); } } return tableNames; } public static void main(String [] args) { Connection conn = null; try{ /* String url="jdbc:oracle:thin:@10.*.*.*:1521:***"; Class.forName("oracle.jdbc.driver.OracleDriver"); conn = DriverManager.getConnection(url , "*" , "*");*/ DataSource datasource = DataSourceFactory.defaultFactory.getDataSource("dataSource"); conn = datasource.getConnection(); } catch (SQLException e1) { e1.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } List tables = getAllTableNames(conn, getUserOfDatabase(), getDatabaseName()); System.out.println(tables.size()); }}