如何选择用户名等于字符串的密码

时间:2019-06-01 12:35:52

标签: java sqlite jdbc

我想选择密码字符串,其中用户名等于我从函数获取的字符串。

public String GetPassOfAnUsername(String username){

        String password = "SELECT Password FROM Person WHERE UserName = ???";

        try (Connection connection = this.connect();
             Statement statement  = connection.createStatement();
             ResultSet results    = statement.executeQuery(password)){

        } catch (SQLException e) {
            System.out.println(e.getMessage());
        }

       return password;

    }

    public static void main(String[] args) {
        String userpass = app.GetPassOfAnUsername("theusername");
    }

1 个答案:

答案 0 :(得分:0)

您可以将值设置为“ ???”,而不是使用 PreparedStatement 来使用 Statement 。在您的声明字符串中:

  String password = "SELECT Password FROM Person WHERE UserName = ?";
  PreparedStatement statement  = connection.prepareStatement(password);
  statement.setString(1, username);
  ResultSet rs = preparedStatement.executeQuery();
  if(rs.next())
      return rs.getString("Password");
  else
      return null;