Menu

Problem with alias AS

Help
2016-11-03
2016-11-04
  • FARNIER Clément

    Hello everyone,

    I got a problem when I execute some sql queries using UCanAccess 3.0.7.
    Here is my code :

    public Connection getConnection() {
            Connection dbCon = null;
            try {
                Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
                dbCon = DriverManager.getConnection("jdbc:ucanaccess://D:/eclipse/eclipse-jee-neon-1-win32-x86_64/workspace/Lending-Hotline/web/Hotline.accdb", "","");
            } catch (ClassNotFoundException e) {
                Trace.logStackTrace(e);
            } catch (SQLException e) {
                Trace.logStackTrace(e);
            } finally {}
            return dbCon;
        }
    
        public synchronized DataList executeQueryCombo(String aQuery, String aScope, String aRefQuery, String aIPaddress) throws SQLException {
    
            DataList dataList = null;
            DataHashtable dataHashtable;
            DataItem dataItem;
    
            try {
                stmt = getConnection().createStatement();
                System.out.println("query = " + aQuery);
                rs = stmt.executeQuery(aQuery);
    
                /* I put my result in a dataList using some parameters (aScope, aRefQuery, aIPaddress) */
    
                ...
    
                /* The following methods just do Object.close() if Object != null */
                closeResultSet();
                closeStatement();
                closeConnection();
            } catch (SQLException e) {
                Trace.logMessage(aQuery);
                Trace.logStackTrace(e);
    
                closeResultSet();
                closeStatement();
                closeConnection();
    
                throw e;
            }
            return dataList;
        }
    

    And I want to execute this query :

    SELECT IdModule AS ITEM_ID, NomModule AS ITEM_VALUE, ModuleLivrable FROM Module;
    

    The query is executed, but there is a problem : the alias ITEM_ID and ITEM_VALUE are ignored and I have, for example, this result :
    [IdModule = 1;NomModule = Engine;ModuleLivrable = TRUE;]
    instead of :
    [ITEM_ID = 1;ITEM_VALUE = Engine;ModuleLivrable = TRUE;]
    and it's a problem for my next treatment.

    I search for a solution by my own and I found my alias are not ignored if the expression before AS is composed and isn't just a column name.
    There is an example :

    SELECT '' & IdModule AS ITEM_ID, '' & NomModule AS ITEM_VALUE, ModuleLivrable FROM Module;
    

    return :
    [ITEM_ID = 1;ITEM_VALUE = Engine;ModuleLivrable = TRUE;]

    If anyone can help me to found a better solution, it would be great !
    I notice I use :
    - Java 6u45
    - MS Access 2013 (.mdb & .accdb)
    - UCanAccess 3.0.7

    Thanks !

    PS : English is not my mothertongue, so sorry if there are mistakes.

     
  • Gord Thompson

    Gord Thompson - 2016-11-03

    If you are using ResultSetMetaData#getColumnName then try using ResultSetMetaData#getColumnLabel instead.

     
  • FARNIER Clément

    Indeed, I'm using ResultSetMetaData#getColumnName.
    Change to ResultSetMetaData#getColumnLabel seems to work well. It's much better than my ugly hack !
    Thanks for the solution ! ;)

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.