Advertisement
Guest User

Untitled

a guest
Dec 7th, 2017
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 38.10 KB | None | 0 0
  1. package pk1;
  2.  
  3. import javax.swing.*;
  4. import javax.swing.event.MenuEvent;
  5. import javax.swing.event.MenuListener;
  6. import javax.swing.table.DefaultTableModel;
  7.  
  8. import pk1.MinhaExcecao;
  9.  
  10. import java.awt.*;
  11. import java.awt.event.*;
  12. import java.sql.*;
  13.  
  14. public class principal1 {
  15.  
  16. JFrame f,fcad; // Set frame, panel ...
  17. JPanel plogin,pcad,pbusca,pcadastro,plistar,pbuscar;
  18. JLabel l1, l2, lnick, lsenha, nome, senha1, nick, usrativo, cpf, fone, cnpj, empresa, cidade, end, admcad, lbusca;
  19. JButton b1, blogin, bbuscar, bdeletar;
  20. JTextField tusr, textnome, textnick, textcpf, textfone,textcnpj, textempresa, textcidade, textend, textadmcad,tbusca;
  21. JPasswordField psf, senha;
  22. JMenuBar barra;
  23. JMenu cadastro, usuarios, relatorios, info, sobre;
  24. JMenuItem fisica, juridica, listar, administrador,buscar;
  25. JCheckBox adm;
  26.  
  27. static int admstate;
  28. static int isadm;
  29.  
  30.  
  31. public principal1() throws MinhaExcecao {
  32.  
  33. // novo frame teste
  34. f = new JFrame ("Gerenciamentos BDP");
  35. // chama o JPanel login
  36. login();
  37. //cadastro("Juridica");
  38. // definições básicas do frame
  39. f.setSize(800, 700);
  40. f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  41. f.setResizable(false);
  42. f.setVisible(true);
  43. f.setLocationRelativeTo(null);
  44. }
  45.  
  46. private class PessoaFisica {
  47.  
  48. final String driver = "com.mysql.jdbc.Driver"; //Classe do Driver JDBC
  49.  
  50. public String nome, cpf_cnpj, fone,tipo;
  51.  
  52. public PessoaFisica (String _nome, String _cpf_cnpj, String _fone, String _tipo)
  53. {
  54.  
  55. this.nome= _nome;
  56. this.cpf_cnpj= _cpf_cnpj;
  57. this.fone= _fone;
  58. this.tipo = _tipo;
  59.  
  60. }
  61.  
  62. public void insere(){
  63. try {
  64. Class.forName(driver); //Carrega o Driver
  65. Connection conn = DriverManager.getConnection("jdbc:Mysql://localhost:3306/bd", "root", "root"); //Obtém a conexão com o banco
  66. //Criar um statement para podermos mandar um SQL para o banco
  67. Statement stmt1 = conn.createStatement(); //Permite a execução do comando
  68.  
  69. if(nome.equals("") || cpf_cnpj.equals("") || fone.equals("") ) {
  70. JOptionPane.showMessageDialog(null, "Preencha todos os campos");
  71. }
  72.  
  73. else {
  74. int id=0;
  75. String comando = "INSERT into bd.cadastropf values('" + tipo + "','" + nome + "','" + cpf_cnpj + "','" + fone+ "' ,'" + id+ "' );";
  76. stmt1.execute(comando);
  77. JOptionPane.showMessageDialog(null, "Inserção OK");
  78. fcad.dispose();
  79.  
  80. }
  81.  
  82. } catch (ClassNotFoundException ex) {
  83. JOptionPane.showMessageDialog(null, "Não foi possível carregar o driver.");
  84. ex.printStackTrace();
  85. } catch (SQLException ex) {
  86. int errCode = ex.getErrorCode();
  87. System.out.println(errCode);
  88. if(errCode == 1062){
  89. JOptionPane.showMessageDialog(null, "Entrada: " + textnome.getText() + "já existente no banco\n");
  90. textnome.setText("");
  91. }
  92. ex.printStackTrace();
  93. }
  94. }
  95. }
  96.  
  97. private class PessoaJuridica extends PessoaFisica {
  98.  
  99. public String cidade, empresa, end;
  100. //cpf_cnpj//
  101. public PessoaJuridica (String _nome,String _cpf_cnpj,String _fone, String _tipo, String _cidade, String _empresa, String _end )
  102. {
  103. super(_nome, _cpf_cnpj, _fone, _tipo);
  104. this.cidade= _cidade;
  105. this.empresa= _empresa;
  106. this.end= _end;
  107. }
  108.  
  109. public void insere(){
  110. try {
  111. Class.forName(driver); //Carrega o Driver
  112. Connection conn = DriverManager.getConnection("jdbc:Mysql://localhost:3306/bd", "root", "root"); //Obtém a conexão com o banco
  113. //Criar um statement para podermos mandar um SQL para o banco
  114. Statement stmt1 = conn.createStatement(); //Permite a execução do comando
  115.  
  116. if(super.nome.equals("") || super.cpf_cnpj.equals("") || super.fone.equals("") || super.tipo.equals("") || cidade.equals("") || empresa.equals("") || end.equals("")) {
  117. JOptionPane.showMessageDialog(null, "Preencha todos os campos");
  118. }
  119.  
  120. else {
  121. int id = 0;
  122. String comando = "INSERT into bd.cadastropj values('" + tipo + "','" + nome + "','" + cpf_cnpj + "','" + fone + "','" + cidade + "','" + empresa+ "','" + end + "' , '" + id +"');";
  123. stmt1.execute(comando);
  124. JOptionPane.showMessageDialog(null, "Inserção OK");
  125. fcad.dispose();
  126.  
  127. }
  128.  
  129. } catch (ClassNotFoundException ex) {
  130. JOptionPane.showMessageDialog(null, "Não foi possível carregar o driver.");
  131. ex.printStackTrace();
  132. } catch (SQLException ex) {
  133. int errCode = ex.getErrorCode();
  134. System.out.println(errCode);
  135. if(errCode == 1062){
  136. JOptionPane.showMessageDialog(null, "Entrada: " + textnome.getText() + "já existente no banco\n");
  137. textnome.setText("");
  138. }
  139. ex.printStackTrace();
  140. }
  141. }
  142. }
  143.  
  144. private class Adm extends PessoaFisica {
  145.  
  146. public String nick, senha, admcad,tipo;
  147.  
  148. public Adm(String _nome, String _nick, String _senha,String _tipo, String _admcad )
  149. {
  150. super (_nome, null, null, null);
  151. this.nick = _nick;
  152. this.senha = _senha;
  153. this.admcad = _admcad;
  154. this.tipo = _tipo;
  155. }
  156.  
  157. public void insere(){
  158. try {
  159. Class.forName(driver); //Carrega o Driver
  160. Connection conn = DriverManager.getConnection("jdbc:Mysql://localhost:3306/bd", "root", "root"); //Obtém a conexão com o banco
  161. //Criar um statement para podermos mandar um SQL para o banco
  162. Statement stmt1 = conn.createStatement(); //Permite a execução do comando
  163. //Adm(String _nome, String _nick, String _senha, String _admcad, String _isadm )
  164.  
  165. if(super.nome.equals("") || nick.equals("") || senha.equals("") || admcad.equals("") ) {
  166. JOptionPane.showMessageDialog(null, "Preencha todos os campos");
  167. }
  168.  
  169. else {
  170. int id = 0;
  171. String comando = "INSERT into bd.cadastroadm values('" + tipo + "','" + nome + "','" + nick + "','" + senha + "','" + admstate + "','" + admcad+ "','"+ id +"');";
  172. stmt1.execute(comando);
  173. JOptionPane.showMessageDialog(null, "Inserção OK");
  174. fcad.dispose();
  175.  
  176. }
  177.  
  178. } catch (ClassNotFoundException ex) {
  179. JOptionPane.showMessageDialog(null, "Não foi possível carregar o driver.");
  180. ex.printStackTrace();
  181. } catch (SQLException ex) {
  182. int errCode = ex.getErrorCode();
  183. System.out.println(errCode);
  184. if(errCode == 1062){
  185. JOptionPane.showMessageDialog(null, "Entrada: " + textnome.getText() + "já existente no banco\n");
  186. textnome.setText("");
  187. }
  188. ex.printStackTrace();
  189. }
  190. }
  191. }
  192.  
  193. private void cadastro(String a){
  194.  
  195. fcad = new JFrame("Cadastro");
  196.  
  197. pcadastro = new JPanel();
  198.  
  199. //Criando os locais para inserção dos dados
  200. String tipo;
  201. tipo = a;
  202.  
  203. nome = new JLabel("NOME:");
  204. textnome = new JTextField(30);
  205. nick = new JLabel("NICK:");
  206. textnick = new JTextField(30);
  207. senha1 = new JLabel("SENHA:");
  208. senha = new JPasswordField(30);
  209. cpf = new JLabel("CPF:");
  210. textcpf = new JTextField(30);
  211. fone = new JLabel("FONE:");
  212. textfone = new JTextField(30);
  213. cnpj = new JLabel("CNPJ:");
  214. textcnpj = new JTextField(30);
  215. empresa = new JLabel("EMPRESA:");
  216. textempresa = new JTextField(30);
  217. cidade = new JLabel("CIDADE:");
  218. textcidade = new JTextField(30);
  219. end = new JLabel("END.:");
  220. textend = new JTextField(30);
  221. admcad = new JLabel("ADM CAD.:");
  222. textadmcad = new JTextField(30);
  223. l1 = new JLabel("CADASTRO");
  224.  
  225. adm = new JCheckBox();
  226. adm.setText("ADICIONAR COMO ADM");
  227. adm.setBounds(470,480,180,25);
  228. System.out.println(tipo);
  229.  
  230. switch(tipo){
  231. case "Fisica":
  232. textnome.setEnabled(true);
  233. textcpf.setEnabled(true);
  234. textfone.setEnabled(true);
  235. textnick.setEnabled(false);
  236. textcnpj.setEnabled(false);
  237. textcidade.setEnabled(false);
  238. textempresa.setEnabled(false);
  239. textend.setEnabled(false);
  240. senha.setEnabled(false);
  241. textadmcad.setEnabled(false);
  242. break;
  243.  
  244. case "Juridica":
  245. textnome.setEnabled(true);
  246. textcnpj.setEnabled(true);
  247. textfone.setEnabled(true);
  248. textempresa.setEnabled(true);
  249. textcidade.setEnabled(true);
  250. textend.setEnabled(true);
  251. textnick.setEnabled(false);
  252. textcpf.setEnabled(false);
  253. senha.setEnabled(false);
  254. textadmcad.setEnabled(false);
  255. break;
  256.  
  257. case "Adm":
  258. textnome.setEnabled(true);
  259. textnick.setEnabled(true);
  260. senha.setEnabled(true);
  261. textadmcad.setEnabled(true);
  262. textcpf.setEnabled(false);
  263. textcnpj.setEnabled(false);
  264. textcidade.setEnabled(false);
  265. textempresa.setEnabled(false);
  266. textend.setEnabled(false);
  267. textfone.setEnabled(false);
  268. pcadastro.add(adm);
  269. break;
  270.  
  271. case " ":
  272. textnome.setEnabled(false);
  273. textnick.setEnabled(false);
  274. senha.setEnabled(false);
  275. textadmcad.setEnabled(false);
  276. textcpf.setEnabled(false);
  277. textcnpj.setEnabled(false);
  278. textcidade.setEnabled(false);
  279. textempresa.setEnabled(false);
  280. textend.setEnabled(false);
  281. textfone.setEnabled(false);
  282. break;
  283. }
  284.  
  285. //Criando o botão
  286. JButton b1 = new JButton(" SALVAR ");
  287. //Ação do botão para inserir no banco
  288. b1.addActionListener(new ActionListener() {
  289. public void actionPerformed(ActionEvent e) {
  290.  
  291. if(adm.isSelected()){
  292. if(isadm == 1)
  293. admstate=1;
  294. else{
  295. adm.setSelected(false);
  296. admstate=0;
  297. JOptionPane.showMessageDialog(null, "O LOGIN ATUAL NÃO PERMITE CADASTRAR COMO ADM", "ERRO!", JOptionPane.ERROR_MESSAGE);
  298. }
  299. }
  300.  
  301. switch(tipo){
  302. case "Fisica":
  303. PessoaFisica pf = new PessoaFisica (textnome.getText(), textcpf.getText(), textfone.getText(), tipo);
  304. pf.insere();
  305. break;
  306.  
  307. case "Juridica":
  308. PessoaJuridica pj = new PessoaJuridica (textnome.getText(), textcnpj.getText(), textfone.getText(), tipo, textcidade.getText(), textempresa.getText(), textend.getText());
  309. pj.insere();
  310. break;
  311.  
  312. case "Adm":
  313. System.out.println(senha.getPassword());
  314. String senha1 = String.valueOf(senha.getPassword());
  315. Adm a = new Adm(textnome.getText(), textnick.getText(), senha1, tipo, textadmcad.getText());
  316. a.insere();
  317. break;
  318. }
  319. }
  320. });
  321.  
  322. //Definição de tamanho, fonte cor ...
  323.  
  324. l1.setForeground(Color.RED);
  325. l1.setFont(new Font("Arial", Font.BOLD, 35));
  326.  
  327. pcadastro.setBackground(Color.WHITE);
  328. pcadastro.setLayout(null);
  329. pcadastro.add(nome);
  330. pcadastro.add(textnome);
  331. pcadastro.add(nick);
  332. pcadastro.add(textnick);
  333. pcadastro.add(senha);
  334. pcadastro.add(senha1);
  335. pcadastro.add(cpf);
  336. pcadastro.add(textcpf);
  337. pcadastro.add(fone);
  338. pcadastro.add(textfone);
  339. pcadastro.add(cnpj);
  340. pcadastro.add(textcnpj);
  341. pcadastro.add(empresa);
  342. pcadastro.add(textempresa);
  343. pcadastro.add(cidade);
  344. pcadastro.add(textcidade);
  345. pcadastro.add(end);
  346. pcadastro.add(textend);
  347. pcadastro.add(admcad);
  348. pcadastro.add(textadmcad);
  349. pcadastro.add(b1);
  350. pcadastro.add(l1);
  351.  
  352. fcad.getContentPane().add(pcadastro);
  353. fcad.setSize(540, 600);
  354. fcad.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
  355. fcad.setResizable(false);
  356. fcad.setVisible(true);
  357. fcad.setLocationRelativeTo(null);
  358. b1.setFont(new Font("Arial", Font.BOLD, 25));
  359.  
  360. nome.setBounds(20, 200, 40, 10);
  361. nick.setBounds(270, 200, 40, 10);
  362. cpf.setBounds(20, 260, 40, 10);
  363. cnpj.setBounds(270, 260, 40, 10);
  364. empresa.setBounds(270, 320, 60, 10);
  365. cidade.setBounds(20, 320, 60, 10);
  366. end.setBounds(20, 380, 70, 10);
  367. fone.setBounds(270, 380, 60, 10);
  368. senha1.setBounds(20, 440, 60, 10);
  369. admcad.setBounds(270, 440, 60, 10);
  370.  
  371. adm.setBounds(190, 470, 200, 20);
  372.  
  373. textnome.setBounds(60, 195, 200, 25);
  374. textnick.setBounds(310, 195, 200, 25);
  375. textcpf.setBounds(60, 255, 200, 25);
  376. textcnpj.setBounds(310, 255, 200, 25);
  377. textcidade.setBounds(70, 315, 190, 25);
  378. textempresa.setBounds(335, 315, 175, 25);
  379. textend.setBounds(60,375,200,25);
  380. textfone.setBounds(310,375,200,25);
  381. senha.setBounds(70,435,195,25);
  382. textadmcad.setBounds(335,435,175,25);
  383.  
  384. b1.setBounds(180, 500, 180, 50);
  385. l1.setBounds(135, 80, 450, 50);
  386.  
  387. }
  388.  
  389. private void login() throws MinhaExcecao {
  390.  
  391. final String driver = "com.mysql.jdbc.Driver";
  392.  
  393. plogin = new JPanel();
  394.  
  395. JLabel l1 = new JLabel("IMAGEM");
  396. lnick = new JLabel("Usuário: ");
  397. tusr = new JTextField(30);
  398. JLabel lsenha = new JLabel("Senha: ");
  399. senha = new JPasswordField(30);
  400. blogin = new JButton("Login");
  401.  
  402. l1.setBounds(135, 100, 450, 50);
  403. blogin.addActionListener(new ActionListener() {
  404. public void actionPerformed(ActionEvent e) {
  405.  
  406.  
  407. try {
  408. Class.forName(driver);
  409. Connection conn = DriverManager.getConnection("jdbc:Mysql://127.0.0.1/bd", "root", "root");
  410.  
  411. //String senha1 = String.valueOf(senha.getPassword()); // converter para string ja que o pw retorna char
  412.  
  413. String sql = "SELECT * FROM bd.cadastroadm where user='" + tusr.getText() + "'";
  414.  
  415. PreparedStatement stmt = conn.prepareStatement(sql);
  416. ResultSet rs = stmt.executeQuery(sql);
  417.  
  418. rs.next();
  419.  
  420. String senha1 = new String(senha.getPassword());
  421. //System.out.println(rs.getString("adm"));
  422. isadm = rs.getInt("adm");
  423.  
  424. if(rs.getString("user").isEmpty() && rs.getString("senha").equals("")){
  425. JOptionPane.showMessageDialog(null, "Preencha todos os campos");
  426. throw new MinhaExcecao("Erro!");
  427. }
  428.  
  429. else if(rs.getString("user").equals(tusr.getText()) && rs.getString("senha").equals(senha1)){
  430.  
  431. plogin.setVisible(false);
  432. plogin.setBounds(0, 0, 0, 0);
  433. System.out.println(isadm);
  434. menu();
  435.  
  436. }
  437.  
  438. } catch (ClassNotFoundException ex) {
  439. JOptionPane.showMessageDialog(null, "Impossivel carregar o driver");
  440. ex.printStackTrace();
  441. } catch (SQLException ex) {
  442. 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);
  443. textnick.setText("");
  444. senha.setText("");
  445. } catch (MinhaExcecao e1) {
  446. // TODO Auto-generated catch block
  447. e1.printStackTrace();
  448. }
  449. }
  450. });
  451.  
  452. plogin.setBackground(Color.WHITE);
  453. lnick.setForeground(Color.RED);
  454. lsenha.setForeground(Color.RED);
  455. blogin.setForeground(Color.RED);
  456. blogin.setBackground(Color.WHITE);
  457. l1.setForeground(Color.RED);
  458.  
  459. lnick.setFont(new Font("Arial", Font.BOLD, 35));
  460. lsenha.setFont(new Font("Arial", Font.BOLD, 35));
  461. blogin.setFont(new Font("Arial", Font.BOLD, 35));
  462. l1.setFont(new Font("Arial", Font.BOLD, 35));
  463.  
  464.  
  465. plogin.setBounds(1, 1, 700, 725);
  466. l1.setBounds(320, 0, 400, 400);
  467. lnick.setBounds(200, 250, 400, 50);
  468. tusr.setBounds(200, 300, 400, 30);
  469. lsenha.setBounds(200, 350, 400, 50);
  470. senha.setBounds(200, 400, 400, 30);
  471. blogin.setBounds(300, 500, 200, 50);
  472.  
  473.  
  474. plogin.add(lnick);
  475. plogin.add(lsenha);
  476. plogin.add(senha);
  477. plogin.add(tusr);
  478. plogin.add(blogin);
  479. plogin.add(l1);
  480. plogin.setLayout(null);
  481. f.getContentPane().add(plogin);
  482.  
  483. }
  484.  
  485. private void buscar(){
  486.  
  487. pbuscar = new JPanel();
  488. pbuscar.setBounds(1, 1, 700, 725);
  489. pbuscar.setLayout(null);
  490.  
  491. final String driver = "com.mysql.jdbc.Driver";
  492.  
  493. l1 = new JLabel("Buscar Cadastro");
  494. l2 = new JLabel("Buscar Por: ");
  495.  
  496. JRadioButton PF = new JRadioButton();
  497. JRadioButton PJ = new JRadioButton();
  498.  
  499. PF.setText("Pessoa Fisica");
  500. PJ.setText("Pessoa Juridica");
  501.  
  502. ButtonGroup group = new ButtonGroup();
  503. group.add(PF);
  504. group.add(PJ);
  505.  
  506. tbusca = new JTextField(30);
  507. tbusca.setVisible(true);
  508. JTable tabela1 = new JTable();
  509. JTable tabela2 = new JTable();
  510.  
  511. //tabela1.setEnabled(false);
  512. //tabela2.setEnabled(false);
  513. //////////////////////pf//////////////////////////////////////////
  514. String[] colunas1 = new String[]{"Tipo","Nome","Cpf","Fone"};
  515. String[][] dados1 = new String[][]{{"", "", "", ""}};
  516. DefaultTableModel modelo1 = new DefaultTableModel(dados1, colunas1);
  517. tabela1.setModel(modelo1);
  518. tabela1.getTableHeader().setResizingAllowed(false);
  519. tabela1.getColumnModel().getColumn(0);
  520. tabela1.getColumnModel().getColumn(1);
  521. tabela1.getColumnModel().getColumn(2);
  522. tabela1.getColumnModel().getColumn(3);
  523. //////////////////////PF//////////////////////////////////////////
  524. //////////////////////PJ//////////////////////////////////////////
  525. /*tabela2.setModel(new DefaultTableModel(new String[][]{{"", "", "", "", "","",""}}, new String[]{"Tipo","Nome","Cnpj","Fone","Cidade","Empresa","End."}) {
  526. public boolean isCellEditable(int row, int column) {
  527. return false;
  528. }*/
  529.  
  530. String[] colunas2 = new String[]{"Tipo","Nome","Cnpj","Fone","Cidade","Empresa","End."};
  531. String[][] dados2 = new String[][]{{"", "", "", "", "","",""}};
  532. DefaultTableModel modelo2 = new DefaultTableModel(dados2, colunas2);
  533. tabela2.setModel(modelo2);
  534. tabela2.getTableHeader().setResizingAllowed(false);
  535. tabela2.getColumnModel().getColumn(0);
  536. tabela2.getColumnModel().getColumn(1);
  537. tabela2.getColumnModel().getColumn(2);
  538. tabela2.getColumnModel().getColumn(3);
  539. tabela2.getColumnModel().getColumn(4);
  540. tabela2.getColumnModel().getColumn(5);
  541. tabela2.getColumnModel().getColumn(6);
  542. /////////////////////PJ//////////////////////////////////////////
  543. bdeletar = new JButton("Deletar");
  544. bbuscar = new JButton("Buscar");
  545. bbuscar.addActionListener(new ActionListener() {
  546. public void actionPerformed(ActionEvent e) {
  547.  
  548. try {
  549. Class.forName(driver);
  550. Connection conn = DriverManager.getConnection("jdbc:Mysql://127.0.0.1/bd", "root", "root");
  551. if(PF.isSelected()){
  552. String sql1 = "SELECT * FROM bd.cadastropf WHERE nome LIKE '%" + tbusca.getText() + "%'";
  553. //Select * from cadastropf Where nome LIKE 'yu%'//
  554. PreparedStatement stmt1 = conn.prepareStatement(sql1);
  555. ResultSet rs1 = stmt1.executeQuery(sql1);
  556.  
  557. int cont1=0;
  558.  
  559. while(rs1.next()){
  560. tabela1.setValueAt(rs1.getString("tipo"), cont1, 0);
  561. tabela1.setValueAt(rs1.getString("nome"), cont1, 1);
  562. tabela1.setValueAt(rs1.getString("cpf"), cont1, 2);
  563. tabela1.setValueAt(rs1.getString("fone"), cont1, 3);
  564.  
  565. modelo1.addRow(new String[]{"", "", "", ""});
  566. cont1++;
  567. }
  568. modelo1.removeRow(cont1--);
  569.  
  570.  
  571. }
  572. else if(PJ.isSelected()){
  573. String sql2 = "SELECT * FROM bd.cadastropj WHERE nome LIKE '%" + tbusca.getText() + "%'";
  574. //Select * from cadastropj Where nome LIKE 'yu%'//
  575. PreparedStatement stmt2 = conn.prepareStatement(sql2);
  576. ResultSet rs2 = stmt2.executeQuery(sql2);
  577. int cont2=0;
  578.  
  579. while(rs2.next()){
  580. tabela2.setValueAt(rs2.getString("tipo"), cont2, 0);
  581. tabela2.setValueAt(rs2.getString("nome"), cont2, 1);
  582. tabela2.setValueAt(rs2.getString("cnpj"), cont2, 2);
  583. tabela2.setValueAt(rs2.getString("fone"), cont2, 3);
  584. tabela2.setValueAt(rs2.getString("cidade"), cont2, 4);
  585. tabela2.setValueAt(rs2.getString("empresa"), cont2, 5);
  586. tabela2.setValueAt(rs2.getString("end"), cont2, 6);
  587.  
  588. modelo2.addRow(new String[]{"", "", "", "", "","",""});
  589. cont2++;
  590. }
  591.  
  592. modelo2.removeRow(cont2--);
  593.  
  594. }
  595. } catch (ClassNotFoundException ex) {
  596. JOptionPane.showMessageDialog(null, "Impossivel carregar o driver");
  597. ex.printStackTrace();
  598. } catch (SQLException ex) {
  599. 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);
  600. ex.printStackTrace();
  601. }
  602. }});
  603.  
  604. bdeletar.addActionListener(new ActionListener() {
  605. public void actionPerformed(ActionEvent e) {
  606. if (tabela1.getSelectedRow() != -1 || tabela2.getSelectedRow() != -1 ) {
  607. int linha = tabela1.getSelectedRow();
  608. int linha2 = tabela2.getSelectedRow();
  609. try {
  610. Class.forName(driver);
  611. Connection conn = DriverManager.getConnection("jdbc:Mysql://127.0.0.1/bd", "root", "root");
  612. if(PF.isSelected()){
  613. String sql = "SELECT * FROM cadastropf";
  614. PreparedStatement stmt = conn.prepareStatement(sql);
  615.  
  616. String nome = "Deletar o cliente de nome: " + tabela1.getValueAt(linha, 1).toString() + " ?";
  617. int opcao_escolhida = JOptionPane.showConfirmDialog(null, nome, "Exclusão ", JOptionPane.YES_NO_OPTION);
  618. if (opcao_escolhida == JOptionPane.YES_OPTION) {
  619. sql = "select * from bd.cadastropf Where nome = " + (tabela1.getValueAt(linha, 1))+"";
  620. int conseguiu_excluir = stmt.executeUpdate("DELETE FROM bd.cadastropf WHERE nome='" + (tabela1.getValueAt(linha, 1).toString()) + "'");
  621. if (conseguiu_excluir == 1) {
  622. modelo1.removeRow(linha);
  623. tabela1.setModel(modelo1);
  624. JOptionPane.showMessageDialog(null, "Exclusão realizada com sucesso");
  625. }
  626. }
  627. }
  628. else if(PJ.isSelected()){
  629. String sql2 = "SELECT * FROM cadastropj";
  630. PreparedStatement stmt2 = conn.prepareStatement(sql2);
  631.  
  632. String nome = "Deletar o cliente de nome: " + tabela2.getValueAt(linha2, 1).toString() + " ?";
  633. int opcao_escolhida = JOptionPane.showConfirmDialog(null, nome, "Exclusão ", JOptionPane.YES_NO_OPTION);
  634. if (opcao_escolhida == JOptionPane.YES_OPTION) {
  635. sql2 = "select * from bd.cadastropj Where nome = " + (tabela2.getValueAt(linha2, 1))+"";
  636. int conseguiu_excluir = stmt2.executeUpdate("DELETE FROM bd.cadastropj WHERE nome='" + (tabela2.getValueAt(linha2, 1).toString()) + "'");
  637. if (conseguiu_excluir == 1) {
  638. modelo2.removeRow(linha2);
  639. tabela2.setModel(modelo2);
  640. JOptionPane.showMessageDialog(null, "Exclusão realizada com sucesso");
  641. }
  642. }
  643. }
  644. }
  645.  
  646. catch (ClassNotFoundException ex) {
  647. JOptionPane.showMessageDialog(null, "Impossivel carregar o driver");
  648. ex.printStackTrace();
  649. } catch (SQLException ex) {
  650. 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);
  651. ex.printStackTrace();
  652. }
  653. }}});
  654.  
  655. l1.setBounds(340, 50, 450, 50);
  656. bbuscar.setBounds(450, 100, 100, 50);
  657. bdeletar.setBounds(600, 100, 100, 50);
  658. tbusca.setBounds(220, 110, 200, 30);
  659.  
  660. tabela1.getAutoResizeMode();
  661. JScrollPane jspane1 = new JScrollPane();
  662. jspane1.setViewportView(tabela1);
  663. jspane1.setBounds(100,210,580,87);
  664. pbuscar.add(jspane1);
  665.  
  666. tabela2.getAutoResizeMode();
  667. JScrollPane jspane2 = new JScrollPane();
  668. jspane2.setViewportView(tabela2);
  669. jspane2.setBounds(100,350,580,87);
  670. pbuscar.add(jspane2);
  671.  
  672. PF.setBounds(300, 160 , 90, 25);
  673. PJ.setBounds(410, 160 , 90, 25);
  674. pbuscar.add(PF);
  675. pbuscar.add(PJ);
  676.  
  677. pbuscar.add(bdeletar);
  678. pbuscar.add(bbuscar);
  679. pbuscar.add(l1);
  680. pbuscar.add(tbusca);
  681.  
  682. f.getContentPane().removeAll();
  683. f.revalidate();
  684. f.repaint();
  685. f.getContentPane().add(pbuscar);
  686.  
  687. }
  688.  
  689. private void listar(){
  690.  
  691. JPanel plistar = new JPanel();
  692. plistar.setBounds(1, 1, 700, 725);
  693. plistar.setLayout(null);
  694.  
  695.  
  696. final String driver = "com.mysql.jdbc.Driver";
  697.  
  698. JLabel l1 = new JLabel("Lista de Cadastros");
  699. JLabel l2 = new JLabel("Pessoa Fisica:");
  700. JLabel l3 = new JLabel("Pessoa Juridica:");
  701. JLabel l4 = new JLabel("Administradores:");
  702. JTable tabela1 = new JTable();
  703. JTable tabela2 = new JTable();
  704. JTable tabela3 = new JTable();
  705.  
  706.  
  707. tabela1.setEnabled(false);
  708. tabela2.setEnabled(false);
  709. tabela3.setEnabled(false);
  710. //////////////////////ADM//////////////////////////////////////////
  711. String[] colunas = new String[]{"Tipo","Nome","User","Senha","ADM","ADMCAD"};
  712. String[][] dados = new String[][]{{"", "", "", "", "",""}};
  713. DefaultTableModel modelo1 = new DefaultTableModel(dados, colunas);
  714. tabela1.setModel(modelo1);
  715. tabela1.getTableHeader().setResizingAllowed(false);
  716. tabela1.getColumnModel().getColumn(0);
  717. tabela1.getColumnModel().getColumn(1);
  718. tabela1.getColumnModel().getColumn(2);
  719. tabela1.getColumnModel().getColumn(3);
  720. tabela1.getColumnModel().getColumn(4);
  721. tabela1.getColumnModel().getColumn(5);
  722. //////////////////////ADM//////////////////////////////////////////
  723. //////////////////////pf//////////////////////////////////////////
  724. String[] colunas2 = new String[]{"Tipo","Nome","Cpf","Fone"};
  725. String[][] dados2 = new String[][]{{"", "", "", ""}};
  726. DefaultTableModel modelo2 = new DefaultTableModel(dados2, colunas2);
  727. tabela2.setModel(modelo2);
  728. tabela2.getTableHeader().setResizingAllowed(false);
  729. tabela2.getColumnModel().getColumn(0);
  730. tabela2.getColumnModel().getColumn(1);
  731. tabela2.getColumnModel().getColumn(2);
  732. tabela2.getColumnModel().getColumn(3);
  733. //////////////////////PF//////////////////////////////////////////
  734. //////////////////////PJ//////////////////////////////////////////
  735. String[] colunas3 = new String[]{"Tipo","Nome","Cnpj","Fone","Cidade","Empresa","End."};
  736. String[][] dados3 = new String[][]{{"", "", "", "", "","",""}};
  737. DefaultTableModel modelo3 = new DefaultTableModel(dados3, colunas3);
  738. tabela3.setModel(modelo3);
  739. tabela3.getTableHeader().setResizingAllowed(false);
  740. tabela3.getColumnModel().getColumn(0);
  741. tabela3.getColumnModel().getColumn(1);
  742. tabela3.getColumnModel().getColumn(2);
  743. tabela3.getColumnModel().getColumn(3);
  744. tabela3.getColumnModel().getColumn(4);
  745. tabela3.getColumnModel().getColumn(5);
  746. tabela3.getColumnModel().getColumn(6);
  747. //////////////////////PJ//////////////////////////////////////////
  748.  
  749. try {
  750. //JOptionPane.showMessageDialog(null, "Carregando Dados.. \n"); //Limpar dados
  751.  
  752. Class.forName(driver);
  753. Connection conn = DriverManager.getConnection("jdbc:Mysql://127.0.0.1/bd", "root", "root");
  754. String sql1 = "SELECT * FROM bd.cadastroadm order by id_adm DESC";
  755. String sql2 = "SELECT * FROM bd.cadastropf order by id_pf DESC";
  756. String sql3 = "SELECT * FROM bd.cadastropj order by id_pj DESC";
  757. PreparedStatement stmt1 = conn.prepareStatement(sql1);
  758. ResultSet rs1 = stmt1.executeQuery(sql1);
  759. PreparedStatement stmt2 = conn.prepareStatement(sql2);
  760. ResultSet rs2 = stmt2.executeQuery(sql2);
  761. PreparedStatement stmt3 = conn.prepareStatement(sql3);
  762. ResultSet rs3 = stmt3.executeQuery(sql3);
  763.  
  764. int cont1=0,cont2=0,cont3=0;
  765.  
  766. while(rs1.next()){
  767.  
  768. String senha="Privado";
  769. tabela1.setValueAt(rs1.getString("tipo"), cont1, 0);
  770. tabela1.setValueAt(rs1.getString("nome"), cont1, 1);
  771. tabela1.setValueAt(rs1.getString("user"), cont1, 2);
  772. tabela1.setValueAt(senha, cont1, 3);
  773. tabela1.setValueAt(rs1.getString("adm"), cont1, 4);
  774. tabela1.setValueAt(rs1.getString("admcad"), cont1, 5);
  775.  
  776. modelo1.addRow(new String[]{"", "", "", "", "",""});
  777. cont1++;
  778. }
  779. while(rs2.next()){
  780. tabela2.setValueAt(rs2.getString("tipo"), cont2, 0);
  781. tabela2.setValueAt(rs2.getString("nome"), cont2, 1);
  782. tabela2.setValueAt(rs2.getString("cpf"), cont2, 2);
  783. tabela2.setValueAt(rs2.getString("fone"), cont2, 3);
  784.  
  785. modelo2.addRow(new String[]{"", "", "", ""});
  786. cont2++;
  787. }
  788. while(rs3.next()){
  789. tabela3.setValueAt(rs3.getString("tipo"), cont3, 0);
  790. tabela3.setValueAt(rs3.getString("nome"), cont3, 1);
  791. tabela3.setValueAt(rs3.getString("cnpj"), cont3, 2);
  792. tabela3.setValueAt(rs3.getString("fone"), cont3, 3);
  793. tabela3.setValueAt(rs3.getString("cidade"), cont3, 4);
  794. tabela3.setValueAt(rs3.getString("empresa"), cont3, 5);
  795. tabela3.setValueAt(rs3.getString("end"), cont3, 6);
  796.  
  797. modelo3.addRow(new String[]{"", "", "", "", "","",""});
  798. cont3++;
  799. }
  800. modelo1.removeRow(cont1--);
  801. modelo2.removeRow(cont2--);
  802. modelo3.removeRow(cont3--);
  803.  
  804. } catch (ClassNotFoundException ex) {
  805. JOptionPane.showMessageDialog(null, "Impossivel carregar o driver");
  806. ex.printStackTrace();
  807. } catch (SQLException ex) {
  808. JOptionPane.showMessageDialog(null, "DEU MERDA", "ERRO ", JOptionPane.ERROR_MESSAGE);
  809. ex.printStackTrace();
  810. }
  811.  
  812. l1.setBounds(340, 50, 450, 50);
  813. l2.setBounds(100,120,450,87);
  814. l3.setBounds(100,270,450,87);
  815. l4.setBounds(100,420,450,87);
  816.  
  817.  
  818. tabela1.getAutoResizeMode();
  819. JScrollPane jspane1 = new JScrollPane();
  820. jspane1.setViewportView(tabela1);
  821. jspane1.setBounds(100,480,580,87);
  822.  
  823.  
  824. plistar.add(jspane1);
  825.  
  826. tabela2.getAutoResizeMode();
  827. JScrollPane jspane2 = new JScrollPane();
  828. jspane2.setViewportView(tabela2);
  829. jspane2.setBounds(100,190,580,87);
  830. plistar.add(jspane2);
  831.  
  832. tabela3.getAutoResizeMode();
  833. JScrollPane jspane3 = new JScrollPane();
  834. jspane3.setViewportView(tabela3);
  835. jspane3.setBounds(100,330,580,87);
  836.  
  837. plistar.add(jspane3);
  838. plistar.add(l1);
  839. plistar.add(l2);
  840. plistar.add(l3);
  841. plistar.add(l4);
  842. plistar.setVisible(true);
  843.  
  844. f.getContentPane().removeAll();
  845. f.revalidate();
  846. f.repaint();
  847. f.getContentPane().add(plistar);
  848. }
  849.  
  850. private void menu(){
  851.  
  852. barra = new JMenuBar();
  853. cadastro = new JMenu( "Cadastro" );
  854. fisica = new JMenuItem( "Pessoa Fisica" );
  855. juridica = new JMenuItem( "Pessoa Juridica" );
  856. administrador = new JMenuItem( "Administrador" );
  857.  
  858. usuarios = new JMenu("Usuarios");
  859. listar = new JMenuItem("Listar");
  860. buscar = new JMenuItem("Buscar");
  861.  
  862. relatorios = new JMenu("Relatorios");
  863.  
  864. sobre = new JMenu("Sobre");
  865.  
  866. sobre.addMenuListener(
  867. new MenuListener() {
  868. public void menuSelected(MenuEvent e) {
  869. 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);
  870. }
  871.  
  872. public void menuCanceled(MenuEvent arg0) {}
  873. public void menuDeselected(MenuEvent arg0) {}
  874. });
  875.  
  876. relatorios.addMenuListener(
  877. new MenuListener() {
  878. public void menuSelected(MenuEvent e) {
  879. //abrir relatorios
  880. }
  881. public void menuCanceled(MenuEvent arg0) {}
  882. public void menuDeselected(MenuEvent arg0) {}
  883. });
  884.  
  885.  
  886. listar.addActionListener(new ActionListener()
  887. {public void actionPerformed(ActionEvent e){
  888. listar ();
  889. }});
  890. buscar.addActionListener(new ActionListener()
  891. {public void actionPerformed(ActionEvent e){
  892. buscar();
  893. }});
  894. fisica.addActionListener(new ActionListener()
  895. {public void actionPerformed(ActionEvent e){
  896. cadastro("Fisica");
  897. }});
  898. juridica.addActionListener(new ActionListener()
  899. {public void actionPerformed(ActionEvent e){
  900. cadastro("Juridica");
  901. }});
  902. administrador.addActionListener(new ActionListener()
  903. {public void actionPerformed(ActionEvent e){
  904. cadastro("Adm");
  905. }});
  906.  
  907. cadastro.add( fisica );
  908. cadastro.add( juridica );
  909. cadastro.add( administrador );
  910.  
  911. usuarios.add(listar);
  912. usuarios.add(buscar);
  913.  
  914. barra.add( cadastro );
  915. barra.add( usuarios );
  916. barra.add( relatorios );
  917. barra.add( sobre );
  918.  
  919. f.setJMenuBar(barra);
  920. f.setVisible(true);
  921.  
  922. }
  923.  
  924. public static void main(String args[]) throws MinhaExcecao {
  925. new principal1(); // call frame
  926. }
  927. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement