Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2017
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.84 KB | None | 0 0
  1. package pk1;
  2.  
  3.  
  4. import javax.swing.*;
  5. import javax.swing.event.MenuEvent;
  6. import javax.swing.event.MenuListener;
  7.  
  8. import java.awt.*;
  9. import java.awt.event.*;
  10. import java.sql.*;
  11.  
  12. public class principal1 {
  13.  
  14. JFrame f; // Set frame, panel ...
  15. JPanel pcadastro, plogin, pmenu,pcad;
  16. JLabel l1, lnick, lsenha, nome, senha1, nick, usrativo, cpf, fone, cnpj, empresa, cidade, end, admcad;
  17. JButton b1, blogin;
  18. JTextField tusr, textnome, textnick, textcpf, textfone,textcnpj, textempresa, textcidade, textend, textadmcad;
  19. JPasswordField psf, senha;
  20. JMenuBar barra;
  21. JMenu cadastro, usuarios, relatorios, info, sobre;
  22. JMenuItem fisica, juridica, listar, buscar,administrador;
  23. JCheckBox adm;
  24. static int admstate,call=0;
  25. static String tipo;
  26.  
  27. public principal1() {
  28.  
  29. // novo frame teste
  30. f = new JFrame ("frame");
  31. // novo panel teste
  32. plogin = new JPanel();
  33. // adicionando plogin ao frame
  34. f.add(plogin);
  35. // chama o JPanel login
  36. login();
  37. // definições básicas do frame
  38. f.setSize(800, 700);
  39. f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  40. f.setResizable(false);
  41. f.setVisible(true);
  42. f.setLocationRelativeTo(null);
  43. }
  44.  
  45. public void cadastro() {
  46.  
  47. final String driver = "com.mysql.jdbc.Driver"; //Classe do Driver JDBC
  48.  
  49. //Criando os locais para inserção dos dados
  50.  
  51. pcadastro = new JPanel();
  52. nome = new JLabel("NOME:");
  53. textnome = new JTextField(30);
  54. nick = new JLabel("NICK:");
  55. textnick = new JTextField(30);
  56. senha1 = new JLabel("SENHA:");
  57. senha = new JPasswordField(30);
  58. cpf = new JLabel("CPF:");
  59. textcpf = new JTextField(30);
  60. fone = new JLabel("FONE:");
  61. textfone = new JTextField(30);
  62. cnpj = new JLabel("CNPJ:");
  63. textcnpj = new JTextField(30);
  64. empresa = new JLabel("EMPRESA:");
  65. textempresa = new JTextField(30);
  66. cidade = new JLabel("CIDADE:");
  67. textcidade = new JTextField(30);
  68. end = new JLabel("END.:");
  69. textend = new JTextField(30);
  70. admcad = new JLabel("ADM CAD.:");
  71. textadmcad = new JTextField(30);
  72. l1 = new JLabel("CADASTRO");
  73.  
  74. adm = new JCheckBox();
  75. adm.setText("ADICIONAR COMO ADM");
  76. adm.setBounds(470,480,180,25);
  77. System.out.println(tipo);
  78. if(tipo == "Fisica"){ //NOME,CPF,FONE
  79. textnome.setEnabled(true);
  80. textcpf.setEnabled(true);
  81. textfone.setEnabled(true);
  82. textnick.setEnabled(false);
  83. textcnpj.setEnabled(false);
  84. textcidade.setEnabled(false);
  85. textempresa.setEnabled(false);
  86. textend.setEnabled(false);
  87. senha.setEnabled(false);
  88. textadmcad.setEnabled(false);
  89.  
  90. }
  91. else if(tipo == "Juridica"){ //NOME,CPF,FONE,CNPJ,EMAIL,EMPRESA,CIDADE
  92. textnome.setEnabled(true);
  93. textcnpj.setEnabled(true);
  94. textfone.setEnabled(true);
  95. textempresa.setEnabled(true);
  96. textcidade.setEnabled(true);
  97. textend.setEnabled(true);
  98. textnick.setEnabled(false);
  99. textcpf.setEnabled(false);
  100. senha.setEnabled(false);
  101. textadmcad.setEnabled(false);
  102. }
  103. else if(tipo == "Adm"){ //NOME,CPF,FONE
  104. textnome.setEnabled(true);
  105. textnick.setEnabled(true);
  106. senha.setEnabled(true);
  107. textadmcad.setEnabled(true);
  108. pcadastro.add(adm);
  109. textcpf.setEnabled(false);
  110. textcnpj.setEnabled(false);
  111. textcidade.setEnabled(false);
  112. textempresa.setEnabled(false);
  113. textend.setEnabled(false);
  114. textfone.setEnabled(false);
  115. }
  116. else{
  117. textnome.setEnabled(false);
  118. textnick.setEnabled(false);
  119. senha.setEnabled(false);
  120. textadmcad.setEnabled(false);
  121. textcpf.setEnabled(false);
  122. textcnpj.setEnabled(false);
  123. textcidade.setEnabled(false);
  124. textempresa.setEnabled(false);
  125. textend.setEnabled(false);
  126. textfone.setEnabled(false);
  127. }
  128.  
  129. //Criando o botão
  130. JButton b1 = new JButton(" SALVAR ");
  131. //Ação do botão para inserir no banco
  132. b1.addActionListener(new ActionListener() {
  133. public void actionPerformed(ActionEvent e) {
  134. try {
  135.  
  136. if(adm.isSelected()) admstate=1;
  137. else admstate=0;
  138.  
  139. Class.forName(driver); //Carrega o Driver
  140. //Obtém a conexão com o banco
  141. Connection conn = DriverManager.getConnection("jdbc:Mysql://localhost:3306/bd", "root", "root");
  142. //Criar um statement para podermos mandar um SQL para o banco
  143. Statement stmt = conn.createStatement(); //Permite a execução do comando
  144. Statement stmt1 = conn.createStatement(); //Permite a execução do comando
  145.  
  146. //Registrando
  147. System.out.println(tipo);
  148. String senha1 = String.valueOf(senha.getPassword());
  149.  
  150. String sql = "SELECT * FROM bd.cadastroadm;";
  151. ResultSet rs = stmt.executeQuery(sql);
  152.  
  153.  
  154. if(tipo == "Fisico" || tipo == "Juridico" || tipo == "Administrador"){
  155. if(textcpf.getText().isEmpty() || textnick.getText().isEmpty() || senha1.equals("") || admcad.getText().isEmpty() ) {
  156. JOptionPane.showMessageDialog(null, "Preencha todos os campos");}}
  157. else {
  158.  
  159. if(call == 1) {
  160. 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() + "');";
  161. stmt1.execute(comando);
  162. JOptionPane.showMessageDialog(null, "Inserção OK");
  163. pcadastro.setBounds(0, 0, 0, 0);
  164. login();
  165. }
  166. if(call == 2) {
  167. 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() + "');";
  168. stmt1.execute(comando);
  169. JOptionPane.showMessageDialog(null, "Inserção OK");
  170. pcadastro.setBounds(0, 0, 0, 0);
  171. login();
  172. }
  173. if(call == 3) {
  174. 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() + "');";
  175. stmt1.execute(comando);
  176. JOptionPane.showMessageDialog(null, "Inserção OK");
  177. pcadastro.setBounds(0, 0, 0, 0);
  178. login();
  179.  
  180. }
  181.  
  182. }
  183.  
  184.  
  185. } catch (ClassNotFoundException ex) {
  186. JOptionPane.showMessageDialog(null, "Não foi possível carregar o driver.");
  187. ex.printStackTrace();
  188. } catch (SQLException ex) {
  189. if(call == 1){
  190. textcpf.setText("");
  191. JOptionPane.showMessageDialog(null, "Já existe este cpf cadastrado");}
  192. else if(call ==2){
  193. textcnpj.setText("");
  194. JOptionPane.showMessageDialog(null, "Já existe este cnpj cadastrado");}
  195. else if(call ==3){
  196. textnick.setText("");
  197. JOptionPane.showMessageDialog(null, "Já existe este usuario cadastrado");}
  198.  
  199. ex.printStackTrace();
  200. }
  201.  
  202. }
  203. });
  204.  
  205. //Definição de tamanho, fonte cor ...
  206.  
  207. l1.setBounds(135, 100, 400, 50);
  208. l1.setForeground(Color.RED);
  209. l1.setFont(new Font("Arial", Font.BOLD, 35));
  210.  
  211. pcadastro.setBackground(Color.WHITE);
  212. pcadastro.setLayout(null);
  213. pcadastro.add(nome);
  214. pcadastro.add(textnome);
  215. pcadastro.add(nick);
  216. pcadastro.add(textnick);
  217. pcadastro.add(senha);
  218. pcadastro.add(senha1);
  219. pcadastro.add(cpf);
  220. pcadastro.add(textcpf);
  221. pcadastro.add(fone);
  222. pcadastro.add(textfone);
  223. pcadastro.add(cnpj);
  224. pcadastro.add(textcnpj);
  225. pcadastro.add(empresa);
  226. pcadastro.add(textempresa);
  227. pcadastro.add(cidade);
  228. pcadastro.add(textcidade);
  229. pcadastro.add(end);
  230. pcadastro.add(textend);
  231. pcadastro.add(admcad);
  232. pcadastro.add(textadmcad);
  233. pcadastro.add(b1);
  234. pcadastro.add(l1);
  235.  
  236. f.add(pcadastro);
  237.  
  238.  
  239. b1.setFont(new Font("Arial", Font.BOLD, 25));
  240.  
  241. nome.setBounds(135, 200, 40, 10);
  242. nick.setBounds(400, 200, 40, 10);
  243. cpf.setBounds(135, 260, 40, 10);
  244. cnpj.setBounds(400, 260, 40, 10);
  245. empresa.setBounds(400, 320, 60, 10);
  246. cidade.setBounds(135, 320, 60, 10);
  247. end.setBounds(135, 380, 70, 10);
  248. fone.setBounds(400, 380, 60, 10);
  249. senha1.setBounds(135, 440, 60, 10);
  250. admcad.setBounds(400, 440, 60, 10);
  251.  
  252. textnome.setBounds(175, 195, 200, 25);
  253. textnick.setBounds(440, 195, 200, 25);
  254. textcpf.setBounds(175, 255, 200, 25);
  255. textcnpj.setBounds(440, 255, 200, 25);
  256. textcidade.setBounds(185, 315, 190, 25);
  257. textempresa.setBounds(465, 315, 175, 25);
  258. textend.setBounds(175,375,200,25);
  259. textfone.setBounds(440,375,200,25);
  260. senha.setBounds(180,435,195,25);
  261. textadmcad.setBounds(465,435,175,25);
  262.  
  263. b1.setBounds(300, 520, 180, 50);
  264. l1.setBounds(135, 100, 450, 50);
  265. }
  266.  
  267. public void login() {
  268.  
  269. final String driver = "com.mysql.jdbc.Driver";
  270.  
  271. plogin = new JPanel();
  272.  
  273. JLabel l1 = new JLabel("IMAGEM");
  274. lnick = new JLabel("Usuário: ");
  275. tusr = new JTextField(30);
  276. JLabel lsenha = new JLabel("Senha: ");
  277. senha = new JPasswordField(30);
  278. blogin = new JButton("Login");
  279.  
  280. l1.setBounds(135, 100, 450, 50);
  281. blogin.addActionListener(new ActionListener() {
  282. public void actionPerformed(ActionEvent e) {
  283.  
  284.  
  285. try {
  286. Class.forName(driver);
  287. Connection conn = DriverManager.getConnection("jdbc:Mysql://127.0.0.1/bd", "root", "root");
  288.  
  289. //String senha1 = String.valueOf(senha.getPassword()); // converter para string ja que o pw retorna char
  290.  
  291. String sql = "SELECT * FROM bd.cadastroadm where nick='" + tusr.getText() + "'";
  292.  
  293. PreparedStatement stmt = conn.prepareStatement(sql);
  294. ResultSet rs = stmt.executeQuery(sql);
  295.  
  296. rs.next();
  297.  
  298. String senha1 = new String(senha.getPassword());
  299.  
  300. if(rs.getString("nick").equals(tusr.getText()) && rs.getString("senha").equals(senha1)){
  301.  
  302. plogin.setBounds(0, 0, 0, 0);
  303. menu();
  304.  
  305. }
  306.  
  307. } catch (ClassNotFoundException ex) {
  308. JOptionPane.showMessageDialog(null, "Impossivel carregar o driver");
  309. ex.printStackTrace();
  310. } catch (SQLException ex) {
  311. 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);
  312.  
  313. senha.setText("");
  314. }
  315. }
  316. });
  317.  
  318. plogin.setBackground(Color.WHITE);
  319. lnick.setForeground(Color.RED);
  320. lsenha.setForeground(Color.RED);
  321. blogin.setForeground(Color.RED);
  322. blogin.setBackground(Color.WHITE);
  323. l1.setForeground(Color.RED);
  324.  
  325. lnick.setFont(new Font("Arial", Font.BOLD, 35));
  326. lsenha.setFont(new Font("Arial", Font.BOLD, 35));
  327. blogin.setFont(new Font("Arial", Font.BOLD, 35));
  328. l1.setFont(new Font("Arial", Font.BOLD, 35));
  329.  
  330.  
  331. plogin.setBounds(1, 1, 700, 725);
  332. l1.setBounds(320, 0, 400, 400);
  333. lnick.setBounds(200, 250, 400, 50);
  334. tusr.setBounds(200, 300, 400, 30);
  335. lsenha.setBounds(200, 350, 400, 50);
  336. senha.setBounds(200, 400, 400, 30);
  337. blogin.setBounds(300, 500, 200, 50);
  338.  
  339.  
  340. plogin.add(lnick);
  341. plogin.add(lsenha);
  342. plogin.add(senha);
  343. plogin.add(tusr);
  344. plogin.add(blogin);
  345. plogin.add(l1);
  346. plogin.setLayout(null);
  347. f.add(plogin);
  348.  
  349. }
  350.  
  351. public void menu(){
  352.  
  353. pmenu = new JPanel();
  354. barra = new JMenuBar();
  355. usrativo = new JLabel("User Ativo:"+tusr.getText());
  356. cadastro = new JMenu( "Cadastro" );
  357. fisica = new JMenuItem( "Pessoa Fisica" );
  358. juridica = new JMenuItem( "Pessoa Juridica" );
  359. administrador = new JMenuItem( "Administrador" );
  360.  
  361. usuarios = new JMenu("Usuarios");
  362. listar = new JMenuItem("Listar");
  363. buscar = new JMenuItem("Buscar");
  364.  
  365. relatorios = new JMenu("Relatorios");
  366.  
  367. sobre = new JMenu("Sobre");
  368.  
  369. sobre.addMenuListener(
  370. new MenuListener() {
  371. public void menuSelected(MenuEvent e) {
  372. 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);
  373. }
  374.  
  375. public void menuCanceled(MenuEvent arg0) {}
  376. public void menuDeselected(MenuEvent arg0) {}
  377. });
  378.  
  379. relatorios.addMenuListener(
  380. new MenuListener() {
  381. public void menuSelected(MenuEvent e) {
  382. //abrir relatorios
  383. }
  384. public void menuCanceled(MenuEvent arg0) {}
  385. public void menuDeselected(MenuEvent arg0) {}
  386. });
  387.  
  388. fisica.addActionListener(new ActionListener()
  389. {public void actionPerformed(ActionEvent e){
  390. pmenu.setBounds(0, 0, 0, 0);
  391. tipo = "Fisica";
  392. cadastro();
  393. }});
  394. juridica.addActionListener(new ActionListener()
  395. {public void actionPerformed(ActionEvent e){
  396. pmenu.setBounds(0, 0, 0, 0);
  397. tipo = "Juridica";
  398. cadastro();
  399. }});
  400. administrador.addActionListener(new ActionListener()
  401. {public void actionPerformed(ActionEvent e){
  402. pmenu.setBounds(0, 0, 0, 0);
  403. tipo = "Adm";;
  404. cadastro();
  405. }});
  406.  
  407. cadastro.add( fisica );
  408. cadastro.add( juridica );
  409. cadastro.add( administrador );
  410.  
  411. usuarios.add(listar);
  412. usuarios.add(buscar);
  413.  
  414. barra.add( cadastro );
  415. barra.add( usuarios );
  416. barra.add( relatorios );
  417. barra.add( sobre );
  418.  
  419. pmenu.setBackground(Color.WHITE);
  420. f.setJMenuBar( barra );
  421. f.add(pmenu);
  422. f.setVisible(true);
  423.  
  424. }
  425.  
  426. public static void main(String args[]) {
  427. new principal1(); // call frame
  428. }
  429. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement