Results 1 to 2 of 2

Thread: Column names

  1. #1
    Join Date
    Jan 2003
    Posts
    2

    Unanswered: Column names

    I want to get the column names of a table. So I have written a little method:

    public String[] getColumnNames(String table)
    {
    ...
    DatabaseMetaData dbmd = conn.getMetaData(); //from the Connection
    ResultSet rs = dbmd.getColumns(null,null,table,null);
    int count=0;
    while (rs.next())
    count++;

    ....
    }

    So, everthing works fine with Informix. I get the right number of the tables and the names of the tables.

    But nothing happens with MySQL. The number is always 0. (I know it is not the right number).

    Is there maybe a bug in the JDBC? I use the mysql-connector-java-2.0.14 from MySQL.

    Another possibiliy to get the column names is:

    try {
    // Create a result set
    Statement stmt = connection.createStatement();
    ResultSet rs = stmt.executeQuery("SELECT * FROM my_table");

    // Get result set meta data
    ResultSetMetaData rsmd = rs.getMetaData();
    int numColumns = rsmd.getColumnCount();

    // Get the column names; column indices start from 1
    for (int i=1; i<numColumns+1; i++) {
    String columnName = rsmd.getColumnName(i);

    // Get the name of the column's table name
    String tableName = rsmd.getTableName(i);
    }
    } catch (SQLException e) {
    }


    I know that. But my database is too large. So I can't load all data in one ResultSet. And why should I, just to get the column names.

    Anyone some good ideas?

    Thanks,

    Nils

  2. #2
    Join Date
    Jan 2003
    Posts
    2
    OK. Found one way.

    Statement state = conn.createStatement();
    ResultSet rs = state.executeQuery("Select * from "+table+" LIMIT 1");


    ResultSetMetaData rsmd = rs.getMetaData();
    int anz = rsmd.getColumnCount();


    Just setting the LIMIT 1.

    Anyhow can I use the jdbc and the method getColumns() from the DataBaseMetaData-interface?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •