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

复制代码
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());
  
 }
}
复制代码
posted @   xingoo  阅读(4092)  评论(0编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
点击右上角即可分享
微信分享提示