Advertisement
Guest User

Untitled

a guest
Dec 25th, 2016
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.61 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. import javax.swing.JPasswordField;
  16.  
  17. public class Login {
  18.  
  19. // Definisi JDBC driver name i URL baze
  20. static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
  21. static final String DB_URL = "jdbc:mysql://127.0.0.1:3306/SurveyDB?verifyServerCertificate=false&useSSL=false";
  22.  
  23. // Db podaci
  24. static final String USER = "root";
  25. static final String PASS = "123456";
  26.  
  27. private JFrame frmPoolSystem;
  28. private JTextField usernameField;
  29. private JTextField passwordBox;
  30.  
  31. /**
  32. * Launch the application.
  33. */
  34. public static void main(String[] args) {
  35. EventQueue.invokeLater(new Runnable() {
  36. public void run() {
  37. try {
  38. Login window = new Login();
  39. window.frmPoolSystem.setVisible(true);
  40. } catch (Exception e) {
  41. e.printStackTrace();
  42. }
  43. }
  44. });
  45. }
  46.  
  47. /**
  48. * Create the application.
  49. */
  50. public Login() {
  51. initialize();
  52. }
  53.  
  54. /**
  55. * Initialize the contents of the frame.
  56. */
  57. private void initialize() {
  58. frmPoolSystem = new JFrame();
  59. frmPoolSystem.setTitle("Pool System - Login");
  60. frmPoolSystem.setResizable(false);
  61. frmPoolSystem.setBounds(100, 100, 335, 276);
  62. frmPoolSystem.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
  63. frmPoolSystem.getContentPane().setLayout(null);
  64.  
  65. JLabel unameText = new JLabel("Username");
  66. unameText.setBounds(10, 132, 77, 14);
  67. frmPoolSystem.getContentPane().add(unameText);
  68.  
  69. JLabel pwText = new JLabel("Password");
  70. pwText.setBounds(10, 157, 77, 14);
  71. frmPoolSystem.getContentPane().add(pwText);
  72.  
  73. usernameField = new JTextField();
  74. usernameField.setBounds(96, 129, 223, 20);
  75. frmPoolSystem.getContentPane().add(usernameField);
  76. usernameField.setColumns(10);
  77.  
  78. JButton loginButton = new JButton("Login");
  79.  
  80. loginButton.addMouseListener(new MouseAdapter() {
  81. public void mouseClicked(MouseEvent e) {
  82. Connection conn = null;
  83. Statement stmt = null;
  84. try {
  85. // Registruj JDBC driver
  86. Class.forName("com.mysql.jdbc.Driver");
  87.  
  88. // Zapocni konekciju conn
  89. conn = DriverManager.getConnection(DB_URL, USER, PASS);
  90.  
  91. // Napravi statement i izvrsi query
  92. stmt = conn.createStatement();
  93. String sql;
  94. sql = ("SELECT id, username, password FROM Users WHERE username='"+usernameField.getText()+"' AND password='"+passwordBox.getText()+"'");
  95. ResultSet rs = stmt.executeQuery(sql);
  96.  
  97. // Provjeri da li su uname i pw tacni tj. da li query daje rezultat
  98. if (rs.next() == false) {
  99. JOptionPane.showMessageDialog(null, "Pogresni Podaci");
  100. }
  101.  
  102. // Povuci podatke
  103. // ZA PRIMJER KORISTIMO ID KAO USER_ROLE (1-admin, 2-user)
  104. int id = rs.getInt("id");
  105. String username = rs.getString("username");
  106. String password = rs.getString("password");
  107.  
  108. //Provjera
  109. if (id == 1) {
  110. // OTVORI ADMINA
  111. LogovanAdmin lAdmin = new LogovanAdmin();
  112. lAdmin.Admin();
  113. frmPoolSystem.dispose();
  114. }else{
  115. JOptionPane.showMessageDialog(null, "MoSa Admin");
  116. }
  117.  
  118. // Zatvori resultset, statement i db konekciju i ispisi greske ako postoje
  119. rs.close();
  120. stmt.close();
  121. conn.close();
  122. } catch (SQLException se) {
  123. // Errors JDBC
  124. se.printStackTrace();
  125. } catch (Exception x) {
  126. // Errors za Class.forName
  127. x.printStackTrace();
  128. } finally {
  129. try {
  130. if (stmt != null)
  131. stmt.close();
  132. } catch (SQLException se2) {
  133. }
  134. try {
  135. if (conn != null)
  136. conn.close();
  137. } catch (SQLException se) {
  138. se.printStackTrace();
  139. }// zavrsi try try
  140. }// zavrsi glavni try try
  141. }
  142. });
  143. loginButton.setBounds(10, 207, 150, 29);
  144. frmPoolSystem.getContentPane().add(loginButton);
  145.  
  146. passwordBox = new JTextField();
  147. passwordBox.setColumns(10);
  148. passwordBox.setBounds(96, 154, 223, 20);
  149. frmPoolSystem.getContentPane().add(passwordBox);
  150.  
  151. JButton exitButton = new JButton("Exit");
  152. exitButton.addMouseListener(new MouseAdapter() {
  153. @Override
  154. public void mouseClicked(MouseEvent e) {
  155. System.exit(0);
  156. }
  157. });
  158. exitButton.setBounds(171, 207, 150, 29);
  159. frmPoolSystem.getContentPane().add(exitButton);
  160.  
  161. JLabel logo = new JLabel("New label");
  162. logo.setIcon(new ImageIcon(Login.class.getResource("/images/nesto.png")));
  163. logo.setBounds(0, 0, 331, 118);
  164. frmPoolSystem.getContentPane().add(logo);
  165. }
  166. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement