Get Column Names From ResultSet for MySQL : Column « Database SQL JDBC « Java






Get Column Names From ResultSet for MySQL

 
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;

public class GetColumnNamesFromResultSet_MySQL {

  public static Connection getConnection() throws Exception {
    String driver = "org.gjt.mm.mysql.Driver";
    String url = "jdbc:mysql://localhost/databaseName";
    String username = "root";
    String password = "root";

    Class.forName(driver);
    Connection conn = DriverManager.getConnection(url, username, password);
    return conn;
  }

  public static void getColumnNames(ResultSet rs) throws SQLException {
    if (rs == null) {
      return;
    }
    ResultSetMetaData rsMetaData = rs.getMetaData();
    int numberOfColumns = rsMetaData.getColumnCount();

    // get the column names; column indexes start from 1
    for (int i = 1; i < numberOfColumns + 1; i++) {
      String columnName = rsMetaData.getColumnName(i);
      // Get the name of the column's table name
      String tableName = rsMetaData.getTableName(i);
      System.out.println("column name=" + columnName + " table=" + tableName + "");
    }
  }

  public static void main(String[] args) {
    Connection conn = null;
    Statement stmt = null;
    ResultSet rs = null;
    try {
      conn = getConnection();
      // prepare query
      String query = "select id, name, age from employees";
      // create a statement
      stmt = conn.createStatement();
      // execute query and return result as a ResultSet
      rs = stmt.executeQuery(query);
      // get the column names from the ResultSet
      getColumnNames(rs);
    } catch (Exception e) {
      e.printStackTrace();
      System.exit(1);
    } finally {
      // release database resources
      try {
        rs.close();
        stmt.close();
        conn.close();
      } catch (SQLException e) {
        e.printStackTrace();
      }
    }
  }
}
           
         
  








Related examples in the same category

1.Get Column Corresponding Class Name
2.Get Column Detail Information
3.Get Column Display Size.zip
4.Get Column Label
5.Get Column Name
6.Use DatabaseMetaData to get table column names
7.Get Column Name And Type For A Table
8.Get Column Name From ResultSet Metadata
9.Get Column Number Of Digits To Right Of The Decimal Point
10.Get Column Number Of Presions Number Of Decimal Digits
11.Get Column ORDINAL POSITION
12.Get Column Privileges
13.Get Column Size
14.Get Column Sql Data Type
15.Get Column SQL Type Name
16.Get Column Type
17.Get Table Optimal Set Of Columns
18.Is Column A Cash Value
19.Is Column Auto Increase
20.Is Column Case Sensitive
21.Is Column Definitely Writable
22.Is Column Nullable
23.Is Column Nullable From ResultSet Metadata
24.Is Column Readonly
25.Is Column Searchable
26.Is Column Signed Number
27.Is Column Writable
28.Make Unique Column in Database Table
29.Remove Unique Column in Database Table
30.Arrange a Column of Database Table
31.Change Column Name of a Table
32.Sum of Column in a Database Table
33.Delete a Column from a Database Table
34.Adding a New Column Name in Database Table
35.Designated column's table name
36.If a table column can have a null value or not?
37.If a table column value is auto-increment?