Recordset getString returns a value but also throws invalid column name exception under SQL Server

252 Views Asked by At

I am querying an existing table that I cannot alter using JDBC. My SQL Server query of "SELECT * FROM " returns and I log the results with

logger.debug("Retrieved record firstname=" + rs.getString("FirstName"));

The logs shows the data returned but ALSO that an exception was thrown as that indicates the column name is invalid.

10:54:54.546 [main] DEBUG com.goprecise.ams.model.GetUserTest - Retrieved record firstname=System Account
10:54:54.754 [main] ERROR com.goprecise.ams.model.GetUserTest - The column name FirstName is not valid.
com.microsoft.sqlserver.jdbc.SQLServerException: The column name [FirstName] is not valid.
    at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:234)
    at com.microsoft.sqlserver.jdbc.SQLServerResultSet.findColumn(SQLServerResultSet.java:699)
    at com.microsoft.sqlserver.jdbc.SQLServerResultSet.getString(SQLServerResultSet.java:2525)
    at com.goprecise.ams.model.GetUserTest.testJbpmOrganizationTableViaJdbc(GetUserTest.java:95)

The column is in the table and the correct value is displayed for the field in the record, so why is an exception about it being an invalid column name thrown? How can this be corrected? "FirstName" is the name of a column defined as a varchar and not null; isn't a String is returned for a varchar using JDBC? The code surrounding the exception (raised on the line shown above) is:

    String sqlSelectUser = "SELECT TOP (5) * FROM " + helper.getUserDbTable();          
    try {
        Connection conn = DriverManager.getConnection(helper.getJdbcUrl(), helper.getDbUsername(), helper.getDbPassword());             
        logger.info(sqlSelectUser);
        Statement stmt = conn.createStatement();
        ResultSet rs = stmt.executeQuery(sqlSelectUser);
        rs.next();
        logger.debug("Retrieved record firstname=" + rs.getString("FirstName"));
        conn.close();
    } catch (Exception e) {
0

There are 0 best solutions below