PreparedStatement获取带有2个字符串的对象

时间:2014-10-07 07:30:23

标签: java mysql jdbc

我正在使用MYSQL 5.6 当我尝试上面的代码时,ResultSet返回null。

public Client viewClient(String clientName, String password) throws Exception {
    String sql = "SELECT * FROM main.clients where client_name = ? and client_password = ?;";
    try (PreparedStatement pstmt = connection.prepareStatement(sql)) {
        pstmt.setString(1, clientName);
        pstmt.setString(2, password);// ;(1, clientID);
        ResultSet resultset = pstmt.executeQuery();
        pstmt.closeOnCompletion();
        while (resultset.next()) {
            return new Client(resultset.getLong("client_id"),
                    resultset.getString("client_name"),
                    resultset.getString("client_password"),
                    ClientType.valueOf(resultset.getString("type")),
                    resultset.getString("address"),
                    resultset.getString("email"),
                    resultset.getString("phone"),
                    resultset.getString("comment"));
        }
    } catch (SQLException e) {
        throw new Exception(e.getMessage());
    }
    return null;
}

即使在下面的代码中使用Statment,ResultSet也会返回Object。

public Client viewClient(String clientName, String password) throws Exception {
    String sql = "SELECT * FROM main.clients where client_name = 'John' and client_password = '1234';";
     try (Statement statement = connection.createStatement()) {
        ResultSet resultset = statement.executeQuery(sql);
        while (resultset.next()) {
            return new Client(resultset.getLong("client_id"),
                    resultset.getString("client_name"),
                    resultset.getString("client_password"),
                    ClientType.valueOf(resultset.getString("type")),
                    resultset.getString("address"),
                    resultset.getString("email"),
                    resultset.getString("phone"),
                    resultset.getString("comment"));
        }
    } catch (SQLException e) {
        throw new Exception(e.getMessage());
    }
    return null;
}

我的语法有问题吗?或者PreparedStatment存在一般性问题?我不知道吗? 谢谢

0 个答案:

没有答案