Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package pk1;
- import javax.swing.*;
- import javax.swing.event.MenuEvent;
- import javax.swing.event.MenuListener;
- import java.awt.*;
- import java.awt.event.*;
- import java.sql.*;
- public class principal1 {
- JFrame f; // Set frame, panel ...
- JPanel pcadastro, plogin, pmenu,pcad;
- JLabel l1, lnick, lsenha, nome, senha1, nick, usrativo, cpf, fone, cnpj, empresa, cidade, end, admcad;
- JButton b1, blogin;
- JTextField tusr, textnome, textnick, textcpf, textfone,textcnpj, textempresa, textcidade, textend, textadmcad;
- JPasswordField psf, senha;
- JMenuBar barra;
- JMenu cadastro, usuarios, relatorios, info, sobre;
- JMenuItem fisica, juridica, listar, buscar,administrador;
- JCheckBox adm;
- static int admstate,call=0;
- static String tipo;
- public principal1() {
- // novo frame teste
- f = new JFrame ("frame");
- // novo panel teste
- plogin = new JPanel();
- // adicionando plogin ao frame
- f.add(plogin);
- // chama o JPanel login
- login();
- // definições básicas do frame
- f.setSize(800, 700);
- f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- f.setResizable(false);
- f.setVisible(true);
- f.setLocationRelativeTo(null);
- }
- public void cadastro() {
- final String driver = "com.mysql.jdbc.Driver"; //Classe do Driver JDBC
- //Criando os locais para inserção dos dados
- pcadastro = new JPanel();
- nome = new JLabel("NOME:");
- textnome = new JTextField(30);
- nick = new JLabel("NICK:");
- textnick = new JTextField(30);
- senha1 = new JLabel("SENHA:");
- senha = new JPasswordField(30);
- cpf = new JLabel("CPF:");
- textcpf = new JTextField(30);
- fone = new JLabel("FONE:");
- textfone = new JTextField(30);
- cnpj = new JLabel("CNPJ:");
- textcnpj = new JTextField(30);
- empresa = new JLabel("EMPRESA:");
- textempresa = new JTextField(30);
- cidade = new JLabel("CIDADE:");
- textcidade = new JTextField(30);
- end = new JLabel("END.:");
- textend = new JTextField(30);
- admcad = new JLabel("ADM CAD.:");
- textadmcad = new JTextField(30);
- l1 = new JLabel("CADASTRO");
- adm = new JCheckBox();
- adm.setText("ADICIONAR COMO ADM");
- adm.setBounds(470,480,180,25);
- System.out.println(tipo);
- if(tipo == "Fisica"){ //NOME,CPF,FONE
- textnome.setEnabled(true);
- textcpf.setEnabled(true);
- textfone.setEnabled(true);
- textnick.setEnabled(false);
- textcnpj.setEnabled(false);
- textcidade.setEnabled(false);
- textempresa.setEnabled(false);
- textend.setEnabled(false);
- senha.setEnabled(false);
- textadmcad.setEnabled(false);
- }
- else if(tipo == "Juridica"){ //NOME,CPF,FONE,CNPJ,EMAIL,EMPRESA,CIDADE
- textnome.setEnabled(true);
- textcnpj.setEnabled(true);
- textfone.setEnabled(true);
- textempresa.setEnabled(true);
- textcidade.setEnabled(true);
- textend.setEnabled(true);
- textnick.setEnabled(false);
- textcpf.setEnabled(false);
- senha.setEnabled(false);
- textadmcad.setEnabled(false);
- }
- else if(tipo == "Adm"){ //NOME,CPF,FONE
- textnome.setEnabled(true);
- textnick.setEnabled(true);
- senha.setEnabled(true);
- textadmcad.setEnabled(true);
- pcadastro.add(adm);
- textcpf.setEnabled(false);
- textcnpj.setEnabled(false);
- textcidade.setEnabled(false);
- textempresa.setEnabled(false);
- textend.setEnabled(false);
- textfone.setEnabled(false);
- }
- else{
- textnome.setEnabled(false);
- textnick.setEnabled(false);
- senha.setEnabled(false);
- textadmcad.setEnabled(false);
- textcpf.setEnabled(false);
- textcnpj.setEnabled(false);
- textcidade.setEnabled(false);
- textempresa.setEnabled(false);
- textend.setEnabled(false);
- textfone.setEnabled(false);
- }
- //Criando o botão
- JButton b1 = new JButton(" SALVAR ");
- //Ação do botão para inserir no banco
- b1.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- try {
- if(adm.isSelected()) admstate=1;
- else admstate=0;
- Class.forName(driver); //Carrega o Driver
- //Obtém a conexão com o banco
- Connection conn = DriverManager.getConnection("jdbc:Mysql://localhost:3306/bd", "root", "root");
- //Criar um statement para podermos mandar um SQL para o banco
- Statement stmt = conn.createStatement(); //Permite a execução do comando
- Statement stmt1 = conn.createStatement(); //Permite a execução do comando
- //Registrando
- System.out.println(tipo);
- String senha1 = String.valueOf(senha.getPassword());
- String sql = "SELECT * FROM bd.cadastroadm;";
- ResultSet rs = stmt.executeQuery(sql);
- if(tipo == "Fisico" || tipo == "Juridico" || tipo == "Administrador"){
- if(textcpf.getText().isEmpty() || textnick.getText().isEmpty() || senha1.equals("") || admcad.getText().isEmpty() ) {
- JOptionPane.showMessageDialog(null, "Preencha todos os campos");}}
- else {
- if(call == 1) {
- String comando = "INSERT into bd.cadastroadm values('" + tipo + "','" + textnome.getText() + "','" + textnick.getText() + "','" + senha1 + "','" + admstate + "','" + admcad.getText() + "','" + textcpf.getText() + "','" + textcnpj.getText() + "','" + textfone.getText() + "','" + textempresa.getText() + "','" + textcidade.getText() + "','" + textend.getText() + "');";
- stmt1.execute(comando);
- JOptionPane.showMessageDialog(null, "Inserção OK");
- pcadastro.setBounds(0, 0, 0, 0);
- login();
- }
- if(call == 2) {
- String comando = "INSERT into bd.cadastroadm values('" + tipo + "','" + textnome.getText() + "','" + textnick.getText() + "','" + senha1 + "','" + admstate + "','" + admcad.getText() + "','" + textcpf.getText() + "','" + textcnpj.getText() + "','" + textfone.getText() + "','" + textempresa.getText() + "','" + textcidade.getText() + "','" + textend.getText() + "');";
- stmt1.execute(comando);
- JOptionPane.showMessageDialog(null, "Inserção OK");
- pcadastro.setBounds(0, 0, 0, 0);
- login();
- }
- if(call == 3) {
- String comando = "INSERT into bd.cadastroadm values('" + tipo + "','" + textnome.getText() + "','" + textnick.getText() + "','" + senha1 + "','" + admstate + "','" + admcad.getText() + "','" + textcpf.getText() + "','" + textcnpj.getText() + "','" + textfone.getText() + "','" + textempresa.getText() + "','" + textcidade.getText() + "','" + textend.getText() + "');";
- stmt1.execute(comando);
- JOptionPane.showMessageDialog(null, "Inserção OK");
- pcadastro.setBounds(0, 0, 0, 0);
- login();
- }
- }
- } catch (ClassNotFoundException ex) {
- JOptionPane.showMessageDialog(null, "Não foi possível carregar o driver.");
- ex.printStackTrace();
- } catch (SQLException ex) {
- if(call == 1){
- textcpf.setText("");
- JOptionPane.showMessageDialog(null, "Já existe este cpf cadastrado");}
- else if(call ==2){
- textcnpj.setText("");
- JOptionPane.showMessageDialog(null, "Já existe este cnpj cadastrado");}
- else if(call ==3){
- textnick.setText("");
- JOptionPane.showMessageDialog(null, "Já existe este usuario cadastrado");}
- ex.printStackTrace();
- }
- }
- });
- //Definição de tamanho, fonte cor ...
- l1.setBounds(135, 100, 400, 50);
- l1.setForeground(Color.RED);
- l1.setFont(new Font("Arial", Font.BOLD, 35));
- pcadastro.setBackground(Color.WHITE);
- pcadastro.setLayout(null);
- pcadastro.add(nome);
- pcadastro.add(textnome);
- pcadastro.add(nick);
- pcadastro.add(textnick);
- pcadastro.add(senha);
- pcadastro.add(senha1);
- pcadastro.add(cpf);
- pcadastro.add(textcpf);
- pcadastro.add(fone);
- pcadastro.add(textfone);
- pcadastro.add(cnpj);
- pcadastro.add(textcnpj);
- pcadastro.add(empresa);
- pcadastro.add(textempresa);
- pcadastro.add(cidade);
- pcadastro.add(textcidade);
- pcadastro.add(end);
- pcadastro.add(textend);
- pcadastro.add(admcad);
- pcadastro.add(textadmcad);
- pcadastro.add(b1);
- pcadastro.add(l1);
- f.add(pcadastro);
- b1.setFont(new Font("Arial", Font.BOLD, 25));
- nome.setBounds(135, 200, 40, 10);
- nick.setBounds(400, 200, 40, 10);
- cpf.setBounds(135, 260, 40, 10);
- cnpj.setBounds(400, 260, 40, 10);
- empresa.setBounds(400, 320, 60, 10);
- cidade.setBounds(135, 320, 60, 10);
- end.setBounds(135, 380, 70, 10);
- fone.setBounds(400, 380, 60, 10);
- senha1.setBounds(135, 440, 60, 10);
- admcad.setBounds(400, 440, 60, 10);
- textnome.setBounds(175, 195, 200, 25);
- textnick.setBounds(440, 195, 200, 25);
- textcpf.setBounds(175, 255, 200, 25);
- textcnpj.setBounds(440, 255, 200, 25);
- textcidade.setBounds(185, 315, 190, 25);
- textempresa.setBounds(465, 315, 175, 25);
- textend.setBounds(175,375,200,25);
- textfone.setBounds(440,375,200,25);
- senha.setBounds(180,435,195,25);
- textadmcad.setBounds(465,435,175,25);
- b1.setBounds(300, 520, 180, 50);
- l1.setBounds(135, 100, 450, 50);
- }
- public void login() {
- final String driver = "com.mysql.jdbc.Driver";
- plogin = new JPanel();
- JLabel l1 = new JLabel("IMAGEM");
- lnick = new JLabel("Usuário: ");
- tusr = new JTextField(30);
- JLabel lsenha = new JLabel("Senha: ");
- senha = new JPasswordField(30);
- blogin = new JButton("Login");
- l1.setBounds(135, 100, 450, 50);
- blogin.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- try {
- Class.forName(driver);
- Connection conn = DriverManager.getConnection("jdbc:Mysql://127.0.0.1/bd", "root", "root");
- //String senha1 = String.valueOf(senha.getPassword()); // converter para string ja que o pw retorna char
- String sql = "SELECT * FROM bd.cadastroadm where nick='" + tusr.getText() + "'";
- PreparedStatement stmt = conn.prepareStatement(sql);
- ResultSet rs = stmt.executeQuery(sql);
- rs.next();
- String senha1 = new String(senha.getPassword());
- if(rs.getString("nick").equals(tusr.getText()) && rs.getString("senha").equals(senha1)){
- plogin.setBounds(0, 0, 0, 0);
- menu();
- }
- } catch (ClassNotFoundException ex) {
- JOptionPane.showMessageDialog(null, "Impossivel carregar o driver");
- ex.printStackTrace();
- } catch (SQLException ex) {
- JOptionPane.showMessageDialog(null, "O usuário e/ou senha estão incorretos. \n Por favor, insira corretamente os dados solicitados.", "ERRO NO LOGIN!!!", JOptionPane.ERROR_MESSAGE);
- senha.setText("");
- }
- }
- });
- plogin.setBackground(Color.WHITE);
- lnick.setForeground(Color.RED);
- lsenha.setForeground(Color.RED);
- blogin.setForeground(Color.RED);
- blogin.setBackground(Color.WHITE);
- l1.setForeground(Color.RED);
- lnick.setFont(new Font("Arial", Font.BOLD, 35));
- lsenha.setFont(new Font("Arial", Font.BOLD, 35));
- blogin.setFont(new Font("Arial", Font.BOLD, 35));
- l1.setFont(new Font("Arial", Font.BOLD, 35));
- plogin.setBounds(1, 1, 700, 725);
- l1.setBounds(320, 0, 400, 400);
- lnick.setBounds(200, 250, 400, 50);
- tusr.setBounds(200, 300, 400, 30);
- lsenha.setBounds(200, 350, 400, 50);
- senha.setBounds(200, 400, 400, 30);
- blogin.setBounds(300, 500, 200, 50);
- plogin.add(lnick);
- plogin.add(lsenha);
- plogin.add(senha);
- plogin.add(tusr);
- plogin.add(blogin);
- plogin.add(l1);
- plogin.setLayout(null);
- f.add(plogin);
- }
- public void menu(){
- pmenu = new JPanel();
- barra = new JMenuBar();
- usrativo = new JLabel("User Ativo:"+tusr.getText());
- cadastro = new JMenu( "Cadastro" );
- fisica = new JMenuItem( "Pessoa Fisica" );
- juridica = new JMenuItem( "Pessoa Juridica" );
- administrador = new JMenuItem( "Administrador" );
- usuarios = new JMenu("Usuarios");
- listar = new JMenuItem("Listar");
- buscar = new JMenuItem("Buscar");
- relatorios = new JMenu("Relatorios");
- sobre = new JMenu("Sobre");
- sobre.addMenuListener(
- new MenuListener() {
- public void menuSelected(MenuEvent e) {
- JOptionPane.showMessageDialog(null, "Desenvolvido por: Yuri Oliveira Alves. \n Versao: 1.0.0.\n Este software tem cunho educativo.\n Proposito: Demonstrar um sistema bancário de gerenciamento de clientes ", "SOBRE", JOptionPane.OK_OPTION);
- }
- public void menuCanceled(MenuEvent arg0) {}
- public void menuDeselected(MenuEvent arg0) {}
- });
- relatorios.addMenuListener(
- new MenuListener() {
- public void menuSelected(MenuEvent e) {
- //abrir relatorios
- }
- public void menuCanceled(MenuEvent arg0) {}
- public void menuDeselected(MenuEvent arg0) {}
- });
- fisica.addActionListener(new ActionListener()
- {public void actionPerformed(ActionEvent e){
- pmenu.setBounds(0, 0, 0, 0);
- tipo = "Fisica";
- cadastro();
- }});
- juridica.addActionListener(new ActionListener()
- {public void actionPerformed(ActionEvent e){
- pmenu.setBounds(0, 0, 0, 0);
- tipo = "Juridica";
- cadastro();
- }});
- administrador.addActionListener(new ActionListener()
- {public void actionPerformed(ActionEvent e){
- pmenu.setBounds(0, 0, 0, 0);
- tipo = "Adm";;
- cadastro();
- }});
- cadastro.add( fisica );
- cadastro.add( juridica );
- cadastro.add( administrador );
- usuarios.add(listar);
- usuarios.add(buscar);
- barra.add( cadastro );
- barra.add( usuarios );
- barra.add( relatorios );
- barra.add( sobre );
- pmenu.setBackground(Color.WHITE);
- f.setJMenuBar( barra );
- f.add(pmenu);
- f.setVisible(true);
- }
- public static void main(String args[]) {
- new principal1(); // call frame
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement