将结果集列值分配给变量以在另一个 SQL 语句中使用?爪哇
我正在 Java 中创建一个以数据为中心的 Web 服务,以便部署到 Glassfish。到目前为止,除了一种方法之外,我的所有方法都工作正常。
我正在尝试将结果集中的值分配给一个变量,以便按照下面的代码在另一个 SQL 语句中使用。我不确定它是否可能,或者我的 SQL 是否错误,但任何想法将不胜感激。
ResultSet rset1 = stmt1.executeQuery("SELECT *
FROM WorkOrder
WHERE WorkOrderID = '"+workOrderID+"'");
Integer custID = rset1.getInt(3);
ResultSet rset2 = stmt2.executeQuery("SELECT *
FROM Customer
WHERE CustID = '"+custID+"'");
Integer quoteID = rset1.getInt(2);
ResultSet rset3 = stmt3.executeQuery("SELECT *
FROM Quote
WHERE QuoteID = '"+quoteID+"'");
I am creating a data centric webservice in Java for deployment to Glassfish. All of my methods so far are working correctly except for one.
I am attempting to assign a value from a result set to a variable to use in another SQL statement as per the below code. I am not sure if its possible, or if perhaps my SQL is wrong, but any ideas would be appreciated.
ResultSet rset1 = stmt1.executeQuery("SELECT *
FROM WorkOrder
WHERE WorkOrderID = '"+workOrderID+"'");
Integer custID = rset1.getInt(3);
ResultSet rset2 = stmt2.executeQuery("SELECT *
FROM Customer
WHERE CustID = '"+custID+"'");
Integer quoteID = rset1.getInt(2);
ResultSet rset3 = stmt3.executeQuery("SELECT *
FROM Quote
WHERE QuoteID = '"+quoteID+"'");
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您忘记调用 ResultSet.next ().
You foget to invoke ResultSet.next().
您发布的内容可以而且应该在单个查询中完成 - 不太复杂,并且更少[不必要的]流量返回和返回。与数据库进行交互:
不使用 JOIN 的原因是,如果有多个工作订单/客户/等相关,则存在重复 QUOTE 值的风险。
另外:
What you posted can and should be done in a single query - less complex, and less [unnecessary] traffic back & forth with the database:
The reason this didn't use JOINs is because there'd be a risk of duplicate
QUOTE
values if there's more than one workorder/customer/etc related.Additionally:
OMG Ponies 提供的注释非常重要,但并没有真正回答问题。 AVD 也是正确的。我已经对其进行了一些清理并包含了准备好的声明。请使用准备好的陈述。它们会帮助您晚上入睡。
The note provided by OMG Ponies was really important to take note of, but does not really answer the question. AVD was also correct. I've cleaned it up a bit and included prepared statements. Please use prepared statements. They will help you sleep at night.