Advertisement
Guest User

Untitled

a guest
Jul 4th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.63 KB | None | 0 0
  1. package Telas;
  2.  
  3. import Classes.SQLConnection;
  4. import com.sun.glass.events.KeyEvent;
  5. import java.awt.Dimension;
  6. import java.awt.Toolkit;
  7. import java.sql.*;
  8. import java.text.DateFormat;
  9. import java.text.SimpleDateFormat;
  10. import java.util.Calendar;
  11. import java.util.GregorianCalendar;
  12. import javax.swing.JOptionPane;
  13. import javax.swing.SwingUtilities;
  14.  
  15. /**
  16. *
  17. * @author rafae
  18. */
  19. public class Login extends javax.swing.JFrame {
  20.  
  21. Connection conn = null;
  22. ResultSet rs = null;
  23. PreparedStatement pst = null;
  24.  
  25. /**
  26. * Creates new form Login
  27. */
  28. public Login() {
  29. initComponents();
  30. setIcon();
  31. SwingUtilities.invokeLater(new Runnable() { public void run() { LoginUsuario.requestFocus(); } });
  32. conn = SQLConnection.java_db();
  33. Toolkit toolkit = getToolkit();
  34. Dimension size = toolkit.getScreenSize();
  35. setLocation(size.width / 2 - getWidth() / 2,
  36. size.height / 2 - getHeight() / 2);
  37. currentDate();
  38. }
  39.  
  40. public void currentDate() {
  41. Calendar cal = new GregorianCalendar();
  42. int month = cal.get(Calendar.MONTH);
  43. int year = cal.get(Calendar.YEAR);
  44. int day = cal.get(Calendar.DAY_OF_MONTH);
  45.  
  46. lbl_data.setText(day + "/" + (month + 1) + "/" + year);
  47.  
  48. //HORA
  49. int second = cal.get(Calendar.SECOND);
  50. int minute = cal.get(Calendar.MINUTE);
  51. int hour = cal.get(Calendar.HOUR);
  52.  
  53. lbl_hora.setText(hour + ":" + (minute) + ":" + second);
  54.  
  55. }
  56.  
  57. /**
  58. * This method is called from within the constructor to initialize the form.
  59. * WARNING: Do NOT modify this code. The content of this method is always
  60. * regenerated by the Form Editor.
  61. */
  62. @SuppressWarnings("unchecked")
  63. // <editor-fold defaultstate="collapsed" desc="Generated Code">
  64. private void initComponents() {
  65.  
  66. jLabel3 = new javax.swing.JLabel();
  67. jPanel1 = new javax.swing.JPanel();
  68. LoginUsuario = new javax.swing.JTextField();
  69. LoginSenha = new javax.swing.JPasswordField();
  70. txt_divisao = new javax.swing.JComboBox<>();
  71. jLabel1 = new javax.swing.JLabel();
  72. jLabel2 = new javax.swing.JLabel();
  73. jLabel4 = new javax.swing.JLabel();
  74. LoginSalvar = new javax.swing.JButton();
  75. LoginEntrar = new javax.swing.JButton();
  76. jMenuBar1 = new javax.swing.JMenuBar();
  77. lbl_data = new javax.swing.JMenu();
  78. lbl_hora = new javax.swing.JMenu();
  79.  
  80. setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  81. setResizable(false);
  82.  
  83. jLabel3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagens/chave.png"))); // NOI18N
  84.  
  85. jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Login", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 18))); // NOI18N
  86. jPanel1.setFocusable(false);
  87.  
  88. LoginUsuario.addActionListener(new java.awt.event.ActionListener() {
  89. public void actionPerformed(java.awt.event.ActionEvent evt) {
  90. LoginUsuarioActionPerformed(evt);
  91. }
  92. });
  93.  
  94. LoginSenha.addActionListener(new java.awt.event.ActionListener() {
  95. public void actionPerformed(java.awt.event.ActionEvent evt) {
  96. LoginSenhaActionPerformed(evt);
  97. }
  98. });
  99. LoginSenha.addKeyListener(new java.awt.event.KeyAdapter() {
  100. public void keyPressed(java.awt.event.KeyEvent evt) {
  101. LoginSenhaKeyPressed(evt);
  102. }
  103. });
  104.  
  105. txt_divisao.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "Funcionario", "Administrador" }));
  106. txt_divisao.setToolTipText("");
  107.  
  108. jLabel1.setText("Usuário :");
  109. jLabel1.setFocusable(false);
  110.  
  111. jLabel2.setText("Senha : ");
  112. jLabel2.setFocusable(false);
  113.  
  114. jLabel4.setText("Divisão :");
  115. jLabel4.setFocusable(false);
  116.  
  117. javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
  118. jPanel1.setLayout(jPanel1Layout);
  119. jPanel1Layout.setHorizontalGroup(
  120. jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  121. .addGroup(jPanel1Layout.createSequentialGroup()
  122. .addContainerGap()
  123. .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  124. .addGroup(jPanel1Layout.createSequentialGroup()
  125. .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  126. .addComponent(jLabel1)
  127. .addComponent(jLabel2))
  128. .addGap(18, 18, 18)
  129. .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
  130. .addComponent(LoginUsuario, javax.swing.GroupLayout.DEFAULT_SIZE, 131, Short.MAX_VALUE)
  131. .addComponent(LoginSenha)))
  132. .addGroup(jPanel1Layout.createSequentialGroup()
  133. .addComponent(jLabel4)
  134. .addGap(18, 18, 18)
  135. .addComponent(txt_divisao, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
  136. .addContainerGap(26, Short.MAX_VALUE))
  137. );
  138. jPanel1Layout.setVerticalGroup(
  139. jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  140. .addGroup(jPanel1Layout.createSequentialGroup()
  141. .addGap(21, 21, 21)
  142. .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  143. .addComponent(jLabel1)
  144. .addComponent(LoginUsuario, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
  145. .addGap(18, 18, 18)
  146. .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  147. .addComponent(jLabel2)
  148. .addComponent(LoginSenha, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
  149. .addGap(18, 18, 18)
  150. .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  151. .addComponent(jLabel4)
  152. .addComponent(txt_divisao, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
  153. .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
  154. );
  155.  
  156. LoginSalvar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagens/power_exit_23.6px_1122609_easyicon.net.png"))); // NOI18N
  157. LoginSalvar.setText("Sair");
  158. LoginSalvar.setFocusable(false);
  159. LoginSalvar.setMaximumSize(new java.awt.Dimension(95, 33));
  160. LoginSalvar.addActionListener(new java.awt.event.ActionListener() {
  161. public void actionPerformed(java.awt.event.ActionEvent evt) {
  162. LoginSalvarActionPerformed(evt);
  163. }
  164. });
  165.  
  166. LoginEntrar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagens/enter_24px_1170222_easyicon.net.png"))); // NOI18N
  167. LoginEntrar.setText("Entrar");
  168. LoginEntrar.setFocusable(false);
  169. LoginEntrar.addActionListener(new java.awt.event.ActionListener() {
  170. public void actionPerformed(java.awt.event.ActionEvent evt) {
  171. LoginEntrarActionPerformed(evt);
  172. }
  173. });
  174.  
  175. jMenuBar1.setFocusable(false);
  176.  
  177. lbl_data.setText("Data");
  178. lbl_data.setFocusable(false);
  179. jMenuBar1.add(lbl_data);
  180.  
  181. lbl_hora.setText("Hora");
  182. lbl_hora.setFocusable(false);
  183. jMenuBar1.add(lbl_hora);
  184.  
  185. setJMenuBar(jMenuBar1);
  186.  
  187. javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  188. getContentPane().setLayout(layout);
  189. layout.setHorizontalGroup(
  190. layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  191. .addGroup(layout.createSequentialGroup()
  192. .addGap(12, 12, 12)
  193. .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 226, javax.swing.GroupLayout.PREFERRED_SIZE)
  194. .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  195. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
  196. .addGroup(layout.createSequentialGroup()
  197. .addComponent(LoginEntrar)
  198. .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  199. .addComponent(LoginSalvar, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE))
  200. .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
  201. .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
  202. );
  203. layout.setVerticalGroup(
  204. layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  205. .addGroup(layout.createSequentialGroup()
  206. .addGap(13, 13, 13)
  207. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  208. .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 220, javax.swing.GroupLayout.PREFERRED_SIZE)
  209. .addGroup(layout.createSequentialGroup()
  210. .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  211. .addGap(18, 18, 18)
  212. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  213. .addComponent(LoginSalvar, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
  214. .addComponent(LoginEntrar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
  215. .addContainerGap(26, Short.MAX_VALUE))
  216. );
  217.  
  218. setSize(new java.awt.Dimension(524, 332));
  219. setLocationRelativeTo(null);
  220. }// </editor-fold>
  221.  
  222. private void LoginEntrarActionPerformed(java.awt.event.ActionEvent evt) {
  223. if (LoginUsuario.getText().equals("")) {
  224. JOptionPane.showMessageDialog(null, "O Campo Usuário está vazio");
  225. } else if (LoginSenha.getText().equals("")) {
  226. JOptionPane.showMessageDialog(null, "O Campo Senha está vazio");
  227. } else {
  228.  
  229. String sql = "select id,username,password,division from Users Where (username =? and password =? and division =?)";
  230.  
  231. try {
  232. int count = 0;
  233.  
  234. pst = conn.prepareStatement(sql);
  235.  
  236. pst.setString(1, LoginUsuario.getText());
  237. pst.setString(2, LoginSenha.getText());
  238. pst.setString(3, txt_divisao.getSelectedItem().toString());
  239.  
  240. rs = pst.executeQuery();
  241.  
  242. {
  243. }
  244. while (rs.next()) {
  245. int id = rs.getInt(1);
  246. Emp.empId = id;
  247. count = count + 1;
  248. }
  249. String access = (txt_divisao.getSelectedItem().toString());
  250.  
  251. if (access.equals("Administrador")) {
  252.  
  253.  
  254.  
  255. if (count == 1) {
  256. JOptionPane.showMessageDialog(null, "Bem Vindo!");
  257. Menu j = new Menu();
  258. j.setVisible(true);
  259. this.dispose();
  260.  
  261.  
  262. java.util.Date currentDate = GregorianCalendar.getInstance().getTime();
  263. DateFormat df = DateFormat.getDateInstance();
  264. String dateString = df.format(currentDate);
  265.  
  266. java.util.Date d = new java.util.Date();
  267. SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
  268. String timeString = sdf.format(d);
  269.  
  270. String value0 = timeString;
  271. String values = dateString;
  272.  
  273. int value = Emp.empId;
  274. String reg = "insert into Informacao (EMP_ID,DATA,STATUS) values ('" + value + "','" + value0 + " / " + values + "','Logged in')";
  275. pst = conn.prepareStatement(reg);
  276. pst.execute();
  277. this.dispose();
  278.  
  279. } else if (count > 1) {
  280. JOptionPane.showMessageDialog(null, "Duplicado, nome de usuário ou acesso à senha negado");
  281. } else {
  282. JOptionPane.showMessageDialog(null, "O nome de usuário e a senha não estão corretos");
  283. }
  284. } else if (access.equals("Funcionario")) {
  285.  
  286. if (count == 1) {
  287. JOptionPane.showMessageDialog(null, "Bem Vindo!");
  288. Menu j = new Menu();
  289. j.setVisible(true);
  290.  
  291. java.util.Date currentDate = GregorianCalendar.getInstance().getTime();
  292. DateFormat df = DateFormat.getDateInstance();
  293. String dateString = df.format(currentDate);
  294.  
  295. java.util.Date d = new java.util.Date();
  296. SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
  297. String timeString = sdf.format(d);
  298.  
  299. String value0 = timeString;
  300. String values = dateString;
  301.  
  302. int value = Emp.empId;
  303. String reg = "insert into Informacao (EMP_ID,DATA,STATUS) values ('" + value + "','" + value0 + " / " + values + "','Logged in')";
  304. pst = conn.prepareStatement(reg);
  305. pst.execute();
  306. this.dispose();
  307. } else {
  308. JOptionPane.showMessageDialog(null, "O nome de usuário e a senha não estão corretos");
  309. }
  310. }
  311. } catch (Exception e) {
  312. JOptionPane.showMessageDialog(null, e);
  313.  
  314. } finally {
  315.  
  316. try {
  317. rs.close();
  318. pst.close();
  319.  
  320. } catch (Exception e) {
  321.  
  322. }
  323. }
  324.  
  325. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement