Designated column's table name : Column « Database « Java Tutorial






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

public class Main {
  private static final String DRIVER = "com.mysql.jdbc.Driver";

  private static final String URL = "jdbc:mysql://localhost/testdb";

  private static final String USERNAME = "root";

  private static final String PASSWORD = "";

  public static void main(String[] args) throws Exception {
    Class.forName(DRIVER);
    Connection connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);

    Statement statement = connection.createStatement();
    String query = "SELECT a.id, a.username, a.country_id, b.country_name " + "FROM users a "
        + "LEFT JOIN countries b ON a.country_id = b.id";
    ResultSet resultSet = statement.executeQuery(query);

    ResultSetMetaData metadata = resultSet.getMetaData();

    String tableName = metadata.getTableName(1);
    System.out.println("Table name of column 'id' = " + tableName);

    tableName = metadata.getTableName(4);
    System.out.println("Table name of column 'country name' = " + tableName);
  }
}








20.24.Column
20.24.1.Get column's precision and scale value
20.24.2.Designated column's table name
20.24.3.If a table column can have a null value or not
20.24.4.Get column names of a table using ResultSetMetaData