Advertisement
Guest User

Untitled

a guest
Jan 21st, 2020
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.41 KB | None | 0 0
  1. public User getBy(String login, String password) throws DAOException {
  2. if(login == null || password == null){
  3. throw new DAOException(MSG_GETBY_EXC + "NULL VALUE");
  4. }
  5. Connection connection = null;
  6. PreparedStatement statement = null;
  7. String sql = "SELECT ID, LOGIN, PASSWORD FROM USER WHERE LOGIN=? AND PASSWORD=?";
  8.  
  9. User user = new User();
  10. try {
  11. connection = connector.getConnection();
  12. statement = connection.prepareStatement(sql);
  13.  
  14. statement.setString(1, user.getLogin());
  15. statement.setString(2, user.getPassword());
  16.  
  17.  
  18. ResultSet resultSet = statement.executeQuery();
  19.  
  20. user.setId(resultSet.getInt("ID"));
  21. user.setLogin(resultSet.getString("LOGIN"));
  22. user.setPassword(resultSet.getString("PASSWORD"));
  23.  
  24. statement.executeUpdate();
  25.  
  26. } catch (ConnectorException | SQLException e) {
  27. throw new DAOException(MSG_GETBY_EXC, e);
  28. } finally {
  29. try {
  30. if(statement != null){
  31. statement.close();
  32. }
  33. if(connection != null){
  34. connection.close();
  35. }
  36. } catch (SQLException e) {
  37. throw new DAOException(MSG_CLOSE_EXC, e);
  38. }
  39. }
  40. return user;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement