Advertisement
Guest User

Untitled

a guest
Aug 6th, 2017
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.19 KB | None | 0 0
  1. import java.awt.EventQueue;
  2. import java.lang.*;
  3. import javax.swing.JFrame;
  4. import javax.swing.JTextField;
  5. import javax.swing.JPasswordField;
  6. import javax.swing.JButton;
  7. import javax.swing.JLabel;
  8. import java.awt.event.ActionListener;
  9. import java.awt.event.ActionEvent;
  10. import java.sql.*;
  11.  
  12. import javax.swing.*;
  13.  
  14. public class Login {
  15.  
  16. Connection connection = null;
  17.  
  18. private JFrame frame;
  19. private JTextField username;
  20. private JPasswordField password;
  21.  
  22. /**
  23. * Launch the application.
  24. */
  25. public static void main(String[] args) {
  26. EventQueue.invokeLater(new Runnable() {
  27. public void run() {
  28. try {
  29. Login window = new Login();
  30. window.frame.setVisible(true);
  31. } catch (Exception e) {
  32. e.printStackTrace();
  33. }
  34. }
  35. });
  36. }
  37.  
  38. /**
  39. * Create the application.
  40. */
  41.  
  42. public Login() {
  43. initialize();
  44. connection = Database.dbconector();
  45.  
  46. }
  47.  
  48. /**
  49. * Initialize the contents of the frame.
  50. */
  51. private void initialize() {
  52. frame = new JFrame();
  53. frame.setBounds(100, 100, 450, 300);
  54. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  55. frame.getContentPane().setLayout(null);
  56.  
  57. username = new JTextField();
  58. username.setBounds(127, 55, 200, 50);
  59. frame.getContentPane().add(username);
  60. username.setColumns(10);
  61.  
  62. password = new JPasswordField();
  63. password.setBounds(127, 125, 200, 50);
  64. frame.getContentPane().add(password);
  65.  
  66. JButton btnLogin = new JButton("Log in");
  67. btnLogin.addActionListener(new ActionListener() {
  68. public void actionPerformed(ActionEvent e) {
  69.  
  70.  
  71. try {
  72. String query = "select * from bsit-db where Username=? and Password=?";
  73. PreparedStatement pst = connection.prepareStatement(query);
  74. pst.setString(1, username.getText());
  75. pst.setString(2, password.getText());
  76.  
  77.  
  78. ResultSet rs = pst.executeQuery();
  79. int count = 0;
  80. while (rs.next()) {
  81. count = count + 1;
  82. }
  83. if (count == 1) {
  84. JOptionPane.showInternalInputDialog(null, "Successfully Login to your account!");
  85.  
  86.  
  87.  
  88. } else {
  89. JOptionPane.showInternalInputDialog(null, "Incorrect ID/Password. Please Try Again!");
  90. }
  91. rs.close();
  92. pst.close();
  93.  
  94. } catch (Exception e1) {
  95. JOptionPane.showInternalInputDialog(null, e1);
  96.  
  97. } finally {
  98. try {
  99.  
  100. } catch (Exception e1) {
  101. JOptionPane.showInternalInputDialog(null, e1);
  102. }
  103. }
  104.  
  105.  
  106. }
  107. });
  108. btnLogin.setBounds(103, 202, 112, 37);
  109. frame.getContentPane().add(btnLogin);
  110.  
  111. JButton btnClear = new JButton("clear");
  112. btnClear.addActionListener(new ActionListener() {
  113. public void actionPerformed(ActionEvent arg0) {
  114.  
  115. username.setText(null);
  116. password.setText(null);
  117. }
  118. });
  119. btnClear.setBounds(237, 202, 112, 37);
  120. frame.getContentPane().add(btnClear);
  121.  
  122. JLabel lblUsername = new JLabel("username");
  123. lblUsername.setBounds(66, 55, 200, 50);
  124. frame.getContentPane().add(lblUsername);
  125.  
  126. JLabel lblPassword = new JLabel("password");
  127. lblPassword.setBounds(66, 125, 200, 50);
  128. frame.getContentPane().add(lblPassword);
  129. }
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement