将结果集列值分配给变量以在另一个 SQL 语句中使用?爪哇

发布于 2024-12-10 20:22:22 字数 793 浏览 0 评论 0原文

我正在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

最舍不得你 2024-12-17 20:22:22

您忘记调用 ResultSet.next ().

if(rset1.next())
 {
   Integer custID = rset1.getInt(3);
   ....
 }

You foget to invoke ResultSet.next().

if(rset1.next())
 {
   Integer custID = rset1.getInt(3);
   ....
 }
云柯 2024-12-17 20:22:22

您发布的内容可以而且应该在单个查询中完成 - 不太复杂,并且更少[不必要的]流量返回和返回。与数据库进行交互:

SELECT q.*
  FROM QUOTE q
 WHERE EXISTS (SELECT NULL 
                FROM CUSTOMER c 
                JOIN WORKORDER wo ON wo.custid = c.custid
               WHERE c.quoteid = q.quoteid
                 AND wo.workorderid = ?)

不使用 JOIN 的原因是,如果有多个工作订单/客户/等相关,则存在重复 QUOTE 值的风险。

另外:

  • 数字数据类型(quoteid、custid 等)不应包含在单引号中 - 无需依赖隐式数据类型转换。
  • 您应该使用参数化查询,而不是动态 SQL

What you posted can and should be done in a single query - less complex, and less [unnecessary] traffic back & forth with the database:

SELECT q.*
  FROM QUOTE q
 WHERE EXISTS (SELECT NULL 
                FROM CUSTOMER c 
                JOIN WORKORDER wo ON wo.custid = c.custid
               WHERE c.quoteid = q.quoteid
                 AND wo.workorderid = ?)

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:

  • Numeric data types (quoteid, custid, etc) should not be wrapped in single quotes - there's no need to rely on implicit data type conversion.
  • You should be using parameterized queries, not dynamic SQL
绝情姑娘 2024-12-17 20:22:22

OMG Ponies 提供的注释非常重要,但并没有真正回答问题。 AVD 也是正确的。我已经对其进行了一些清理并包含了准备好的声明。请使用准备好的陈述。它们会帮助您晚上入睡。

PreparedStatement pstmt1 = con.prepareStatement(
    "SELECT * FROM WorkOrder WHERE WorkOrderID = ?");
PreparedStatement pstmt2 = con.prepareStatement(
    "SELECT * FROM Customer WHERE CustID = ?");
PreparedStatement pstmt3 = con.prepareStatement(
    "SELECT * FROM Quote WHERE QuoteID = ?");

pstmt1.setInt(1, workOrderId)
ResultSet rset1 = pstmt1.executeQuery();

// test validity of rset1 
if(rset1.next()) {
    pstmt2.setInt(1, rset1.getInt(3))
    ResultSet rset2 = pstmt2.executeQuery();

    // test validity of rset2
    if(rset2.next()) {
        pstmt3.setInt(1, rset1.getInt(2))
        ResultSet rset3 = pstmt3.executeQuery();
    }
}

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.

PreparedStatement pstmt1 = con.prepareStatement(
    "SELECT * FROM WorkOrder WHERE WorkOrderID = ?");
PreparedStatement pstmt2 = con.prepareStatement(
    "SELECT * FROM Customer WHERE CustID = ?");
PreparedStatement pstmt3 = con.prepareStatement(
    "SELECT * FROM Quote WHERE QuoteID = ?");

pstmt1.setInt(1, workOrderId)
ResultSet rset1 = pstmt1.executeQuery();

// test validity of rset1 
if(rset1.next()) {
    pstmt2.setInt(1, rset1.getInt(3))
    ResultSet rset2 = pstmt2.executeQuery();

    // test validity of rset2
    if(rset2.next()) {
        pstmt3.setInt(1, rset1.getInt(2))
        ResultSet rset3 = pstmt3.executeQuery();
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文