Advertisement
Guest User

Untitled

a guest
Nov 23rd, 2014
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.41 KB | None | 0 0
  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.ActionEvent;
  4. import java.awt.event.ActionListener;
  5. import java.sql.Connection;
  6. import java.sql.DriverManager;
  7. import java.sql.ResultSet;
  8. import java.sql.Statement;
  9.  
  10. /**
  11.  * Created by jacek on 23.11.14.
  12.  */
  13.  
  14. public class Login extends JFrame implements ActionListener
  15. {
  16.     public JTextField textField;
  17.     public JTextField textField1;
  18.     public JButton button;
  19.  
  20.  
  21.     public Login()
  22.     {
  23.         InitUI();
  24.     }
  25.  
  26.     private void InitUI()
  27.     {
  28.         setSize(800,500);
  29.         setTitle("Okno Logowania");
  30.         setLocationRelativeTo(null);
  31.         setLayout(null);
  32.         setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  33.         getContentPane().setBackground(Color.orange);
  34.  
  35.         JLabel label = new JLabel(" Logowanie ");
  36.         label.setFont(new Font("Arial Bold", Font.PLAIN, 20));
  37.         label.setBounds(325,30,150,30);
  38.         add(label);
  39.  
  40.         JLabel label1 = new JLabel(" Login ");
  41.         label1.setFont(new Font("Arial Bold", Font.PLAIN, 16));
  42.         label1.setBounds(150,150,150,30);
  43.         add(label1);
  44.  
  45.         JLabel label2 = new JLabel(" Hasło ");
  46.         label2.setFont(new Font("Arial Bold", Font.PLAIN, 16));
  47.         label2.setBounds(150,250,150,30);
  48.         add(label2);
  49.  
  50.         textField = new JTextField();
  51.         textField.setBounds(325,150,150,30);
  52.         add(textField);
  53.  
  54.         textField1 = new JTextField();
  55.         textField1.setBounds(325,250,150,30);
  56.         add(textField1);
  57.  
  58.         button = new JButton("Zaloguj");
  59.         button.setBounds(350,400,100,30);
  60.         button.addActionListener(this);
  61.         add(button);
  62.     }
  63.  
  64.     public static void main(String[] args)
  65.     {
  66.         Login login = new Login();
  67.         login.setVisible(true);
  68.     }
  69.  
  70.     @Override
  71.     public void actionPerformed(ActionEvent e)
  72.     {
  73.         String getLogin = textField.getText();
  74.         String getPass = textField1.getText();
  75.  
  76.         String DBlogin = null;
  77.         String DBpass = null;
  78.         String DBusername = null;
  79.         String DBusersurname = null;
  80.         int DBUserId = 0;
  81.         int DBrole = 0;
  82.  
  83.         Connection c;
  84.         Statement stmt;
  85.         Statement stmt1;
  86.         try
  87.         {
  88.             Class.forName("org.postgresql.Driver");
  89.             c = DriverManager.getConnection("jdbc:postgresql://localhost:5432/dziennik", "jacek", "zuza");
  90.             c.setAutoCommit(false);
  91.  
  92.             //Trzeba przerobić na PreparedStatement aby uniknąć SQLInjection
  93.  
  94.             stmt = c.createStatement();
  95.             stmt1 = c.createStatement();
  96.             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 +"';" );
  97.  
  98.             while ( rs.next() )
  99.             {
  100.                 DBusername = rs.getString("user_name");
  101.                 DBusersurname = rs.getString("user_surname");
  102.                 DBlogin = rs.getString("user_login");
  103.                 DBpass = rs.getString("user_pass");
  104.                 DBUserId = rs.getInt("user_id");
  105.             }
  106.  
  107.             ResultSet rs1 = stmt1.executeQuery("SELECT role_id FROM user_role WHERE user_id='" +DBUserId +"';");
  108.  
  109.             while ( rs1.next() )
  110.             {
  111.                 DBrole = rs1.getInt("role_id");
  112.             }
  113.             rs.close();
  114.             rs1.close();
  115.             stmt.close();
  116.             c.close();
  117.         }
  118.  
  119.         catch ( Exception error )
  120.         {
  121.             System.err.println( error.getClass().getName()+": "+ error.getMessage() );
  122.             System.exit(0);
  123.         }
  124.  
  125.         if((DBlogin!=null)&(DBpass!=null))
  126.         {
  127.             if (DBrole == 1)
  128.             {
  129.                 UserInterface userInterface = new UserInterface();
  130.                 //userInterface.b.setEnabled(false);
  131.                 userInterface.setVisible(true);
  132.                 JOptionPane.showMessageDialog(null, "Zalogowany jako Admin.\n Witaj: " + DBusername + " " + DBusersurname + " ");
  133.                 this.dispose();
  134.             }
  135.             if (DBrole == 2)
  136.             {
  137.                 UserInterface userInterface = new UserInterface();
  138.                 userInterface.b.setEnabled(false);
  139.                 userInterface.b3.setEnabled(false);
  140.                 userInterface.setVisible(true);
  141.                 this.dispose();
  142.                 JOptionPane.showMessageDialog(null, "Zalogowany jako Nauczyciel.\n Witaj: " + DBusername + " " + DBusersurname + " ");
  143.             }
  144.  
  145.             if (DBrole == 3)
  146.             {
  147.                 UserInterface userInterface = new UserInterface();
  148.                 userInterface.b.setEnabled(false);
  149.                 userInterface.b1.setEnabled(false);
  150.                 userInterface.b3.setEnabled(false);
  151.                 userInterface.setVisible(true);
  152.                 RateGUI rateGUI = new RateGUI();
  153.                 rateGUI.b.setEnabled(false);
  154.                 rateGUI.b1.setEnabled(false);
  155.                 this.dispose();
  156.                 JOptionPane.showMessageDialog(null, "Zalogowany jako Uczeń.\n Witaj: " + DBusername + " " + DBusersurname + " ");
  157.             }
  158.         }
  159.  
  160.         else if((DBlogin==null)|(DBpass == null))
  161.         {
  162.             JOptionPane.showMessageDialog(null,"Musisz podać login i hasło \n\t  Sporóbuj ponownie");
  163.             textField.setText("");
  164.             textField1.setText("");
  165.         }
  166.     }
  167. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement