JDBC ResultSet get columns with table alias

62,331

Solution 1

JDBC will simply name the columns by what is specified in the query - it doesn't know about table names etc.

You have two options:

Option 1: Name the columns differently in the query, ie

SELECT
    a.columnName as columnNameA,
    b.columnName as columnNameB,
    ...
from table1 a, table2 b where (WHATEVER)

then in your java code refer to the column aliases:

resultSet.getString("columnNameA");
resultSet.getString("columnNameB");


Option 2: Refer to the column position in your call to the JDBC API:

resultSet.getString(1);
resultSet.getString(2);

Note that the JDBC API uses one-based indexes - ie they count from 1 (not from 0 like java indexes), so use 1 for the first column, 2 for the second column, etc


I would recommend option 1, because it's safer to refer to named columns: Someone may change the order of the columns in the query and it would silently break your code (you would be accessing the wrong column but would not know), but if they change the columns names, you'll at least get a "no such column" exception at runtime.

Solution 2

ResultSetMetadata.getColumnLabel() is what you need

(edit) sample example, as stated by bharal in comment

SELECT * from table1 a, table2 b where (WHATEVER)

ResultSetMetaData rsmd = rset.getMetaData();
rsmd.getColumnLabel(1);

Solution 3

Use column aliases like:

SELECT A.ID 'A_ID', B.ID 'B_ID' FROM TABLE1 AS A, TABLE2 AS B...

And specify all the columns you are retrieving (is a good practice).

Solution 4

If you are using MySQL just add

&useOldAliasMetadataBehavior=true

to your connectionString.

Afterwards you can use this little Helper:

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

public class ResultSetHelper {

    private final Map<String, Integer> columnMap;

    public ResultSetHelper(ResultSet rs) throws SQLException {
        this.columnMap = new HashMap<>();
        ResultSetMetaData md = rs.getMetaData();
        int columnCount = md.getColumnCount();
        for (int index = 1; index <= columnCount; index++) {
            String columnName = md.getColumnLabel(index);
            if (!columnMap.containsKey(columnName)) {
                columnMap.put(columnName, index);
            }

            String tableAlias = md.getTableName(index);
            if (tableAlias != null && !tableAlias.trim().isEmpty()) {
                columnMap.put(tableAlias + "." + columnName, index);
            }
        }
    }

    public Integer getColumnIndex(String columnName) {
        return columnMap.get(columnName);
    }

    public Integer getColumnIndex(String tableAlias, String columnName) {
        return columnMap.get(tableAlias + "." + columnName);
    }

}

Solution 5

Ok, it seems there's no method like resultSet.getString("a.columnName"); and you have to alias your columns at sql level, but inasmuch as there's a getTableName(iCol) method I hope guys at java.sql.ResultSet add such a feature.

Share:
62,331
Franz Kafka
Author by

Franz Kafka

Czech beer is awesome

Updated on July 09, 2022

Comments

  • Franz Kafka
    Franz Kafka almost 2 years

    Imagine I have a query like

    SELECT * from table1 a, table2 b where (WHATEVER)
    

    Maybe both tables have the same column name. So I though it would be nice to access the data via

    resultSet.getString("a.columnName");
    resultSet.getString("b.columnName");
    

    But this backfires on me and I get nothing. I read the API, but they don't really talk about this case. Is such a feature vendor dependent?

  • Franz Kafka
    Franz Kafka over 12 years
    Okay looks like I will have to forget about the nice * and name everything and use an extra mapping.
  • Mateen
    Mateen over 7 years
    there you go edited the answer by a sample code snippet
  • inyourcorner
    inyourcorner over 6 years
    @Bohemian - you can use the ResultSetMetaData::getTableName(int) function to get the tablename. I'm not sure if the result depends on the jdbc implementation, though. docs.oracle.com/javase/7/docs/api/java/sql/…
  • Miha_x64
    Miha_x64 about 4 years
    With SQLite JDBC driver this gives me only column name, e.g. username, instead of full a.username. Is there a way to get a, table alias?