JAVA

NAVIGATION
CATEGORIES
REFERRENCE
LINKS
  • Column names

    1 answers - 1653 bytes - related search similar search Add To My Delicious Add To My Stumble Upon Add To My Google Mark Add To My Facebook Add To My Digg Add To My Reddit

    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
  • No.1 | | 473 bytes | |

    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?

Re: Column names


max 4000 letters.
Your nickname that display:
In order to stop the spam: 2 + 1 =
QUESTION ON "JAVA"

DATABASE TECH