Advertisement
Guest User

Untitled

a guest
Mar 25th, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.42 KB | None | 0 0
  1. import java.awt.EventQueue;
  2.  
  3. import javax.swing.JFrame;
  4. import javax.swing.JPanel;
  5. import javax.swing.JPasswordField;
  6. import javax.swing.border.EmptyBorder;
  7. import javax.swing.JLabel;
  8. import javax.swing.JTextField;
  9. import javax.swing.JButton;
  10. import java.awt.event.ActionListener;
  11. import java.sql.Connection;
  12. import java.sql.DriverManager;
  13. import java.sql.ResultSet;
  14. import java.sql.Statement;
  15. import java.awt.event.ActionEvent;
  16. import javax.swing.JComboBox;
  17. import java.awt.Color;
  18. import java.awt.Font;
  19.  
  20. @SuppressWarnings("serial")
  21. public class Login extends JFrame {
  22.  
  23. public static Login frame = new Login();
  24. private JPanel contentPane;
  25. private JTextField username;
  26. private JTextField password;
  27. private JLabel lblError;
  28. private String selectedUser;
  29. private String path = "jdbc:mysql://localhost:8889/DocumentsWork";
  30. private Connection connection;
  31. private Statement statement;
  32.  
  33. public static void main(String[] args)
  34. {
  35. EventQueue.invokeLater(new Runnable()
  36. {
  37. public void run()
  38. {
  39. try
  40. {
  41. frame.setVisible(true);
  42. }
  43. catch (Exception e)
  44. {
  45. e.printStackTrace();
  46. }
  47. }
  48. });
  49. }
  50.  
  51. public Login()
  52. {
  53. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  54. setBounds(100, 100, 500, 300);
  55. contentPane = new JPanel();
  56. contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
  57. setContentPane(contentPane);
  58. contentPane.setLayout(null);
  59.  
  60. username = new JTextField();
  61. username.setToolTipText("");
  62. username.setBounds(148, 61, 204, 26);
  63. contentPane.add(username);
  64. username.setColumns(10);
  65.  
  66. password = new JPasswordField();
  67. password.setBounds(148, 116, 204, 26);
  68. contentPane.add(password);
  69. password.setColumns(10);
  70.  
  71. JLabel lblLogin = new JLabel("Логин");
  72. lblLogin.setBounds(230, 40, 39, 16);
  73. contentPane.add(lblLogin);
  74.  
  75. JLabel lblPassword = new JLabel("Пароль");
  76. lblPassword.setBounds(226, 95, 48, 16);
  77. contentPane.add(lblPassword);
  78.  
  79. lblError = new JLabel();
  80. lblError.setFont(new Font("Lucida Grande", Font.BOLD, 13));
  81. lblError.setForeground(Color.RED);
  82. lblError.setBounds(6, 247, 488, 16);
  83. lblError.setVisible(false);
  84. lblError.setHorizontalAlignment(0);
  85. contentPane.add(lblError);
  86.  
  87. JButton btnLogin = new JButton("Войти");
  88. btnLogin.addActionListener(new ActionListener()
  89. {
  90. public void actionPerformed(ActionEvent arg0)
  91. {
  92. SignIn();
  93. }
  94. });
  95. btnLogin.setBounds(191, 172, 117, 29);
  96. contentPane.add(btnLogin);
  97.  
  98. JButton btnRegister = new JButton("Регистрация");
  99. btnRegister.addActionListener(new ActionListener()
  100. {
  101. public void actionPerformed(ActionEvent arg0)
  102. {
  103. Register frame = new Register();
  104. frame.setVisible(true);
  105. dispose();
  106. }
  107. });
  108. btnRegister.setBounds(191, 202, 117, 29);
  109. contentPane.add(btnRegister);
  110. }
  111.  
  112. public void SignIn()
  113. {
  114. try
  115. {
  116. Class.forName("com.mysql.cj.jdbc.Driver");
  117. connection = DriverManager.getConnection(path, "admin", "");
  118. statement = connection.createStatement();
  119. String sql = "Select Type from Users where Username='" + username.getText().toString() + "'";
  120. ResultSet result = statement.executeQuery(sql);
  121. result.next();
  122. selectedUser = result.getString(1);
  123. System.out.println("selectedUser =" + selectedUser);
  124. sql = "Select * from Users where Username='" + username.getText().toString()
  125. + "' and Password='" + password.getText().toString() + "'";
  126. result = statement.executeQuery(sql);
  127. if (!result.next())
  128. {
  129. lblError.setText("Неверный логин или пароль!");
  130. lblError.setVisible(true);
  131. }
  132. else
  133. {
  134. lblError.setVisible(false);
  135. OpenWindow();
  136. }
  137. connection.close();
  138. }
  139. catch(Exception e)
  140. {
  141. System.out.print(e);
  142. }
  143. }
  144.  
  145. public void OpenWindow()
  146. {
  147. if(selectedUser == "Operator")
  148. {
  149. Operator op = new Operator();
  150. op.lblOperator.setText(username.getText().toString());
  151. op.setVisible(true);
  152. System.out.println("Operator created");
  153. }
  154. else if (selectedUser == "Controller")
  155. {
  156. Controller con = new Controller();
  157. con.lblController.setText(username.getText().toString());
  158. con.setVisible(true);
  159. System.out.println("Controller created");
  160. }
  161. else
  162. {
  163. User user = new User();
  164. user.lblUser.setText(username.getText().toString());
  165. user.setVisible(true);
  166. System.out.println("User created");
  167. }
  168. dispose();
  169. }
  170. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement