如何使用子选择与PreparedStatements结合使用?

时间:2013-05-16 07:54:04

标签: java sql database jdbc

例如,我有以下SQL代码:

SELECT * 
FROM customer c 
WHERE 2 = (SELECT count(*)
           FROM account a
           WHERE a.cust_id = c.cust_id);

如何将该SQL语句转换为预准备语句命令?

1 个答案:

答案 0 :(得分:1)

String queryString = " SELECT * FROM customer c";
queryString += " WHERE ? = (SELECT count(*) FROM account a WHERE a.cust_id = c.cust_id)";

PreparedStatement stmt = connection.prepareStatement(queryString);
stmt.setInt(1, theCountYouLookFor) // theCountYouLookFor would be 2 in your example