The column name is not valid - error

妖精的绣舞 提交于 2021-01-28 19:22:16

问题


I am getting this error while I am fetching value from resultset.

Error : com.microsoft.sqlserver.jdbc.SQLServerException: The column name company.short_name is not valid

CASE 1 :

select company.short_Name,location_name from company,location;

this query is executing fine on SQL Server but in my java code when I trying to retrieve value like resultset.getString("company.short_name"); that time this give the above error.

CASE 2 :

select company.short_Name short_name,location_name from company,location;

and retrieve value like resultset.getString("short_name"); than it work fine with both database MySQL and MSSQL.

I am migrating my database from MySQL to MSSQL.above case 1 is work fine in MySQL, but why it is not work in MSSQL?


回答1:


resultset.getString("company.short_name"); is wrong here. No need to specifying fully qualified name while trying to fetch the data in your application. Just specify the column name like resultset.getString("short_name");.

Cause even though you say select company.short_Name ... query out the column name as short_Name since that's what defined in table schema.

In case both tables has same column which may result in ambiguity, give a alias name to the columns like

select company.short_Name as company_shortname,
       location.short_Name as location_shortname,
location.location_name from company,location;



回答2:


When you do

select company.short_Name,location_name from company,location;

This query outs the column name short_Name and resultSet would also have short_Name

since the company.short_name doesnt exist you get an error.




回答3:


the function resultset.getString(String columnLabel)

Retrieves the value of the designated column in the current row of this ResultSet object as a String in the Java programming language.

Parameters:

columnLabel the label for the column specified with the SQL AS clause. If the SQL AS clause was not specified, then the label is the name of the column

Returns:

the column value; if the value is SQL NULL, the value returned is null Throws: SQLException - if the columnLabel is not valid; if a database access error occurs or this method is called on a closed result set

in the function resultset.getString(String columnLabel), the arg is a column name for executing sql, the statement select company.short_Name,location_name from company,location; will get a result set, which has table headers short_Name,location_name




回答4:


add the following to your application.properties file spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl



来源:https://stackoverflow.com/questions/31921482/the-column-name-is-not-valid-error

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