(轉)擷取指定資料庫和使用者的所有表表名

來源:互聯網
上載者:User

標籤: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());   }}
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.