以下是我的两个SQL Server表。
地址
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());
}
如果出现以下情况,它将失败:
如果通过,将通过:
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句