Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.EventQueue;
- import javax.swing.JFrame;
- import javax.swing.JPanel;
- import javax.swing.JPasswordField;
- import javax.swing.border.EmptyBorder;
- import javax.swing.JLabel;
- import javax.swing.JTextField;
- import javax.swing.JButton;
- import java.awt.event.ActionListener;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.Statement;
- import java.awt.event.ActionEvent;
- import javax.swing.JComboBox;
- import java.awt.Color;
- import java.awt.Font;
- @SuppressWarnings("serial")
- public class Login extends JFrame {
- public static Login frame = new Login();
- private JPanel contentPane;
- private JTextField username;
- private JTextField password;
- private JLabel lblError;
- private String selectedUser;
- private String path = "jdbc:mysql://localhost:8889/DocumentsWork";
- private Connection connection;
- private Statement statement;
- public static void main(String[] args)
- {
- EventQueue.invokeLater(new Runnable()
- {
- public void run()
- {
- try
- {
- frame.setVisible(true);
- }
- catch (Exception e)
- {
- e.printStackTrace();
- }
- }
- });
- }
- public Login()
- {
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- setBounds(100, 100, 500, 300);
- contentPane = new JPanel();
- contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
- setContentPane(contentPane);
- contentPane.setLayout(null);
- username = new JTextField();
- username.setToolTipText("");
- username.setBounds(148, 61, 204, 26);
- contentPane.add(username);
- username.setColumns(10);
- password = new JPasswordField();
- password.setBounds(148, 116, 204, 26);
- contentPane.add(password);
- password.setColumns(10);
- JLabel lblLogin = new JLabel("Логин");
- lblLogin.setBounds(230, 40, 39, 16);
- contentPane.add(lblLogin);
- JLabel lblPassword = new JLabel("Пароль");
- lblPassword.setBounds(226, 95, 48, 16);
- contentPane.add(lblPassword);
- lblError = new JLabel();
- lblError.setFont(new Font("Lucida Grande", Font.BOLD, 13));
- lblError.setForeground(Color.RED);
- lblError.setBounds(6, 247, 488, 16);
- lblError.setVisible(false);
- lblError.setHorizontalAlignment(0);
- contentPane.add(lblError);
- JButton btnLogin = new JButton("Войти");
- btnLogin.addActionListener(new ActionListener()
- {
- public void actionPerformed(ActionEvent arg0)
- {
- SignIn();
- }
- });
- btnLogin.setBounds(191, 172, 117, 29);
- contentPane.add(btnLogin);
- JButton btnRegister = new JButton("Регистрация");
- btnRegister.addActionListener(new ActionListener()
- {
- public void actionPerformed(ActionEvent arg0)
- {
- Register frame = new Register();
- frame.setVisible(true);
- dispose();
- }
- });
- btnRegister.setBounds(191, 202, 117, 29);
- contentPane.add(btnRegister);
- }
- public void SignIn()
- {
- try
- {
- Class.forName("com.mysql.cj.jdbc.Driver");
- connection = DriverManager.getConnection(path, "admin", "");
- statement = connection.createStatement();
- String sql = "Select Type from Users where Username='" + username.getText().toString() + "'";
- ResultSet result = statement.executeQuery(sql);
- result.next();
- selectedUser = result.getString(1);
- System.out.println("selectedUser =" + selectedUser);
- sql = "Select * from Users where Username='" + username.getText().toString()
- + "' and Password='" + password.getText().toString() + "'";
- result = statement.executeQuery(sql);
- if (!result.next())
- {
- lblError.setText("Неверный логин или пароль!");
- lblError.setVisible(true);
- }
- else
- {
- lblError.setVisible(false);
- OpenWindow();
- }
- connection.close();
- }
- catch(Exception e)
- {
- System.out.print(e);
- }
- }
- public void OpenWindow()
- {
- if(selectedUser == "Operator")
- {
- Operator op = new Operator();
- op.lblOperator.setText(username.getText().toString());
- op.setVisible(true);
- System.out.println("Operator created");
- }
- else if (selectedUser == "Controller")
- {
- Controller con = new Controller();
- con.lblController.setText(username.getText().toString());
- con.setVisible(true);
- System.out.println("Controller created");
- }
- else
- {
- User user = new User();
- user.lblUser.setText(username.getText().toString());
- user.setVisible(true);
- System.out.println("User created");
- }
- dispose();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement