Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.Statement;
- /**
- * Created by jacek on 23.11.14.
- */
- public class Login extends JFrame implements ActionListener
- {
- public JTextField textField;
- public JTextField textField1;
- public JButton button;
- public Login()
- {
- InitUI();
- }
- private void InitUI()
- {
- setSize(800,500);
- setTitle("Okno Logowania");
- setLocationRelativeTo(null);
- setLayout(null);
- setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
- getContentPane().setBackground(Color.orange);
- JLabel label = new JLabel(" Logowanie ");
- label.setFont(new Font("Arial Bold", Font.PLAIN, 20));
- label.setBounds(325,30,150,30);
- add(label);
- JLabel label1 = new JLabel(" Login ");
- label1.setFont(new Font("Arial Bold", Font.PLAIN, 16));
- label1.setBounds(150,150,150,30);
- add(label1);
- JLabel label2 = new JLabel(" Hasło ");
- label2.setFont(new Font("Arial Bold", Font.PLAIN, 16));
- label2.setBounds(150,250,150,30);
- add(label2);
- textField = new JTextField();
- textField.setBounds(325,150,150,30);
- add(textField);
- textField1 = new JTextField();
- textField1.setBounds(325,250,150,30);
- add(textField1);
- button = new JButton("Zaloguj");
- button.setBounds(350,400,100,30);
- button.addActionListener(this);
- add(button);
- }
- public static void main(String[] args)
- {
- Login login = new Login();
- login.setVisible(true);
- }
- @Override
- public void actionPerformed(ActionEvent e)
- {
- String getLogin = textField.getText();
- String getPass = textField1.getText();
- String DBlogin = null;
- String DBpass = null;
- String DBusername = null;
- String DBusersurname = null;
- int DBUserId = 0;
- int DBrole = 0;
- Connection c;
- Statement stmt;
- Statement stmt1;
- try
- {
- Class.forName("org.postgresql.Driver");
- c = DriverManager.getConnection("jdbc:postgresql://localhost:5432/dziennik", "jacek", "zuza");
- c.setAutoCommit(false);
- //Trzeba przerobić na PreparedStatement aby uniknąć SQLInjection
- stmt = c.createStatement();
- stmt1 = c.createStatement();
- ResultSet rs = stmt.executeQuery( "SELECT user_name,user_surname,user_login,user_pass, user_id FROM users WHERE user_login='" +getLogin + "' AND user_pass='" +getPass +"';" );
- while ( rs.next() )
- {
- DBusername = rs.getString("user_name");
- DBusersurname = rs.getString("user_surname");
- DBlogin = rs.getString("user_login");
- DBpass = rs.getString("user_pass");
- DBUserId = rs.getInt("user_id");
- }
- ResultSet rs1 = stmt1.executeQuery("SELECT role_id FROM user_role WHERE user_id='" +DBUserId +"';");
- while ( rs1.next() )
- {
- DBrole = rs1.getInt("role_id");
- }
- rs.close();
- rs1.close();
- stmt.close();
- c.close();
- }
- catch ( Exception error )
- {
- System.err.println( error.getClass().getName()+": "+ error.getMessage() );
- System.exit(0);
- }
- if((DBlogin!=null)&(DBpass!=null))
- {
- if (DBrole == 1)
- {
- UserInterface userInterface = new UserInterface();
- //userInterface.b.setEnabled(false);
- userInterface.setVisible(true);
- JOptionPane.showMessageDialog(null, "Zalogowany jako Admin.\n Witaj: " + DBusername + " " + DBusersurname + " ");
- this.dispose();
- }
- if (DBrole == 2)
- {
- UserInterface userInterface = new UserInterface();
- userInterface.b.setEnabled(false);
- userInterface.b3.setEnabled(false);
- userInterface.setVisible(true);
- this.dispose();
- JOptionPane.showMessageDialog(null, "Zalogowany jako Nauczyciel.\n Witaj: " + DBusername + " " + DBusersurname + " ");
- }
- if (DBrole == 3)
- {
- UserInterface userInterface = new UserInterface();
- userInterface.b.setEnabled(false);
- userInterface.b1.setEnabled(false);
- userInterface.b3.setEnabled(false);
- userInterface.setVisible(true);
- RateGUI rateGUI = new RateGUI();
- rateGUI.b.setEnabled(false);
- rateGUI.b1.setEnabled(false);
- this.dispose();
- JOptionPane.showMessageDialog(null, "Zalogowany jako Uczeń.\n Witaj: " + DBusername + " " + DBusersurname + " ");
- }
- }
- else if((DBlogin==null)|(DBpass == null))
- {
- JOptionPane.showMessageDialog(null,"Musisz podać login i hasło \n\t Sporóbuj ponownie");
- textField.setText("");
- textField1.setText("");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement