首页 > 代码库 > (转)获取指定数据库和用户的所有表表名

(转)获取指定数据库和用户的所有表表名

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());   }}