Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private void login() {
- if (!validateAllInput()) {
- return;
- }
- try {
- String username = usernameField.getText();
- String password = String.valueOf(passwordField.getPassword());
- UserDao userDao = new UserDaoImpl();
- User user = userDao.findBy(username);
- boolean granted = isGranted(user, password, username);
- if (granted) {
- startMainActivity(user);
- }
- } catch (SQLException e) {
- e.printStackTrace();
- Throwable cause = e.getCause();
- if (cause instanceof ConnectException) {
- showWarning("Tidak dapat menghubungkan ke database server");
- return;
- }
- showWarning("Tidak bisa menghubungkan ke database \n\n"
- + "Error : " + e.getMessage());
- }
- }
- private boolean isGranted(User user, String password, String username) {
- if (user == null) {
- showWarning("Tidak menemukan user dengan username : " + username);
- return false;
- }
- if (!user.getPassword().equals(password)) {
- showWarning("Password salah");
- return false;
- }
- return true;
- }
- private void startMainActivity(User user) {
- Session.getInstance().setUser(user);
- MainActivity frame = new MainActivity();
- frame.setVisible(true);
- frame.setLocationRelativeTo(null);
- dispose();
- }
- private boolean validateAllInput() {
- return validateUsername() && validatePassword();
- }
- private boolean validateUsername() {
- String username = usernameField.getText();
- if (username.isEmpty()) {
- showWarning("Username masih kosong");
- return false;
- }
- return true;
- }
- private boolean validatePassword() {
- String password = String.valueOf(passwordField.getPassword());
- if (password.isEmpty()) {
- showWarning("Password masih kosong");
- return false;
- }
- return true;
- }
- private void showWarning(String message) {
- JOptionPane.showMessageDialog(this, message, "Gagal",
- JOptionPane.ERROR_MESSAGE);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement