Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to retrieve sequences metadata from JDBC?

I am trying to retrieve different kind of metadata of my Oracle DB from Java code (using basic JDBC). For example, if I want to retrieve the list of tables with _FOO suffix, I can do something like:

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...

Now, I want to retrieve all the sequences from my database (for example all sequence named S_xxx_FOO).

How would I do that, as I don't see anything in DatabaseMetaData related to sequences?

Do I have to run a query like select * from user_sequences ?

like image 385
Romain Linsolas Avatar asked Apr 12 '11 15:04

Romain Linsolas


People also ask

How do I find the metadata of a database?

To get the metadata from your source, call the getMetaData() method using the Connection object that was created in the last part of this series. Here is a simple code to extract all the user defined tables from your data source. The important method calls to notice are the connection.

How do I get metadata from ResultSet?

The getMetaData() method of ResultSet interface retrieves the ResultSetMetaData object of the current ResultSet. This method returns a ResultSetMetaData object which holds the description of this ResultSet object's columns.

What is metadata in JDBC?

Overview. JDBC provides a Java API to read the actual data stored in database tables. Besides this, the same API can also be used to read metadata about the database. Metadata means data about the data such as table names, column names, and column types.


2 Answers

Had the same question. It's fairly easy. Just pass in "SEQUENCE" into the getMetaData().getTables() types param.

In your specific case it would be something like:

meta.getTables(connection.getCatalog(), null, "%_FOO", new String[] { "SEQUENCE" });
like image 127
Jeff Avatar answered Oct 11 '22 08:10

Jeff


You can't do this through the JDBC API, because some databases (still) do not support sequences.

The only way to get them is to query the system catalog of your DBMS (I guess it's Oracle in your case as you mention user_sequences)

like image 3
a_horse_with_no_name Avatar answered Oct 11 '22 09:10

a_horse_with_no_name