Advertisement
Guest User

login

a guest
May 7th, 2019
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.92 KB | None | 0 0
  1. import java.awt.EventQueue;
  2.  
  3. import javax.swing.JFrame;
  4. import java.sql.*;
  5. import javax.swing.*;
  6. import java.awt.event.ActionListener;
  7. import java.awt.event.ActionEvent;
  8. import java.awt.Font;
  9.  
  10. public class Login {
  11.  
  12. private JFrame frame;
  13.  
  14. /**
  15. * Launch the application.
  16. */
  17. public static void main(String[] args) {
  18. EventQueue.invokeLater(new Runnable() {
  19. public void run() {
  20. try {
  21. Login window = new Login();
  22. window.frame.setVisible(true);
  23. } catch (Exception e) {
  24. e.printStackTrace();
  25. }
  26. }
  27. });
  28. }
  29. Connection connection=null;
  30. private JTextField textFieldUN;
  31. private JPasswordField passwordField;
  32.  
  33. /**
  34. * Create the application.
  35. */
  36. public Login() {
  37. initialize();
  38. connection=sqliteConnection.dbConnector();
  39. }
  40.  
  41. /**
  42. * Initialize the contents of the frame.
  43. */
  44. private void initialize() {
  45. frame = new JFrame();
  46. frame.setBounds(100, 100, 450, 300);
  47. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  48. frame.getContentPane().setLayout(null);
  49.  
  50. JLabel lblNewLabel = new JLabel("Username:");
  51. lblNewLabel.setBounds(12, 75, 97, 16);
  52. frame.getContentPane().add(lblNewLabel);
  53.  
  54. JLabel lblNewLabel_1 = new JLabel("Password:");
  55. lblNewLabel_1.setBounds(12, 139, 97, 16);
  56. frame.getContentPane().add(lblNewLabel_1);
  57.  
  58. textFieldUN = new JTextField();
  59. textFieldUN.setBounds(12, 104, 116, 22);
  60. frame.getContentPane().add(textFieldUN);
  61. textFieldUN.setColumns(10);
  62.  
  63. passwordField = new JPasswordField();
  64. passwordField.setBounds(12, 168, 116, 22);
  65. frame.getContentPane().add(passwordField);
  66.  
  67. JButton btnLogin = new JButton("Login");
  68. btnLogin.addActionListener(new ActionListener() {
  69. public void actionPerformed(ActionEvent arg0) {
  70.  
  71. try {
  72. String query="select * from Users where UserName=? and Password=? ";
  73. PreparedStatement pst=connection.prepareStatement(query);
  74. pst.setString(1, textFieldUN.getText());
  75. pst.setString(2, passwordField.getText());
  76.  
  77. ResultSet rs=pst.executeQuery();
  78. int count =0;
  79. while(rs.next()) {
  80. count=count+1;
  81. }
  82. if(count ==1)
  83. {
  84. JOptionPane.showMessageDialog(null, "Authentification Successfull!");
  85. frame.dispose();
  86. Events ev = new Events();
  87. ev.setVisible(true);
  88. }
  89. else if(count>1)
  90. {
  91. JOptionPane.showMessageDialog(null, "Duplicate Username or Password");
  92. }
  93. else
  94. {
  95. JOptionPane.showMessageDialog(null, "Authentification failed!");
  96. }
  97. rs.close();
  98. pst.close();
  99.  
  100. }catch(Exception e)
  101. {
  102. JOptionPane.showMessageDialog(null, e);
  103. }
  104.  
  105.  
  106.  
  107. }
  108. });
  109. btnLogin.setBounds(12, 203, 97, 25);
  110. frame.getContentPane().add(btnLogin);
  111.  
  112. JLabel title = new JLabel("Login");
  113. title.setFont(new Font("Tahoma", Font.PLAIN, 25));
  114. title.setBounds(177, 0, 143, 51);
  115. frame.getContentPane().add(title);
  116. }
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement