import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.net.URL; import java.net.URLClassLoader; import java.sql.*; import java.util.ArrayList; import java.util.List; import java.util.Properties; import java.sql.Driver; /** * JDBC驱动加载器和表信息获取器 */ public class JdbcTableLoader implements AutoCloseable { private Properties config; private Connection connection; private Class driverClass; public JdbcTableLoader(String configFile) throws IOException { loadConfig(configFile); } /** * 加载配置文件 */ private void loadConfig(String configFile) throws IOException { System.out.println("加载配置文件: " + new File(configFile).getAbsolutePath()); config = new Properties(); try (FileInputStream fis = new FileInputStream(configFile)) { config.load(fis); } } /** * 加载JDBC驱动 */ public void loadDriver() throws Throwable { String libDir = config.getProperty("jdbc.lib.dir"); String driverClassName = config.getProperty("jdbc.driver.class"); // 加载lib目录下的所有jar包 File libFolder = new File(libDir); if (!libFolder.exists() || !libFolder.isDirectory()) { throw new RuntimeException("驱动目录不存在: " + libDir); } List urls = new ArrayList<>(); File[] jarFiles = libFolder.listFiles((dir, name) -> name.endsWith(".jar")); if (jarFiles != null) { for (File jarFile : jarFiles) { urls.add(jarFile.toURI().toURL()); } } // 创建类加载器并加载驱动 URLClassLoader classLoader = new URLClassLoader(urls.toArray(new URL[0])); // 某些驱动需要设置当前线程的类加载器 Thread.currentThread().setContextClassLoader(classLoader); driverClass = classLoader.loadClass(driverClassName); System.out.println("JDBC驱动加载成功: " + driverClass); } /** * 建立数据库连接 */ public void connect() throws Throwable { String url = config.getProperty("jdbc.url"); String username = config.getProperty("jdbc.username"); String password = config.getProperty("jdbc.password"); Properties info = new Properties(); info.put("user", username); info.put("password", password); connection = ((Driver) driverClass.getConstructor().newInstance()).connect(url, info); System.out.println("数据库连接成功"); } /** * 获取指定catalog和schema的表信息 */ public List getTables() throws Exception { String catalog = config.getProperty("jdbc.catalog"); String schema = config.getProperty("jdbc.schema"); String tableTypes = config.getProperty("jdbc.table.types"); System.out.println("catalog: " + catalog); System.out.println("schema: " + schema); System.out.println("tableTypes: " + tableTypes); if (tableTypes == null || tableTypes.isEmpty()) { tableTypes = "TABLE,VIEW,MATERIALIZED QUERY TABLE,SYNONYM,ALIAS,EXTERNAL TABLE"; } String tableMaxString = config.getProperty("jdbc.table.max"); int maxTableCount = 100; try { maxTableCount = Integer.parseInt(tableMaxString); } catch (NumberFormatException e) { // 默认100 } List tables = new ArrayList<>(); DatabaseMetaData metaData = connection.getMetaData(); try (ResultSet rs = metaData.getTables(catalog, schema, "%", tableTypes.split(","))) { int count = 0; while (rs.next()) { if (count >= maxTableCount) { break; } count++; String tableName = rs.getString("TABLE_NAME"); tables.add(tableName); System.out.println("发现表: " + tableName); } } return tables; } /** * 关闭连接 */ @Override public void close() { if (connection != null) { try { connection.close(); System.out.println("数据库连接已关闭"); } catch (SQLException e) { e.printStackTrace(); } } } /** * 主方法,用于测试 */ public static void main(String[] args) { long start = System.currentTimeMillis(); try (JdbcTableLoader loader = new JdbcTableLoader("config.properties")) { loader.loadDriver(); loader.connect(); List tables = loader.getTables(); System.out.println("展示 " + tables.size() + " 个表"); long end = System.currentTimeMillis(); System.out.println("耗时: " + (end - start) + " 毫秒"); } catch (Throwable e) { e.printStackTrace(); } } }