Java SQL访问行数据

时间:2017-04-09 12:49:31

标签: java sql jdbc

我写了一个Java应用程序,用户可以在其中登录/注册。这已经有效了。 但不知何故,当我尝试编写逻辑来检查用户是否已经存在时,我收到错误错误。

这是我的代码:

public static void checkRegister(String mail) {

        try {
            // create a mysql database connection

            Class.forName(myDriver);
            Connection conn = DriverManager.getConnection(myUrl, user, passwordDB);

              // the mysql insert statement
              String query = "SELECT COUNT(email) mailc, email from users where users.email = \"" + mail + "\"";

              // create the mysql insert preparedstatement
              PreparedStatement preparedStmt = conn.prepareStatement(query);

              // execute the preparedstatement
              preparedStmt.execute();
            conn.close();
        } catch (Exception e) {
            System.err.println("Got an exception!");
            System.err.println(e.getMessage());
        }
    }

我尝试使用preparedStmt.getResultSet().getInt("mailc"),但它不起作用。

示例邮件尚未注册

example mail not registered yet

已注册的示例邮件

enter image description here

我的目的是检查mailc是否是> 0

4 个答案:

答案 0 :(得分:3)

您必须使用 AS Count(c) AS col这样:

String query = "SELECT COUNT(email) AS mailc, email from users ....";

我建议改为使用PreparedStatement的?,以避免任何SQL注入。所以你的查询应该是这样的:

String query = "SELECT COUNT(email) mailc, email from users where users.email =?";
PreparedStatement preparedStmt = conn.prepareStatement(query);
preparedStmt.setString(1, mail);

要获得结果,您必须使用ResultSet:

String query = "SELECT COUNT(email) AS mailc, email from users where users.email =?";
PreparedStatement preparedStmt = conn.prepareStatement(query);
preparedStmt.setString(1, mail);

ResultSet result = preparedStmt.executeQuery();
while (result.next()) {
   int count = result.getInt("mailc");
   String email = result.getString("email");
}

答案 1 :(得分:2)

ResultSet rs = stmt.executeQuery(query);

您的代码可能有效,声明不完美但有效。你能提供这个例子,我们会解决吗? 或者您可以尝试使用'而不是"在你的QUERY中

答案 2 :(得分:1)

在这里做一些猜测:你想检查users表是否已经拥有至少一个具有该电子邮件地址的用户,并且该查询将始终在一个给定地址上运行,而不是在地址列表上运行。< / p>

您可能需要考虑重新考虑此问题:您想检查该值是否存在,但是您要求数据库计算有多少封电子邮件。这个特定的例子可能没问题,因为根本就只有一个这样的用户或根本没有用户。但是在更复杂的用例中,您实际上会强制您的数据库执行它应该做的更多工作,这将达到您的应用程序性能。

我建议您应该使用的正确查询实际上就是这个:

select case when
  exists (select 1 from users where mail = ?)
  then 1 else 0 end as answer
from dual //this is a synthetic table which some DB vendors need.
          //that whole line can be removed if your DB vendor doesn't require it.

各自的Java代码是:

void checkRegistered(String email) {
  initDriver();
  // note the try block below: it will close all resources you have allocated
  try (Connection conn = DriverManager.getConnection(uri, credentials.name, credentials.pwd);
       PreparedStatement st = conn.prepareStatement(query);
       ResultSet result = st.executeQuery()) {
    if (result.next() && result.getInt("answer") == 1) {
      //do here whatever you do when user is invalid.
    }
  }
}

答案 3 :(得分:0)

请修改您的代码:

String query = "SELECT COUNT(email) mailc, email from users where users.email =?";

// create the mysql insert preparedstatement
PreparedStatement preparedStmt = conn.prepareStatement(query);
preparedStmt.setString(1,mail);
// execute the preparedstatement
preparedStmt.execute();