Advertisement
alexandre-c

classeJava

Nov 2nd, 2019
562
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 11.58 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package br.com.imobiliaria.view;
  7.  
  8. import br.com.imobiliaria.controller.Imobiliaria;
  9. import br.com.imobiliaria.controller.Imovel;
  10. import br.com.imobiliaria.model.ModuloConexao;
  11. import java.sql.Connection;
  12. import java.sql.PreparedStatement;
  13. import java.sql.ResultSet;
  14. import java.sql.SQLClientInfoException;
  15. import java.sql.SQLException;
  16. import java.sql.Statement;
  17. import java.util.Arrays;
  18. import java.util.logging.Level;
  19. import java.util.logging.Logger;
  20. import javax.swing.JOptionPane;
  21.  
  22. /**
  23.  *
  24.  * @author alexandre
  25.  */
  26. public class TelaLogin extends javax.swing.JFrame {
  27.  
  28.     Connection c = null;
  29.     //Prepared Statement e ResultSet são frameworks do pacote java.sql e servem para preparar e executar as instruções SQL
  30.     PreparedStatement pst = null;
  31.     ResultSet rs = null;
  32.     Imobiliaria i = new Imobiliaria();
  33.  
  34.     /**
  35.      * Creates new form TelaLogin
  36.      */
  37.     public TelaLogin() {
  38.         initComponents();
  39.         try {
  40.             c = ModuloConexao.conector();
  41.             if (c == null) {
  42.                 lblStatus.setText("OFFLINE");
  43.                 txtSenha.setEnabled(false);
  44.                 txtUsuario.setEnabled(false);
  45.                 btnLogar.setEnabled(false);
  46.                 btnCadastrar.setEnabled(false);
  47.                 //System.exit(0);
  48.             } else {
  49.                 lblStatus.setText("ONLINE");
  50.                 try {
  51.                     c.close();
  52.                 } catch (SQLException ex) {
  53.                     Logger.getLogger(TelaLogin.class.getName()).log(Level.SEVERE, null, ex);
  54.                 }
  55.             }
  56.         } catch (SQLClientInfoException ex) {
  57.             JOptionPane.showMessageDialog(null, "SISTEMA OFFLINE\nErro: " + ex.getMessage(), "STATUS", JOptionPane.ERROR_MESSAGE);
  58.             //Logger.getLogger(TelaLogin.class.getName()).log(Level.SEVERE, null, ex);
  59.         }
  60.     }
  61.  
  62.     /**
  63.      * This method is called from within the constructor to initialize the form.
  64.      * WARNING: Do NOT modify this code. The content of this method is always
  65.      * regenerated by the Form Editor.
  66.      */
  67.     @SuppressWarnings("unchecked")
  68.     // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
  69.     private void initComponents() {
  70.  
  71.         buttonGroupTipoLogin = new javax.swing.ButtonGroup();
  72.         jLabel1 = new javax.swing.JLabel();
  73.         jLabel2 = new javax.swing.JLabel();
  74.         txtUsuario = new javax.swing.JTextField();
  75.         txtSenha = new javax.swing.JPasswordField();
  76.         btnLogar = new javax.swing.JButton();
  77.         btnCadastrar = new javax.swing.JButton();
  78.         lblStatus = new javax.swing.JLabel();
  79.  
  80.         setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  81.         setTitle("TELA DE LOGIN");
  82.         setResizable(false);
  83.  
  84.         jLabel1.setText("Usuário .:");
  85.  
  86.         jLabel2.setText("Senha .:");
  87.  
  88.         btnLogar.setText("Logar");
  89.         btnLogar.addActionListener(new java.awt.event.ActionListener() {
  90.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  91.                 btnLogarActionPerformed(evt);
  92.             }
  93.         });
  94.  
  95.         btnCadastrar.setText("Cadastre-se");
  96.         btnCadastrar.addActionListener(new java.awt.event.ActionListener() {
  97.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  98.                 btnCadastrarActionPerformed(evt);
  99.             }
  100.         });
  101.  
  102.         lblStatus.setFont(new java.awt.Font("Ubuntu", 0, 12)); // NOI18N
  103.  
  104.         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  105.         getContentPane().setLayout(layout);
  106.         layout.setHorizontalGroup(
  107.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  108.             .addGroup(layout.createSequentialGroup()
  109.                 .addContainerGap()
  110.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
  111.                     .addComponent(jLabel2)
  112.                     .addComponent(jLabel1))
  113.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  114.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  115.                     .addGroup(layout.createSequentialGroup()
  116.                         .addComponent(btnLogar, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
  117.                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  118.                         .addComponent(btnCadastrar)
  119.                         .addGap(18, 18, 18)
  120.                         .addComponent(lblStatus, javax.swing.GroupLayout.DEFAULT_SIZE, 135, Short.MAX_VALUE))
  121.                     .addComponent(txtUsuario)
  122.                     .addComponent(txtSenha))
  123.                 .addGap(26, 26, 26))
  124.         );
  125.         layout.setVerticalGroup(
  126.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  127.             .addGroup(layout.createSequentialGroup()
  128.                 .addGap(25, 25, 25)
  129.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  130.                     .addComponent(jLabel1)
  131.                     .addComponent(txtUsuario, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
  132.                 .addGap(18, 18, 18)
  133.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  134.                     .addComponent(jLabel2)
  135.                     .addComponent(txtSenha, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
  136.                 .addGap(18, 18, 18)
  137.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  138.                     .addComponent(btnLogar)
  139.                     .addComponent(btnCadastrar)
  140.                     .addComponent(lblStatus, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE))
  141.                 .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
  142.         );
  143.  
  144.         pack();
  145.         setLocationRelativeTo(null);
  146.     }// </editor-fold>                        
  147.  
  148.     public void buscarImobiliaria() {
  149.         String sql = "SELECT cod_imob, cnpj, nm_imob, email, tel, login, senha, ult_acesso FROM imobiliaria where login = ?";
  150.         try {
  151.             c = ModuloConexao.conector();
  152.             pst = c.prepareStatement(sql);
  153.             pst.setString(1, txtUsuario.getText().trim());
  154.             rs = pst.executeQuery();
  155.  
  156.             if (rs.next()) {
  157.                 i.setCodigo(rs.getInt("cod_imob"));
  158.                 i.setCnpj(rs.getString("cnpj"));
  159.                 i.setNome(rs.getString("nm_imob"));
  160.                 i.setEmail(rs.getString("email"));
  161.                 i.setTelefone(rs.getString("tel"));
  162.                 i.setLogin(rs.getString("login"));
  163.                 i.setSenha(rs.getString("senha"));
  164.                 i.setUlt_acesso(rs.getTimestamp("ult_acesso"));
  165.             }
  166.  
  167.         } catch (SQLException ex) {
  168.             Logger.getLogger(TelaLogin.class.getName()).log(Level.SEVERE, null, ex);
  169.         }
  170.     }
  171.    
  172.     //Metodo com problema
  173.     public void atualizaUltAcesso() {
  174.         String sql = "UPDATE imobiliaria SET ult_acesso = SYSDATE-interval'3'hour WHERE cod_imob=?";
  175.         try {
  176.  
  177.             c = ModuloConexao.conector();
  178.             pst = c.prepareStatement(sql);            
  179.             pst.setInt(1, (i.getCodigo())); //i refere-se a variável do tipo imobiliaria            
  180.             pst.executeUpdate();
  181.             c.commit();            
  182.  
  183.         } catch (Exception e) {
  184.             e.printStackTrace();
  185.         }
  186.  
  187.     }
  188.  
  189.     //Fonte usada pra solução do problema de select:
  190.     //https://www.javac.com.br/jc/posts/list/3793-faultstringora03115tipo-de-dados-ou-representacao-de-rede-nao-aceitaofaultstringresolvido.page
  191.     private void btnLogarActionPerformed(java.awt.event.ActionEvent evt) {                                        
  192.         if (txtUsuario.getText().length() == 0) {
  193.             JOptionPane.showMessageDialog(null, "Campo Vazio, Digite usuário", "ATENÇÃO !", JOptionPane.INFORMATION_MESSAGE);
  194.         } else {
  195.             buscarImobiliaria();
  196.             //Verifica se usuário e senha correspondem
  197.             System.out.println(i.getLogin() + "---" + i.getSenha());
  198.             if (txtUsuario.getText().trim().equals(i.getLogin()) && txtSenha.getText().equals(i.getSenha())) {
  199.                 atualizaUltAcesso();
  200.                 //buscarImobiliaria();
  201.                 //Envia os dados da imobiliaria para o proximo form
  202.                 //TelaPrincipal.imob = i;
  203.                 //new TelaPrincipal().setVisible(true);
  204.                 this.dispose();
  205.             } else {
  206.                 JOptionPane.showMessageDialog(null, "Usuário Inválido", "ATENÇÃO !", JOptionPane.ERROR_MESSAGE);
  207.             }
  208.         }
  209.     }                                        
  210.  
  211.     private void btnCadastrarActionPerformed(java.awt.event.ActionEvent evt) {                                            
  212.         CadastroDeImobiliaria ci = new CadastroDeImobiliaria();
  213.         ci.setVisible(true);
  214.     }                                            
  215.  
  216.     /**
  217.      * @param args the command line arguments
  218.      */
  219.     public static void main(String args[]) {
  220.         /* Set the Nimbus look and feel */
  221.         //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
  222.         /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
  223.          * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
  224.          */
  225.         try {
  226.             for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
  227.                 if ("Nimbus".equals(info.getName())) {
  228.                     javax.swing.UIManager.setLookAndFeel(info.getClassName());
  229.                     break;
  230.                 }
  231.             }
  232.         } catch (ClassNotFoundException ex) {
  233.             java.util.logging.Logger.getLogger(TelaLogin.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  234.         } catch (InstantiationException ex) {
  235.             java.util.logging.Logger.getLogger(TelaLogin.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  236.         } catch (IllegalAccessException ex) {
  237.             java.util.logging.Logger.getLogger(TelaLogin.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  238.         } catch (javax.swing.UnsupportedLookAndFeelException ex) {
  239.             java.util.logging.Logger.getLogger(TelaLogin.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  240.         }
  241.         //</editor-fold>
  242.  
  243.         /* Create and display the form */
  244.         java.awt.EventQueue.invokeLater(new Runnable() {
  245.             public void run() {
  246.                 new TelaLogin().setVisible(true);
  247.             }
  248.         });
  249.     }
  250.  
  251.     // Variables declaration - do not modify                    
  252.     private javax.swing.JButton btnCadastrar;
  253.     private javax.swing.JButton btnLogar;
  254.     private javax.swing.ButtonGroup buttonGroupTipoLogin;
  255.     private javax.swing.JLabel jLabel1;
  256.     private javax.swing.JLabel jLabel2;
  257.     private javax.swing.JLabel lblStatus;
  258.     private javax.swing.JPasswordField txtSenha;
  259.     private javax.swing.JTextField txtUsuario;
  260.     // End of variables declaration                  
  261. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement