Advertisement
Guest User

Untitled

a guest
Dec 26th, 2016
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.35 KB | None | 0 0
  1. import java.awt.EventQueue;
  2.  
  3. import javax.swing.JFrame;
  4. import javax.swing.JButton;
  5.  
  6. import java.awt.event.MouseAdapter;
  7. import java.awt.event.MouseEvent;
  8.  
  9. import javax.swing.JLabel;
  10. import javax.swing.JOptionPane;
  11. import javax.swing.JTextField;
  12. import javax.swing.ImageIcon;
  13.  
  14. import java.sql.*;
  15.  
  16. import javax.swing.JPasswordField;
  17.  
  18. public class Login {
  19.  
  20. public static int id;
  21. private static String username = "";
  22. private static String first_name = "";
  23. private static String last_name = "";
  24. private static String gender_is = "";
  25. private static int gender;
  26. private static String age_is;
  27.  
  28. // Definisi JDBC driver name i URL baze
  29. static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
  30. static final String DB_URL = "jdbc:mysql://127.0.0.1:3306/SurveyDB?verifyServerCertificate=false&useSSL=false";
  31.  
  32. // Db podaci
  33. static final String USER = "root";
  34. static final String PASS = "123456";
  35.  
  36. private JFrame frmPoolSystem;
  37. private JTextField usernameField;
  38. private JPasswordField passwordField;
  39.  
  40. /**
  41. * Launch the application.
  42. */
  43. public static void main(String[] args) {
  44. EventQueue.invokeLater(new Runnable() {
  45. public void run() {
  46. try {
  47. Login window = new Login();
  48. window.frmPoolSystem.setVisible(true);
  49. } catch (Exception e) {
  50. e.printStackTrace();
  51. }
  52. }
  53. });
  54. }
  55.  
  56. /**
  57. * Create the application.
  58. */
  59. public Login() {
  60. initialize();
  61. }
  62.  
  63. /**
  64. * Initialize the contents of the frame.
  65. */
  66. private void initialize() {
  67. frmPoolSystem = new JFrame();
  68. frmPoolSystem.setTitle("Pool System - Login");
  69. frmPoolSystem.setResizable(false);
  70. frmPoolSystem.setBounds(100, 100, 479, 335);
  71. frmPoolSystem.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
  72. frmPoolSystem.getContentPane().setLayout(null);
  73.  
  74. JLabel unameText = new JLabel("Username:");
  75. unameText.setBounds(10, 182, 77, 14);
  76. frmPoolSystem.getContentPane().add(unameText);
  77.  
  78. JLabel pwText = new JLabel("Password:");
  79. pwText.setBounds(10, 218, 77, 14);
  80. frmPoolSystem.getContentPane().add(pwText);
  81.  
  82. usernameField = new JTextField();
  83. usernameField.setBounds(96, 179, 367, 20);
  84. frmPoolSystem.getContentPane().add(usernameField);
  85. usernameField.setColumns(10);
  86.  
  87. JButton loginButton = new JButton("Login");
  88.  
  89. loginButton.addMouseListener(new MouseAdapter() {
  90. @SuppressWarnings("deprecation")
  91. public void mouseClicked(MouseEvent e) {
  92. Connection conn = null;
  93. Statement stmt = null;
  94. try {
  95. // Registruj JDBC driver
  96. Class.forName("com.mysql.jdbc.Driver");
  97.  
  98. // Zapocni konekciju conn
  99. conn = DriverManager.getConnection(DB_URL, USER, PASS);
  100.  
  101. // Napravi statement i izvrsi query
  102. stmt = conn.createStatement();
  103. String sql;
  104. sql = ("SELECT id, username, password, first_name, last_name, age, gender, user_role FROM Users WHERE username='"+usernameField.getText()+"' AND password='"+passwordField.getText()+"'");
  105. ResultSet rs = stmt.executeQuery(sql);
  106.  
  107. // Provjeri da li su uname i pw tacni tj. da li query daje rezultat
  108. if (rs.next() == false) {
  109. JOptionPane.showMessageDialog(null, "Pogresni Podaci");
  110. }
  111.  
  112. // Povuci podatke
  113. setId(rs.getInt("id"));
  114. setUsername(rs.getString("username"));
  115. setFirstName(rs.getString("first_name"));
  116. setLastName(rs.getString("last_name"));
  117. int age = rs.getInt("age");
  118. setAgeIs(String.valueOf(age));
  119. setGender(rs.getInt("gender"));
  120. int user_role = rs.getInt("user_role");
  121.  
  122. if (gender == 1){
  123. setGenderIs("Musko");
  124. }else{
  125. setGenderIs("Zensko");
  126. }
  127.  
  128. //Provjera User_Role (admin 1, sve ostalo user)
  129. if (user_role == 1) {
  130. // OTVORI ADMIN
  131. LogovanAdmin lAdmin = new LogovanAdmin();
  132. lAdmin.Admin();
  133. frmPoolSystem.dispose();
  134. }else{
  135. // OTVORI USERA
  136. LogovanKorisnik IKorisnik = new LogovanKorisnik();
  137. IKorisnik.LogovanProfil();
  138. frmPoolSystem.dispose();
  139. }
  140.  
  141. // Zatvori resultset, statement i db konekciju i ispisi greske ako postoje
  142. rs.close();
  143. stmt.close();
  144. conn.close();
  145. } catch (SQLException se) {
  146. // Errors JDBC
  147. se.printStackTrace();
  148. } catch (Exception x) {
  149. // Errors za Class.forName
  150. x.printStackTrace();
  151. } finally {
  152. try {
  153. if (stmt != null)
  154. stmt.close();
  155. } catch (SQLException se2) {
  156. }
  157. try {
  158. if (conn != null)
  159. conn.close();
  160. } catch (SQLException se) {
  161. se.printStackTrace();
  162. }// zavrsi try try
  163. }// zavrsi glavni try try
  164. }
  165. });
  166. loginButton.setBounds(21, 260, 200, 29);
  167. frmPoolSystem.getContentPane().add(loginButton);
  168.  
  169. JButton registerButton = new JButton("Registruj Se");
  170. registerButton.addMouseListener(new MouseAdapter() {
  171. @Override
  172. public void mouseClicked(MouseEvent e) {
  173. RegistracijaKorisnika r = new RegistracijaKorisnika();
  174. r.RegistracijaIzLogina();
  175. }
  176. });
  177. registerButton.setBounds(248, 260, 200, 29);
  178. frmPoolSystem.getContentPane().add(registerButton);
  179.  
  180. JLabel logo = new JLabel("");
  181. logo.setIcon(new ImageIcon(Login.class.getResource("/images/logo.png")));
  182. logo.setBounds(21, 24, 423, 118);
  183. frmPoolSystem.getContentPane().add(logo);
  184.  
  185. passwordField = new JPasswordField();
  186. passwordField.setBounds(97, 215, 366, 20);
  187. frmPoolSystem.getContentPane().add(passwordField);
  188.  
  189.  
  190. }
  191.  
  192. //Getteri i setteri
  193. public String getUsername() {
  194. return username;
  195. }
  196.  
  197. public static void setUsername(String username) {
  198. Login.username = username;
  199. }
  200.  
  201. public int getId() {
  202. return id;
  203. }
  204.  
  205. public static void setId(int id) {
  206. Login.id = id;
  207. }
  208.  
  209. public String getFirstName() {
  210. return first_name;
  211. }
  212.  
  213. public static void setFirstName(String first_name) {
  214. Login.first_name = first_name;
  215. }
  216.  
  217. public String getLastName() {
  218. return last_name;
  219. }
  220.  
  221. public static void setLastName(String last_name) {
  222. Login.last_name = last_name;
  223. }
  224.  
  225. public int getGender() {
  226. return gender;
  227. }
  228.  
  229. public static void setGender(int gender) {
  230. Login.gender = gender;
  231. }
  232.  
  233. public String getGenderIs() {
  234. return gender_is;
  235. }
  236.  
  237. public static void setGenderIs(String gender_is) {
  238. Login.gender_is = gender_is;
  239. }
  240.  
  241. public String getAgeIs() {
  242. return age_is;
  243. }
  244.  
  245. public static void setAgeIs(String age_is) {
  246. Login.age_is = age_is;
  247. }
  248. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement