简体   繁体   中英

Determine Derby Database Schema Version With Java?

I have found this article which describes how to get the Derby database schema version from the command line with derbyrun.jar.

How can I determine the schema version from within my Java program?

I answered my own question but I think it is not the best solution.我回答了我自己的问题,但我认为这不是最好的解决方案。 When someone proposes a better solution I will accept that as an answer.

I've come up with something to get the schema version in Java, but it's ugly (and not quite complete). I hope somebody out there has something better:


public static void main(String[] args) throws Exception {
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    String strConnectionURL = "jdbc:derby:c:\data\tomcat7\active\LearnyErnie\data\derby;create=false";
    Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
    Connection connection = DriverManager.getConnection(strConnectionURL);
    String strCommand = "values syscs_util.syscs_get_database_property( 'DataDictionaryVersion' );";
    InputStream inputStream = new ByteArrayInputStream(strCommand.getBytes("UTF-8"));
    ij.runScript(connection, inputStream, "UTF-8", outputStream, "UTF-8");
    String strOutput = new String(outputStream.toByteArray());
    System.out.println("Output = " + strOutput);
}

strOutput will contain the following text:


Output = CONNECTION0* -     jdbc:derby:c:\data\tomcat7\active\LearnyErnie\data\derby
* = current connection
ij> values syscs_util.syscs_get_database_property( 'DataDictionaryVersion' );
1
(long line of dashes taken out because it squirrels up the SO output)
10.9
1 row selected
ij>

The schema version is "10.9" in the output above. It is up to you to parse it out. (I'm not bothering with that today. This is just for extra info in a Help -> About dialog and I will just write the whole ugly mess to the screen.)

We certainly can't count on that parsing logic to work in future versions, or even the call to syscs_get_database_property() itself.

I am hoping someone out there has a more resilient way to do this.

It occurred to me later that perhaps the JDBC DatabaseMetaData object would contain info on the database schema in one of its properties, but I've examined them all and it doesn't.后来我想到 JDBC DatabaseMetaData 对象可能会在其属性之一中包含有关数据库架构的信息,但我已经检查了所有这些信息,但没有。 They give info on the driver version only (which can be different than the database schema).

This is many years later, but I was looking for information on using syscs_util.syscs_get_database_property( 'DataDictionaryVersion' ) from Java and found this post. Thought I'd share an easier way to get the DataDictionaryVersion from Java. This example doesn't have any Exception or other error handling, but provides the basic code on how to do it. I initially wrote more code to determine that the Statement execute() call returns a ResultSet containing one column of varchar data, with a column name of 1. I used ResultSet.getMetaData() to get the column information. Once I figured that out, I chose to assume that the column information won't change and did not keep that code.

String connectionURL = "jdbc:derby:dbname";
java.sql.Connection conn = java.sql.DriverManager.getConnection(dbConnectionURL)
String ddVersionQueryStr = "values syscs_util.syscs_get_database_property( 'DataDictionaryVersion' )";
java.sql.Statement stmt = conn.createStatement();
boolean isResultSet = stmt.execute(ddVersionQueryStr);
java.sql.ResultSet resultSet = stmt.getResultSet();
resultSet.next();
System.out.print("ddVersion = " + resultSet.getString(1) + "\n");

The output from this for me is "10.11".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM