使用 JDBC 从多个表中进行 SELECT 时如何识别列?
我在 id 列上加入了两个表,它们看起来像:
+-------+
| users |
+----+--+---+
| id | name |
+----+------+
+-------+
| posts |
+-------+------+---------+
| id | user_id | message |
+----+---------+---------+
现在我想选择所有帖子并包含用户名,其中:
SELECT * FROM posts, users WHERE user_id = users.id
然后我尝试通过以下方式获取值:
ResultSet rs = // SQL
if(rs.next()) {
rs.getInt("posts.id");
...
}
但我得到 SQLException< /code> 执行
rs.getInt("posts.id")
时:
java.sql.SQLException: Column 'posts.id' not found.
如何使用 JDBC 和 JavaDB/Derby 作为数据库从上面的 SQL 查询中获取值?
使用 ResultSet
检索值时,如何区分 users
和 posts
表中的 id
列?
I have two tables that I join on the id-column, they look like:
+-------+
| users |
+----+--+---+
| id | name |
+----+------+
+-------+
| posts |
+-------+------+---------+
| id | user_id | message |
+----+---------+---------+
And now I want to select all posts and include the username, with:
SELECT * FROM posts, users WHERE user_id = users.id
And then I try to get the values with:
ResultSet rs = // SQL
if(rs.next()) {
rs.getInt("posts.id");
...
}
But I get SQLException
when executing rs.getInt("posts.id")
:
java.sql.SQLException: Column 'posts.id' not found.
How can I get the values from the SQL-query above using JDBC, and JavaDB/Derby as database?
How can I distinguish between the id
column in the users
and posts
table when retrieving values with ResultSet
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您正在尝试检索
id
值,但您正在使用“posts.id”来引用它。 不要您需要的只是列名称或别名,而不是表名称:
如果您的列名称本身是“posts.id”,那么它就可以工作,但如果您选择更新表,我建议使用下划线 (_) 而不是句点。
但是我在两个表中都有一个 id 列,我如何区分它们?
您需要指定一个列别名:
...并在 Java 代码中引用该列别名:
You're attempting to retrieve the
id
value, but you're using "posts.id" to reference it. Don'tAll you need is the column name or alias, not the table name as well:
It would've worked if your column name itself was "posts.id", but I recommend using underscore (_) instead of a period should you choose to update the table.
But I have an id column in both tables, how do i distinguish between them?
You need to specify a column alias:
...and reference that column alias in the Java code:
解决方案 1:使用别名
解决方案 2:删除重复的 user.id,因为您已经在 posts 表中拥有它
Solution 1 : use alias
Solution 2: remove the duplicate user.id since you already have it in the posts table
根据您的 sql 风格为列名设置别名
,该别名可能需要是
Alias the column names
depending on your sql flavour, that alias may need to be
要获取值,您应该使用
ResultSet
的列名称。ResultSet
中使用的列名将是原始列名或别名。如果您的
ResultSet
具有相同名称或别名的列,则getInt()
将返回第一个匹配项。通过您的选择...
...
rs.getInt()
将仅返回posts.id
,因为它将是第一列。您可以为每个列使用唯一的别名,也可以使用
ResultSetMetaData
SQL
Java
ResultSetMetaData
SQL
Java
To get values, you should use the
ResultSet
's column name. The column name used in theResultSet
will be original column name or the alias.If your
ResultSet
has columns with the same name or alias, thegetInt()
will return the first match.With your select ...
...the
rs.getInt()
will return only theposts.id
since it will the first column.You can use a unique alias for every column or you can use the
ResultSetMetaData
SQL
Java
ResultSetMetaData
SQL
Java
您可以通过根据列索引获取数据而不是通过列名称获取数据来进一步简化这些......!!我们从数据库检索的任何数据都根据 select 语句按列顺序存储在结果集中,因此不是根据列名称获取数据,而是根据结果集中的列索引获取数据。
例如:
这些将帮助您消除由于数据库表中相似的列名而产生的复杂性和混乱。
希望这些有所帮助...祝
一切顺利..!
you can simplify these further by getting data as per the column index instead of getting it by column name..!! what ever data we retrieve from DB is stored in result set in the column order as per the select statement so instead of getting data as per the column name prefer fetching data as per the column index in result set..
ex:
these will help you do away with complexity and confusion created due to similar column names in your database tables..
Hope these helps...
All the best..!!