Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package db.kursus;
- import java.sql.*;
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.*;
- /**
- *
- * @author Aditya Pramana
- */
- public class DBLogin {
- private String url;
- private String username;
- private String password;
- private JFrame frame;
- private JPanel panel;
- private JLabel lblUsername;
- private JLabel lblPassword;
- private JTextField txtUsername;
- private JPasswordField txtPassword;
- private JButton btnLogin;
- private JButton btnCancel;
- private JButton btnClear;
- private JButton btnGuest;
- public DBLogin(){
- this.url = "jdbc:mysql://localhost:3306/kursus";
- initializeComponent();
- }
- void initializeComponent(){
- frame = new JFrame("Login as Admin");
- panel = new JPanel();
- lblUsername = new JLabel("Username");
- lblPassword = new JLabel("Password");
- txtUsername = new JTextField(28);
- txtPassword = new JPasswordField(28);
- btnGuest = new JButton("Guest Mode");
- btnLogin = new JButton("Login");
- btnCancel = new JButton("Cancel");
- btnClear = new JButton("Clear");
- txtPassword.setEchoChar('●');
- btnGuest.addActionListener(new btnGuestListener());
- btnLogin.addActionListener(new btnLoginListener());
- btnCancel.addActionListener(new btnCancelListener());
- btnClear.addActionListener(new btnClearListener());
- panel.add(lblUsername);
- panel.add(txtUsername);
- panel.add(lblPassword);
- panel.add(txtPassword);
- panel.add(btnGuest);
- panel.add(btnLogin);
- panel.add(btnCancel);
- panel.add(btnClear);
- frame.setResizable(false);
- frame.getContentPane().add(BorderLayout.CENTER, panel);
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.setSize(400, 150);
- frame.setVisible(true);
- txtUsername.requestFocus();
- }
- public class btnLoginListener implements ActionListener{
- @Override
- public void actionPerformed(ActionEvent e) {
- username = txtUsername.getText();
- password = String.valueOf(txtPassword.getPassword());
- try (Connection connection = DriverManager.getConnection(url, username, password)) {
- JOptionPane.showMessageDialog(null, "Login Succeeded");
- connection.close();
- DBInsertData t = new DBInsertData(url, username, password);
- frame.setVisible(false);
- }
- catch(SQLException ex){
- JOptionPane.showMessageDialog(null, "Login Failed");
- txtPassword.setText("");
- }
- }
- }
- public class btnCancelListener implements ActionListener{
- @Override
- public void actionPerformed(ActionEvent e) {
- System.exit(0);
- }
- }
- public class btnClearListener implements ActionListener{
- @Override
- public void actionPerformed(ActionEvent e) {
- txtUsername.setText("");
- txtPassword.setText("");
- txtUsername.requestFocus();
- }
- }
- public class btnGuestListener implements ActionListener{
- @Override
- public void actionPerformed(ActionEvent e){
- username = "guest";
- password = "guest123";
- DBUserReport t = new DBUserReport(url, username, password);
- frame.setVisible(false);
- }
- }
- /**
- * @param args the command line arguments
- */
- public static void main(String[] args) {
- DBLogin dbt = new DBLogin();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement