如何从JDBC检索序列元数据?

时间:2011-04-12 15:07:06

标签: java jdbc sequence database-metadata

我正在尝试从Java代码(使用基本JDBC)检索我的Oracle DB的不同类型的元数据。例如,如果我想检索带有_FOO后缀的表列表,我可以执行以下操作:

Connection connection = dataSource.getConnection();
DatabaseMetaData meta = connection.getMetaData();
ResultSet tables = meta.getTables(connection.getCatalog(), null, "%_FOO", new String[] { "TABLE" });
// Iterate on the ResultSet to get information on tables...

现在,我想从我的数据库中检索所有序列(例如,所有名为S_xxx_FOO的序列。)

我怎么能这样做,因为我在DatabaseMetaData中没有看到与序列相关的任何内容?

我是否必须运行select * from user_sequences之类的查询?

4 个答案:

答案 0 :(得分:3)

您无法通过JDBC API执行此操作,因为某些数据库(仍然)不支持序列。

获取它们的唯一方法是查询DBMS的系统目录(在你提到user_sequences时,我猜你的情况就是Oracle)

答案 1 :(得分:3)

有同样的问题。这很容易。只需将“SEQUENCE”传入getMetaData()。getTables()类型参数。

在您的具体情况下,它将类似于:

meta.getTables(connection.getCatalog(),null,“%_FOO”,new String [] {“SEQUENCE”});

答案 2 :(得分:0)

您可以使用hibernate方言api来检索序列名称。见:http://docs.jboss.org/hibernate/orm/3.2/api/org/hibernate/dialect/Dialect.html

从下面的示例中,您可以看到如何使用dialect获取序列名称

public static void main(String[] args) {
        Connection jdbcConnection = null;
        try {
            jdbcConnection = DriverManager.getConnection("", "", "");
            printAllSequenceName(jdbcConnection);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            if(jdbcConnection != null) {
                try {
                    jdbcConnection.close();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
}

public static void printAllSequenceName(Connection conn) throws JDBCConnectionException, SQLException {
        DialectResolver dialectResolver = new StandardDialectResolver();
        Dialect dialect =  dialectResolver.resolveDialect(conn.getMetaData());

        if ( dialect.supportsSequences() ) {
            String sql = dialect.getQuerySequencesString();
            if (sql!=null) {

                Statement statement = null;
                ResultSet rs = null;
                try {
                    statement = conn.createStatement();
                    rs = statement.executeQuery(sql);

                    while ( rs.next() ) {
                        System.out.println("Sequence Name : " +  rs.getString(1));
                    }
                }
                finally {
                    if (rs!=null) rs.close();
                    if (statement!=null) statement.close();
                }

            }
        }
    }

如果您不想使用hibernate,那么您必须创建自定义顺序特定实现。

自定义实施的示例代码

interface SequenceQueryGenerator {
    String getSelectSequenceNextValString(String sequenceName);
    String getCreateSequenceString(String sequenceName, int initialValue, int incrementSize); 
    String getDropSequenceStrings(String sequenceName); 
    String getQuerySequencesString(); 
}


class OracleSequenceQueryGenerator implements SequenceQueryGenerator {

    @Override
    public String getSelectSequenceNextValString(String sequenceName) {
        return "select " + getSelectSequenceNextValString( sequenceName ) + " from dual";
    }

    @Override
    public String getCreateSequenceString(String sequenceName,
            int initialValue, int incrementSize) {
        return "create sequence " + sequenceName +  " start with " + initialValue + " increment by " + incrementSize;
    }

    @Override
    public String getDropSequenceStrings(String sequenceName) {
        return "drop sequence " + sequenceName;
    }

    @Override
    public String getQuerySequencesString() {
        return "select sequence_name from user_sequences";
    }

}


class PostgresSequenceQueryGenerator implements SequenceQueryGenerator {

    @Override
    public String getSelectSequenceNextValString(String sequenceName) {
        return "select " + getSelectSequenceNextValString( sequenceName );
    }

    @Override
    public String getCreateSequenceString(String sequenceName,
            int initialValue, int incrementSize) {
        return "create sequence " + sequenceName + " start " + initialValue + " increment " + incrementSize;
    }

    @Override
    public String getDropSequenceStrings(String sequenceName) {
        return "drop sequence " + sequenceName;
    }

    @Override
    public String getQuerySequencesString() {
        return "select relname from pg_class where relkind='S'";
    }

}

public void printSequenceName (SequenceQueryGenerator queryGenerator, Connection conn) throws SQLException {
        String sql = queryGenerator.getQuerySequencesString();
        if (sql!=null) {

            Statement statement = null;
            ResultSet rs = null;
            try {
                statement = conn.createStatement();
                rs = statement.executeQuery(sql);

                while ( rs.next() ) {
                    System.out.println("Sequence Name : " +  rs.getString(1));
                }
            }
            finally {
                if (rs!=null) rs.close();
                if (statement!=null) statement.close();
            }

        }
    }

public static void main(String[] args) {
        Connection jdbcConnection = null;
        try {
            jdbcConnection = DriverManager.getConnection("", "", "");
            printAllSequenceName(new OracleSequenceQueryGenerator(), jdbcConnection);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
            if(jdbcConnection != null) {
                try {
                    jdbcConnection.close();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
}

答案 3 :(得分:0)

鉴于最新版本的Oracle JDBC驱动程序(例如12.1.0.2)don't return sequence information,当types设置为["SEQUENCE"]时调用DatabaseMetaData#getTables,您最好的选择是自己运行必要的查询,例如:

  SELECT o.owner AS sequence_owner,
       o.object_name AS sequence_name
  FROM all_objects o
  WHERE o.owner LIKE 'someOwnerPattern' ESCAPE '/'
    AND o.object_name LIKE 'someNamePattern' ESCAPE '/'
    AND o.object_type = 'SEQUENCE'
  ORDER BY 1, 2

...其中someOwnerPatternsomeNamePattern是与您LIKE运算符一起使用的SQL模式(例如%匹配任何内容)。

这与驱动程序本身运行的查询基本相同,只是它查询类型为SEQUENCE的对象。