我的JDBC select prepared语句出了什么问题?

时间:2016-02-16 14:14:55

标签: java jdbc

我试图在java中调试我准备好的语句,并且我坚持使用我实现的checkEmail函数。当我进入调试并且它到达setString行时,它显示NOT SPECIFIED而不是'?'。如果我将'findEmail'硬编码到String查询中,它将工作并找到该电子邮件。这是一段代码:

public static boolean checkEmail(String findEmail) {
    Connection conn = EstablishConnection.conn;
    boolean found = false;
    try {
        String query = "SELECT email FROM customers WHERE email=?";
        Logging.debug(query);
        PreparedStatement preparedStatement = conn.prepareStatement(query);
        preparedStatement.setString(1,findEmail);
        ResultSet rs = preparedStatement.executeQuery(query);
        //Iterate through the results of the query
        if (rs.next()) {
            found = true;
        }
        preparedStatement.close();
    } catch (Exception e) {
        Logging.debug("Exception thrown in CustomerOperations.getCustomerInfo(): " + e.getMessage());
        e.printStackTrace();
    }
    return found;

}

1 个答案:

答案 0 :(得分:2)

尝试替换它:

  ResultSet rs = preparedStatement.executeQuery(query);

使用:

  ResultSet rs = preparedStatement.executeQuery();

因为您已将查询传递给prepareStatementconn.prepareStatement(query);