如何从DB2中获取列名

puruo6ea  于 2022-11-07  发布在  DB2
关注(0)|答案(4)|浏览(287)

我想知道如何从DB2-DB中获取表的列名。
我不是说像这样:

SELECT * FROM TABLE A;

我会得到:

|  ColumnA  |  ColumnB |
------------------------
  ValueA 1     ValueB 1

  ValueA 2     ValueB 2

  ValueA 3     ValueB 3

但更像是:

SELECT column_name FROM TABLE A;

并得到:

ColumnA, ColumnB
64jmpszr

64jmpszr1#

对于DB2 for i,此信息位于

qsys2.syscolumns2  (for 7.1+, better performance if just looking at 1 table's columns)
qsys2.syscolumns (for any recent version)

ODBC/JDBC和DB2 LUW v8兼容

sysibm.sqlcolumns

ANSI/ISO标准

INFORMATION_SCHEMA.columns

(note INFORMATION_SCHEMA是QSYS 2的同义词)
目录视图的完整列表如下:http://pic.dhe.ibm.com/infocenter/iseries/v7r1m0/topic/db2/rbafzcatalog.htm

lp0sw83n

lp0sw83n2#

此信息可在表syscat.columns中找到。

uajslkp6

uajslkp63#

另一种方法是从Linux bash shell执行db2命令:

$ db2 "DESCRIBE TABLE A" |awk '{printf "%s\t",$1} END{print}'
1qczuiv0

1qczuiv04#

db400(IBM i)中任意表(PF)的列名,我们可以使用java程序检索列名.示例代码:

String sql = "select * from KFILNBB.V_OCADATA";
PreparedStatement pstmt = StaticUtil.getDB2Connection().prepareStatement(sql);
ResultSet rs = pstmt.executeQuery();

ResultSetMetaData rs1 = pstmt.getMetaData();

ResultSetMetaData rsmd = rs.getMetaData();
int numberOfColumns = rsmd.getColumnCount();
System.out.println(rsmd.getColumnLabel(1));

// -------------------db2 connection----------

    public static Connection getDB2Connection() throws ClassNotFoundException, SQLException {
            String DRIVER_NAME = "com.ibm.as400.access.AS400JDBCDriver";

            String URL = "jdbc:as400://ibm i IP address";
             String USERNAME = "username";
            String PASSWORD = "password";
            Class.forName(DRIVER_NAME);
            return DriverManager.getConnection(URL , USERNAME, PASSWORD);
        }

相关问题