- 追加された行はこの色です。
- 削除された行はこの色です。
[[JAVAの記事一覧]]
&topicpath;
*目次 [#y8b7236f]
#contents
*カラム一覧を取得するわけなんだけど、 [#k891bb57]
文字列の
-COLUMN_NAME
とかって、どうやって知り得るのかわかんなかったので、Googleで検索してみました。
**大体こんな感じ [#c08fcac3]
PerlのSQLiteのサンプルコードを参考にする
http://cpansearch.perl.org/src/ADAMK/DBD-SQLite-1.29/lib/DBD/SQLite.pm
TABLE_CAT
TABLE_SCHEM
TABLE_NAME
COLUMN_NAME
DATA_TYPE
TYPE_NAME
COLUMN_SIZE
BUFFER_LENGTH
DECIMAL_DIGITS
NUM_PREC_RADIX
NULLABLE
REMARKS
COLUMN_DEF
SQL_DATA_TYPE
SQL_DATETIME_SUB
CHAR_OCTET_LENGTH
ORDINAL_POSITION
IS_NULLABLE
**型名らしき説明が掲載されているURL [#tc3af60f]
***IBMのDB2 [#ic9b8ce9]
http://publib.boulder.ibm.com/infocenter/iseries/v5r4/index.jsp?topic=/db2/rbafzmstcatalogodbc.htm
***Googleコードより、rubyのサンプルコード [#a074a2a7]
http://code.google.com/p/rubeus/source/browse/trunk/lib/rubeus/jdbc/column.rb?r=237
***SQL Server 2005チックな説明 [#o0aa1341]
https://blogs.msdn.com/b/sqlserverfaq/archive/2009/10/30/sp-columns-take-longer-in-sql-server-2005-sp3-compared-to-sql-server-2000.aspx
***ODBC スキーマ コレクション (ADO.NET) [#dbe67885]
http://msdn.microsoft.com/ja-jp/library/cc668760.aspx
***Symfoware [#h9ee02fe]
http://software.fujitsu.com/jp/manual/manualfiles/M100006/J2X02268/09Z202/J2268-e-03-04.html
***SQLLite [#h07dd2fb]
***SQLite [#h07dd2fb]
http://d.hatena.ne.jp/miyohide/20100317
*カラム一覧を得る [#q8365a01]
package base;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class HelloColumnList {
public static void main(String[] args) {
try {
// ドライバクラスをロード
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
// データベースへ接続
Connection con = DriverManager.getConnection("jdbc:odbc:helloworld");
// ステートメントオブジェクトを生成
Statement stmt = con.createStatement();
DatabaseMetaData dmd = con.getMetaData();
ResultSet rs = null;
rs = dmd.getColumns(null, null,"HELLO_WORLD_TABLE",null);
try {
while(rs.next()){
System.out.println(rs.getString("COLUMN_NAME") + "\n");
}
} finally {
rs.close();
}
stmt.close();
con.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
*カラム情報を得る [#ha477d01]
package base;
import java.sql.ResultSet;
import java.sql.SQLException;
/**<P>
* 検索結果に格納されたカラム情報の結果をキーからJavaのシンボルに切り替える
* </P>
*/
public class ColumnUtil {
//TABLE_CAT
public static String getTABLE_CAT(ResultSet rsColumns) throws SQLException{
return rsColumns.getString("TABLE_CAT");
}
//TABLE_SCHEM
public static String getTABLE_SCHEM(ResultSet rsColumns) throws SQLException{
return rsColumns.getString("TABLE_SCHEM");
}
//TABLE_NAME
public static String getTABLE_NAME(ResultSet rsColumns) throws SQLException{
return rsColumns.getString("TABLE_NAME");
}
//COLUMN_NAME
public static String getCOLUMN_NAME(ResultSet rsColumns) throws SQLException{
return rsColumns.getString("COLUMN_NAME");
}
//DATA_TYPE
public static int getDATA_TYPE(ResultSet rsColumns) throws SQLException{
return rsColumns.getInt("DATA_TYPE");
}
//TYPE_NAME
public static String getTYPE_NAME(ResultSet rsColumns) throws SQLException{
return rsColumns.getString("TYPE_NAME");
}
//COLUMN_SIZE
public static int getCOLUMN_SIZE(ResultSet rsColumns) throws SQLException{
return rsColumns.getInt("COLUMN_SIZE");
}
//BUFFER_LENGTH
public static int getBUFFER_LENGTH(ResultSet rsColumns) throws SQLException{
return rsColumns.getInt("BUFFER_LENGTH");
}
//DECIMAL_DIGITS
public static int getDECIMAL_DIGITS(ResultSet rsColumns) throws SQLException{
return rsColumns.getInt("DECIMAL_DIGITS");
}
//NUM_PREC_RADIX
public static int getNUM_PREC_RADIX(ResultSet rsColumns) throws SQLException{
return rsColumns.getInt("NUM_PREC_RADIX");
}
//NULLABLE
public static int getNULLABLE(ResultSet rsColumns) throws SQLException{
return rsColumns.getInt("NULLABLE");
}
//REMARKS
public static String getREMARKS(ResultSet rsColumns) throws SQLException{
return rsColumns.getString("REMARKS");
}
//COLUMN_DEF
public static String getCOLUMN_DEF(ResultSet rsColumns) throws SQLException{
return rsColumns.getString("COLUMN_DEF");
}
//SQL_DATA_TYPE
public static int getSQL_DATA_TYPE(ResultSet rsColumns) throws SQLException{
return rsColumns.getInt("SQL_DATA_TYPE");
}
//SQL_DATETIME_SUB
public static int getSQL_DATETIME_SUB(ResultSet rsColumns) throws SQLException{
return rsColumns.getInt("SQL_DATETIME_SUB");
}
//CHAR_OCTET_LENGTH
public static int getCHAR_OCTET_LENGTH(ResultSet rsColumns) throws SQLException{
return rsColumns.getInt("CHAR_OCTET_LENGTH");
}
//ORDINAL_POSITION
public static int getORDINAL_POSITION(ResultSet rsColumns) throws SQLException{
return rsColumns.getInt("ORDINAL_POSITION");
}
//IS_NULLABLE
public static String getIS_NULLABLE(ResultSet rsColumns) throws SQLException{
return rsColumns.getString("IS_NULLABLE");
}
//JDBC_DATA_TYPE
public static int getJDBC_DATA_TYPE(ResultSet rsColumns) throws SQLException{
return rsColumns.getInt("JDBC_DATA_TYPE");
}
//PSEUDO_COLUMN
public static int getPSEUDO_COLUMN(ResultSet rsColumns) throws SQLException{
return rsColumns.getInt("PSEUDO_COLUMN");
}
//COLUMN_TEXT
public static String getCOLUMN_TEXT(ResultSet rsColumns) throws SQLException{
return rsColumns.getString("COLUMN_TEXT");
}
}