Advertisement
ojoakua

DBLogin.java

Dec 14th, 2017
262
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.87 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package db.kursus;
  7. import java.sql.*;
  8. import javax.swing.*;
  9. import java.awt.*;
  10. import java.awt.event.*;
  11.  
  12. /**
  13.  *
  14.  * @author Aditya Pramana
  15.  */
  16. public class DBLogin {
  17.     private String url;
  18.     private String username;
  19.     private String password;
  20.    
  21.     private JFrame frame;
  22.     private JPanel panel;
  23.     private JLabel lblUsername;
  24.     private JLabel lblPassword;
  25.     private JTextField txtUsername;
  26.     private JPasswordField txtPassword;
  27.     private JButton btnLogin;
  28.     private JButton btnCancel;
  29.     private JButton btnClear;
  30.     private JButton btnGuest;
  31.    
  32.     public DBLogin(){
  33.         this.url = "jdbc:mysql://localhost:3306/kursus";
  34.         initializeComponent();
  35.     }
  36.    
  37.     void initializeComponent(){
  38.         frame = new JFrame("Login as Admin");
  39.         panel = new JPanel();
  40.         lblUsername = new JLabel("Username");
  41.         lblPassword = new JLabel("Password");
  42.         txtUsername = new JTextField(28);
  43.         txtPassword = new JPasswordField(28);
  44.         btnGuest = new JButton("Guest Mode");
  45.         btnLogin = new JButton("Login");
  46.         btnCancel = new JButton("Cancel");
  47.         btnClear = new JButton("Clear");
  48.        
  49.         txtPassword.setEchoChar('●');
  50.         btnGuest.addActionListener(new btnGuestListener());
  51.         btnLogin.addActionListener(new btnLoginListener());
  52.         btnCancel.addActionListener(new btnCancelListener());
  53.         btnClear.addActionListener(new btnClearListener());
  54.         panel.add(lblUsername);
  55.         panel.add(txtUsername);
  56.         panel.add(lblPassword);
  57.         panel.add(txtPassword);
  58.         panel.add(btnGuest);
  59.         panel.add(btnLogin);
  60.         panel.add(btnCancel);
  61.         panel.add(btnClear);
  62.        
  63.         frame.setResizable(false);
  64.         frame.getContentPane().add(BorderLayout.CENTER, panel);
  65.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  66.         frame.setSize(400, 150);
  67.         frame.setVisible(true);
  68.         txtUsername.requestFocus();
  69.     }
  70.    
  71.     public class btnLoginListener implements ActionListener{
  72.         @Override
  73.         public void actionPerformed(ActionEvent e) {
  74.             username = txtUsername.getText();
  75.             password = String.valueOf(txtPassword.getPassword());
  76.             try (Connection connection = DriverManager.getConnection(url, username, password)) {
  77.                 JOptionPane.showMessageDialog(null, "Login Succeeded");
  78.                 connection.close();
  79.                 DBInsertData t = new DBInsertData(url, username, password);
  80.                 frame.setVisible(false);
  81.             }
  82.             catch(SQLException ex){
  83.                 JOptionPane.showMessageDialog(null, "Login Failed");
  84.                 txtPassword.setText("");
  85.             }
  86.         }
  87.     }
  88.    
  89.     public class btnCancelListener implements ActionListener{
  90.         @Override
  91.         public void actionPerformed(ActionEvent e) {
  92.             System.exit(0);
  93.         }
  94.     }
  95.    
  96.     public class btnClearListener implements ActionListener{
  97.         @Override
  98.         public void actionPerformed(ActionEvent e) {
  99.             txtUsername.setText("");
  100.             txtPassword.setText("");
  101.             txtUsername.requestFocus();
  102.         }
  103.     }
  104.    
  105.     public class btnGuestListener implements ActionListener{
  106.         @Override
  107.         public void actionPerformed(ActionEvent e){
  108.             username = "guest";
  109.             password = "guest123";
  110.             DBUserReport t = new DBUserReport(url, username, password);
  111.             frame.setVisible(false);
  112.         }
  113.     }
  114.    
  115.     /**
  116.      * @param args the command line arguments
  117.      */
  118.     public static void main(String[] args) {
  119.         DBLogin dbt = new DBLogin();
  120.     }
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement