看到下面的代码,但getCoulmnName没有采取索引我?帮助我

时间:2015-02-07 17:05:19

标签: jdbc

package pack;

import java.sql.*;

class Rsmd {
    public static void main(String args[]) {
        try {
            Class.forName("oracle.jdbc.driver.OracleDriver");
            Connection con = DriverManager.getConnection(
                    "jdbc:oracle:thin:@10.23.0.100:1521:esgdb10", "scott",
                    "tiger");

       PreparedStatement ps = con.prepareStatement("select * from employee");
                        ResultSet rs = ps.executeQuery();
                        ResultSetMetaData rsmd = rs.getMetaData();
            System.out.println                              ("Totalcolumns: "rsmd.getColumnCount ());
            int x = 0;
            x = rsmd.getColumnCount();
            for (int i = 0; i < x; i++) {
                System.out.println("Column Name of 1stcolumn: "+ rsmd.getColumnName(i));
System.out.println("Column Type Name of column: "
                        + rsmd.getColumnTypeName(i));/*here it is not taking index i */
 /* i want to display all the column name of a record*/
            }
            con.close();

        } catch (Exception e) {
            System.out.println(e);
        }

    }
}

在上面的代码中,使用了oracle驱动程序,我想显示一个记录的所有列名。为此我使用了index-i,但getColumnName不支持index-i.pls帮助我。

1 个答案:

答案 0 :(得分:0)

ResultSetMetaData.getColumnName(int) 一个为基础。

因此,您的迭代应从1开始,到getColumnCount()结束,包括:

int x = rsmd.getColumnCount();
for (int i = 1; i <= x; i++) {
    System.out.println("Column Name of 1stcolumn: " + rsmd.getColumnName(i));
    System.out.println("Column Type Name of column: " + rsmd.getColumnTypeName(i));
}