使用SchemaCrawler获取存储过程

时间:2015-08-17 18:29:27

标签: sql-server schemacrawler

当我尝试使用SchemaCrawler API从SQL Server数据库检索存储过程时,出现此错误:

12:28:07.427 [main] INFO  schemacrawler.crawl.SchemaCrawler - Retrieving routines
12:28:07.767 [main] WARN  schemacrawler.crawl.RoutineRetriever - JDBC driver does not support retrieving functions
java.lang.AbstractMethodError: null
    at net.sourceforge.jtds.jdbc.JtdsDatabaseMetaData.getFunctions(JtdsDatabaseMetaData.java:3570) ~[jtds-1.3.1.jar:1.3.1]
    at schemacrawler.crawl.RoutineRetriever.retrieveFunctions(RoutineRetriever.java:175) ~[schemacrawler-14.02.02.jar:na]
    at schemacrawler.crawl.SchemaCrawler.crawlRoutines(SchemaCrawler.java:214) [schemacrawler-14.02.02.jar:na]
    at schemacrawler.crawl.SchemaCrawler.crawl(SchemaCrawler.java:564) [schemacrawler-14.02.02.jar:na]
    at schemacrawler.utility.SchemaCrawlerUtility.getCatalog(SchemaCrawlerUtility.java:49) [schemacrawler-14.02.02.jar:na]
    at schemacrawler.utility.SchemaCrawlerUtility.getCatalog(SchemaCrawlerUtility.java:57) [schemacrawler-14.02.02.jar:na]
    at com.expedia.cgs.db.ExportScripts.main(ExportScripts.java:41) [classes/:na]

jtds驱动程序支持DatabaseMetaData.getProcedures()但不支持DatabaseMetaData.getFunctions()。有解决方法吗?

更新:这是我的代码:

import java.sql.Connection;
import java.sql.SQLException;
import javax.sql.DataSource;
import org.slf4j.bridge.SLF4JBridgeHandler;
import schemacrawler.schema.Catalog;
import schemacrawler.schema.Column;
import schemacrawler.schema.Routine;
import schemacrawler.schema.Schema;
import schemacrawler.schema.Table;
import schemacrawler.schema.View;
import schemacrawler.schemacrawler.DatabaseConnectionOptions;
import schemacrawler.schemacrawler.RegularExpressionInclusionRule;
import schemacrawler.schemacrawler.SchemaCrawlerException;
import schemacrawler.schemacrawler.SchemaCrawlerOptions;
import schemacrawler.schemacrawler.SchemaInfoLevelBuilder;
import schemacrawler.utility.SchemaCrawlerUtility;

public class ExportScripts {

    public static void main(String[] args) throws SchemaCrawlerException, SQLException {
        SLF4JBridgeHandler.removeHandlersForRootLogger();
        SLF4JBridgeHandler.install();

        // Create a database connection
        final DataSource dataSource = new DatabaseConnectionOptions("jdbc:sqlserver://myDatabase;appName=SchemaCrawler;useCursors=true");
        final Connection connection = dataSource.getConnection("username", "password");

        // Create the options
        final SchemaCrawlerOptions options = new SchemaCrawlerOptions();
        // Set what details are required in the schema - this affects the
        // time taken to crawl the schema
        options.setSchemaInfoLevel(SchemaInfoLevelBuilder.standard());
        options.setRoutineInclusionRule(new RegularExpressionInclusionRule("ContentGeneration\\.dbo.*"));
        options.setSchemaInclusionRule(new RegularExpressionInclusionRule("ContentGeneration\\.dbo.*"));

        // Get the schema definition
        final Catalog catalog = SchemaCrawlerUtility.getCatalog(connection,
                options);

        for (final Schema schema : catalog.getSchemas()) {
            System.out.println(schema);
            for (final Routine routine : catalog.getRoutines()) {
                System.out.println("r--> " + routine);
                System.out.println("definition: " + routine.getDefinition());
            }
            for (final Table table : catalog.getTables(schema)) {
                System.out.print("o--> " + table);
                if (table instanceof View) {
                    System.out.println(" (VIEW)");
                } else {
                    System.out.println();
                }

                for (final Column column : table.getColumns()) {
                    System.out.println("     o--> " + column + " ("
                            + column.getColumnDataType() + ")");
                }
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

以下是如何获得对Microsoft SQL Server的完整SchemaCrawler支持。

  1. 在Maven项目中包含具有Microsoft SQL Server支持的SchemaCrawler jar。
  2. <dependency> <groupId>us.fatehi</groupId> <artifactId>schemacrawler-sqlserver</artifactId> <version>14.02.02</version> </dependency>

    1. 使用与此类似的代码。

      final DatabaseSystemConnector dbSystemConnector = new SqlServerDatabaseConnector()。getDatabaseSystemConnector();

      final DatabaseSpecificOverrideOptions databaseSpecificOverrideOptions = dbSystemConnector.getDatabaseSpecificOverrideOptionsBuilder()。toOptions();

      final SchemaCrawlerOptions schemaCrawlerOptions = new SchemaCrawlerOptions(); schemaCrawlerOptions.setSchemaInfoLevel(InfoLevel.maximum.buildSchemaInfoLevel());

      final Catalog catalog = SchemaCrawlerUtility.getCatalog(getConnection(),databaseSpecificOverrideOptions,schemaCrawlerOptions);

    2. 循环遍历例程,并使用Routine.getDefinition()获取定义。

    3. Sualeh Fatehi,SchemaCrawler