简体   繁体   English

列名无效 - 错误

[英]The column name is not valid - error

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错误:com.microsoft.sqlserver.jdbc.SQLServerException:列名 company.short_name 无效

CASE 1 :情况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");这个查询在 SQL Server 上执行得很好,但在我的 java 代码中,当我尝试检索像resultset.getString("company.short_name");这样的值时resultset.getString("company.short_name"); that time this give the above error.那个时候这给出了上述错误。

CASE 2 :案例2:

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

and retrieve value like resultset.getString("short_name");并检索值,如resultset.getString("short_name"); than it work fine with both database MySQL and MSSQL.比它在数据库 MySQL 和 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?我正在将我的数据库从 MySQL 迁移到 MSSQL。上面的案例 1 在 MySQL 中工作正常,但为什么它在 MSSQL 中不起作用?

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");只需指定列名,如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.原因即使你说select company.short_Name ...查询列名作为short_Name因为那是在表模式中定义的。

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;

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此查询超出列名 short_Name 和 resultSet 也将具有 short_Name

since the company.short_name doesnt exist you get an error.由于 company.short_name 不存在,您会收到错误消息。

the function resultset.getString(String columnLabel)函数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.以 Java 编程语言中的 String 形式检索此 ResultSet 对象的当前行中指定列的值。

Parameters:参数:

columnLabel the label for the column specified with the SQL AS clause. columnLabel 使用 SQL AS 子句指定的列的标签。 If the SQL AS clause was not specified, then the label is the name of the column如果未指定 SQL AS 子句,则标签是列的名称

Returns:返回:

the column value;列值; if the value is SQL NULL, the value returned is null Throws: SQLException - if the columnLabel is not valid;如果值为 SQL NULL,则返回值为 null 抛出: SQLException - 如果 columnLabel 无效; 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;函数resultset.getString(String columnLabel)arg是执行sql的列名,语句select company.short_Name,location_name from company,location; will get a result set, which has table headers short_Name,location_name将获得一个结果集,其中包含表头short_Name,location_name

将以下内容添加到您的 application.properties 文件 spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM