Designated column's table name : Column « Database SQL JDBC « Java

Designated column's table name

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 {
    Connection connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);

    Statement statement = connection.createStatement();
    String query = "SELECT, a.username, a.country_id, b.country_name " + "FROM users a "
        + "LEFT JOIN countries b ON a.country_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);


Related examples in the same category

1.Get Column Corresponding Class Name
2.Get Column Detail Information
3.Get Column Display
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
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.If a table column can have a null value or not?
36.If a table column value is auto-increment?
37.Get Column Names From ResultSet for MySQL