因此,您可以使用类似
select count(*) ...或
select (count(*) > 0) as found ...作为基本查询的内容。
当您致电时
executeQuery,您将得到一个
ResultSet回报,您需要确定它的内容。
在您的情况下,您只期望得到单行结果,因此您可以简单地使用
ResultSet#next移动到第一行,然后从中提取列值…
public void ftpTableCheck(String host, String port, String username, String password) { try { String query = "SELECT (count(*) > 0) as found FROM ftp WHERe Host LIKE ? AND Username LIKE ?"; PreparedStatement pst = conn.prepareStatement(query); pst.setString(1, host); pst.setString(2, username); try (ResultSet rs = pst.executeQuery()) { // only expecting a single result if (rs.next()) { boolean found = rs.getBoolean(1); // "found" column if (found) { // You have rows } else { // You have no rows } } } } catch (SQLException e) { e.printStackTrace(); }}就拿仔细看看JDBC数据库访问和使用预处理语句了解更多详情



