JDBC ResultSet get columns with table alias

后端 未结 7 917
春和景丽
春和景丽 2020-11-28 13:22

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

相关标签:
7条回答
  • 2020-11-28 13:35

    One idea I had is to use the getTableName(iCol) to grab the table names for the duplicately-named columns, then wrap a hash of your own keys (with the table name prefix) that would point you to the correct column index, and reference your column-values that way. This would require an initial loop through the meta data at the beginning to set up. The only issue I see with this is that you are aliasing the table names as well. I've yet to find a way of getting those table name aliases from jdbc without managing it yourself when you build the sql statement. This solution would depend on what the syntactical pay-off would be to you.

    0 讨论(0)
  • 2020-11-28 13:40

    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);
        }
    
    }
    
    0 讨论(0)
  • 2020-11-28 13:44

    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).

    0 讨论(0)
  • 2020-11-28 13:46

    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);
    
    0 讨论(0)
  • 2020-11-28 13:47

    You can use alias on SQL level. Then you retrieve data by indexes. (But this approach will make maintenance a real nightmare)

    SELECT a.column, b.column FROM table1 a, table2 b
    
    String value = rs.getString(1);
    
    0 讨论(0)
  • 2020-11-28 13:54

    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.

    0 讨论(0)
提交回复
热议问题