gpt4 book ai didi

java - 使用 SQL 子查询计算表中的行数,并根据结果通过/失败 java 测试

转载 作者:太空宇宙 更新时间:2023-11-04 10:06:03 25 4
gpt4 key购买 nike

下面是我的 2 个 SQL Server 表。

T地址

addressId | addressName |
2 | testAddress |

链接

linkId | clientId | addressId |
1 | 4 | 2 |

我正在尝试编写一个 java 测试,该测试使用 SQL 子查询来检查 taddress 中是否存在记录。

例如:

SELECT COUNT(*) FROM Taddress
WHERE addressId =
(SELECT addressId FROM Tlink
WHERE clientId = param)

目前,当我运行以下测试时,即使 tLink 中不存在 clientId 参数,测试也始终会通过。

try {
dbAccessSetUp();
ResultSet rs = stmt.executeQuery("SELECT COUNT(*) FROM taddress a INNER JOIN tlink l on a.address_id = l.internal_address_id WHERE l.ext_client_id =" + this.clientNo);

if(!rs.next()) {
fail("Record does not exist in taddress based on ExtClientNo");
}

int count = 0;

while(rs.next()) {

count = rs.getInt(1);
System.out.println("number of count : " + count);
assertTrue(0 < count);
}
rs.close();
} catch(SQLException se) {
se.printStackTrace();
assertEquals(true, false);
} catch(Exception e) {
e.printStackTrace();
assertEquals(true, false);
} finally {
try {
if(stmt!=null) stmt.close();
} catch(SQLException se2) {
assertEquals(true, false);
}
try {
if(conn!=null) conn.close();
} catch(SQLException se) {
se.printStackTrace();
assertEquals(true, false);
}
}

最佳答案

根据上面的代码示例,这应该适合您:

try {
dbAccessSetUp();
ResultSet rs = stmt.executeQuery("SELECT COUNT(*)FROM taddress A INNER JOIN tlink L ON A.address_id = L.internal_address_id WHERE L.ext_client_id =" + this.clientNo);
int count=0;
while(rs.next()) {
count = ((Number) rs.getObject(1)).intValue();

}

if(count > 0) {
assertTrue(true);
System.out.println(count + " record(s) in taddress based on ExtClientNo");
}
else {
fail("No records in taddress");
}

}catch(Exception ex) {
System.out.println(ex.getMessage());
}

如果出现以下情况,这将失败:

  • tlink 中没有与您的客户匹配的记录否
  • tlink中有匹配的记录,但taddress中没有匹配的记录

如果满足以下条件,则会通过:

  • tlink 中有匹配记录 & taddress 中有匹配记录

关于java - 使用 SQL 子查询计算表中的行数,并根据结果通过/失败 java 测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52906656/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com