【问题标题】:What's wrong with my JDBC select prepared statement? [duplicate]我的 JDBC 选择准备好的语句有什么问题? [复制]
【发布时间】:2016-05-27 20:50:19
【问题描述】:

我正在尝试在 java 中调试我准备好的语句,但我被困在我实现的这个 checkEmail 函数上。当我进入调试并到达setString 行时,它显示未指定代替'?'。如果我将'findEmail' 硬编码到字符串查询中,它将起作用并找到电子邮件。这是一段代码:

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;

}

【问题讨论】:

  • 你需要ResultSet rs = preparedStatement.executeQuery();
  • 该死的,我认为这很简单。谢谢!

标签: java jdbc


【解决方案1】:

尝试替换这个:

  ResultSet rs = preparedStatement.executeQuery(query);

与:

  ResultSet rs = preparedStatement.executeQuery();

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

【讨论】:

  • 谢谢!可能是之前没准备好转过来忘记拆了。
猜你喜欢
  • 2015-03-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-04-25
  • 2018-10-21
  • 2011-07-13
  • 2014-06-30
相关资源
最近更新 更多