create alias if not exists sc_get_tables 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_tables(String jdbcDriver, String url,
String user, String password, String schemaName)
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.");
}
if( schemaName == null ){
throw new Exception("schemaName is not specified.");
}
Class.forName(jdbcDriver);
Connection conn = null;
SimpleResultSet rs = new SimpleResultSet();
rs.addColumn("NAME", Types.VARCHAR, 4096, 0);
rs.addColumn("FULLNAME", Types.VARCHAR, 4096, 0);
rs.addColumn("REMARKS", Types.VARCHAR, 4096, 0);
try
{
conn = DriverManager.getConnection(url, user, password);
SchemaCrawlerOptions options = new SchemaCrawlerOptions();
Database db = SchemaCrawlerUtility.getDatabase(conn, options);
Schema schema = db.getSchema(schemaName);
if( schema != null ){
Table tables[] = schema.getTables();
for(int li=0;li<tables.length;li++){
rs.addRow(tables[li].getName(),
tables[li].getFullName(),
tables[li].getRemarks());
}
}
}
finally
{
if( conn != null )conn.close();
}
return rs;
}
$$
実行例
select * from
sc_get_tables('org.postgresql.Driver',
'jdbc:postgresql://localhost:5432/postgres',
'postgres', 'postgres', 'public');
※以下の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 件のコメント:
コメントを投稿