java 列名无效 - 错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31921482/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
The column name is not valid - error
提问by Mitul Chauhan
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");
that time this give the above error.
这个查询在 SQL Server 上执行得很好,但是在我的 java 代码中,当我尝试检索这样的值resultset.getString("company.short_name");
时,会出现上述错误。
CASE 2 :
案例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.
并检索值resultset.getString("short_name");
比它在数据库 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 中不起作用?
回答by Rahul
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("company.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;
回答by ihappyk
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 不存在,您会收到错误消息。
回答by Javy
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. If the SQL AS clause was not specified, then the label is the name of the column
columnLabel 使用 SQL AS 子句指定的列的标签。如果未指定 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; if a database access error occurs or this method is called on a closed result set
列值;如果值为 SQL NULL,则返回值为 null 抛出: SQLException - 如果 columnLabel 无效;如果发生数据库访问错误或在关闭的结果集上调用此方法
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
在函数中resultset.getString(String columnLabel)
,arg
是执行sql的列名,语句select company.short_Name,location_name from company,location;
会得到一个结果集,里面有表头short_Name,location_name