jsp jstl sql strange behaviour with as in mysql

北城以北 提交于 2021-01-27 06:51:55

问题


In mysql i m having a stored procedure which has a sql like:

select firstname as i_firstname , lastname as i_lastname from roleuser 
where user_id = uid ;

I m using a jstl code to get the values: -

<sql:query var="comm_codes" dataSource="jdbc/myDatasource">
    call sp_select_username(?);
    <sql:param>${user_id}</sql:param>
</sql:query>

<c:forEach var="rows" items="${comm_codes.rows}">
    ${rows.i_firstname} ${rows.i_lastname}
</c:forEach>

But this code does not return anything but when the replace the above code ${rows.i_firstname} with ${rows.firstname} i get the correct values.

Anything wrong with jstl, is this replicable or my fault here...

Question also posted here and here

thanks


回答1:


I know it is an old post, but I encountered this problem as well. It is discussed here: http://forums.mysql.com/read.php?39,432843,432862#msg-432862

Importantly, the poster in the mysql forum states

ResultSetMetaData.getColumnName() will return the actual name of the column, if it exists

This provides a work-around - prevent the column name from existing, so that the alias must be used. As an example, the original poster's stored procedure could be modified to be

select concat(first name,'') as i_firstname , 
       concat(lastname,'') as i_lastname from roleuser 
where user_id = uid ; 

In this case, the original column is now unknown, and the alias is used. I've tested this on my system in a similar situation at it worked. Likewise, if you need to use an alias for an int, you can try SELECT (id+0) AS id_alias. I'm sure most column types have similar solutions. Hope this helps.



来源:https://stackoverflow.com/questions/4998749/jsp-jstl-sql-strange-behaviour-with-as-in-mysql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!