create alias if not exists sc_get_schemas as $$
import java.io.*;
import java.util.*;
import java.sql.*;
import org.h2.tools.*;
import java.net.*;
import schemacrawler.schema.*;
import schemacrawler.schemacrawler.*;
import schemacrawler.utility.*;
@CODE
ResultSet sc_get_schemas(String jdbcDriver, String url,
String user, String password)
throws Exception
{
if( jdbcDriver == null ){
throw new Exception("jdbcDriver is not specified.");
}
if( url == null ){
throw new Exception("url is not specified.");
}
if( user == null ){
throw new Exception("user is not specified.");
}
if( password == null ){
throw new Exception("password is not specified.");
}
Class.forName(jdbcDriver);
Connection conn = null;
SimpleResultSet rs = new SimpleResultSet();
rs.addColumn("CATALOG", Types.VARCHAR, 4096, 0);
rs.addColumn("SCHEMA", Types.VARCHAR, 4096, 0);
try
{
conn = DriverManager.getConnection(url, user, password);
SchemaCrawlerOptions options = new SchemaCrawlerOptions();
Database db = SchemaCrawlerUtility.getDatabase(conn, options);
Schema schemas[] = db.getSchemas();
for(int li=0;li<schemas.length;li++){
rs.addRow(schemas[li].getCatalogName(),
schemas[li].getSchemaName());
}
}
finally
{
if( conn != null )conn.close();
}
return rs;
}
$$
実行例
select * from
sc_get_schemas('org.postgresql.Driver',
'jdbc:postgresql://localhost:5432/postgres',
'postgres', 'postgres');
※以下のjarをCLASSPATH環境変数に追加
schemacrawler-8.5.1.jar, schemacrawler-postgresql-8.5.1.jar
○動作環境
JDK6 Update23, H2 Database 1.2.149 (2011-01-07), SchemaCrawler 8.5.1
○関連情報
・SchemaCrawlerのウェブサイト
http://schemacrawler.sourceforge.net/
・H2 Databaseに関する他の記事はこちらを参照してください。
0 件のコメント:
コメントを投稿