The Java Developers Almanac 1.4


Order this book from Amazon.

   
Home > List of Packages > java.sql  [73 examples] > Retrieving Data  [7 examples]

e254. Getting the Column Names in a Result Set

    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) {
    }

 Related Examples
e251. Getting Rows from a Database Table
e252. Getting Data from a Result Set
e253. Determining If a Fetched Value Is NULL
e255. Getting the Number of Rows in a Database Table
e256. Getting BLOB Data from a Database Table
e257. Matching with Wildcards in a SQL Statement

See also: Batching    Connections    Database Meta Data    Deleting Data    Drivers    Importing and Exporting    Inserting and Updating Data    Oracle OBJECTs    Oracle VARRAYs    Procedures and Functions    Scrollable Result Sets    Tables    Updatable Result Sets   


© 2002 Addison-Wesley.