Guest User

Untitled

a guest
Jan 14th, 2019
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.78 KB | None | 0 0
  1. /*
  2. * To change this template, choose Tools | Templates
  3. * and open the template in the editor.
  4. */
  5. package javaapplication3;
  6.  
  7. import java.sql.Connection;
  8. import java.sql.DriverManager;
  9. import java.sql.ResultSet;
  10. import java.sql.SQLException;
  11. import java.sql.Statement;
  12. import javax.swing.JOptionPane;
  13.  
  14. /**
  15. *
  16. * @author rodrigo
  17. */
  18. public class JavaApplication3 {
  19.  
  20. private static final String DB_URL = "jdbc:mysql://localhost/devweb2";
  21. private static final String DB_USER = "root";
  22. private static final String DB_PASSWD = "root";
  23.  
  24. public static void main(String[] args) throws GerenciadorException {
  25. try {
  26. Connection conexao = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWD);
  27.  
  28. Statement st = conexao.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
  29. ResultSet rs = st.executeQuery("SELECT * FROM usuario");
  30.  
  31. Seguranca Seguranca = new Seguranca();
  32. Funcoes f = new Funcoes();
  33.  
  34.  
  35. //Captura os dados do usuário e coloca em um array de string
  36. String msg, m1, m2, mc;
  37. int resposta;
  38. msg = JOptionPane.showInputDialog("Para cadastrar um novo usuário digite os dados precedios por"
  39. + " vírgula na seguinte ordem (Login, Nome Completo, E-mail, Senha,) \n "
  40. + "Para alterar a senha de um usuário ja existente, digite apenas o login:");
  41. String s[] = msg.split("\\,");
  42.  
  43. //entra no while para Verificar se o login já existe na base de dados
  44. while (rs.next()) {
  45.  
  46. try {
  47. //Verifica se o login já existe na base de dados
  48. if (rs.getString("login").equals(s[0])) {
  49. resposta = JOptionPane.showConfirmDialog(null, "O login \"" + s[0] + "\" já existe na base de dados!"
  50. + " \n Para modificar a senha digite OK, para sair clique em NO ou CANCEL.");
  51.  
  52. if (resposta == JOptionPane.YES_OPTION) {
  53. mc = Seguranca.criptografar(f.validaSenha()); //usa a classe para criptografar a senha
  54. int id2 = f.retornaIDdeLogin(s[0]); //descobre o id da linha do login que ja existe
  55.  
  56. rs.absolute(id2); //vai até alinha do id fornecido anteriormente
  57. rs.updateString("senha", mc); //realiza a modificação na senha
  58. rs.updateRow();
  59.  
  60. JOptionPane.showMessageDialog(null, "Sua senha foi alterada com sucesso!");
  61. System.exit(0); //sai
  62. }
  63. if (resposta == JOptionPane.NO_OPTION) {
  64. System.exit(0); //sai
  65. }
  66. // ********** if (resposta == JOptionPane.CANCEL_OPTION){
  67. // ver o q fazer nestes outros botoes e ver como alterar apenas a senha na inserção
  68. // }
  69. }
  70.  
  71. } catch (NullPointerException e) {
  72. e.printStackTrace();
  73. }
  74. }
  75.  
  76. //sai do while, CASO FOI DIGITADO TODOS OS CAMPOS PARA A INSERÇÃO DE UM NOVO CADASTRO
  77.  
  78. //Criptografa a senha
  79. s[3] = Seguranca.criptografar(s[3]);
  80.  
  81. // Insere os dados na tabela
  82. rs.moveToInsertRow(); // moves cursor to the insert row
  83. rs.updateString("login", s[0]); // updates the
  84. rs.updateString("nomecompleto", s[1]);
  85. rs.updateString("email", s[2]);
  86. rs.updateString("senha", s[3]);
  87. // first column of the insert row to be AINSWORTH
  88. // rs.updateInt(1, 2); // updates the second column to be 35
  89. // rs.updateBoolean(3, true); // updates the third column to true
  90. rs.insertRow();
  91. // rs.moveToCurrentRow();
  92.  
  93. //Mostra na tela o cabeçalho da tabela.
  94. System.out.println("ID\tLogin\tNome Completo\tE-mail\tsenha");
  95.  
  96. //Mostra na tela os dados da tabela
  97. rs.beforeFirst();
  98. while (rs.next()) {
  99. int id = rs.getInt("id");
  100. String login = rs.getString("login");
  101. String nomecompleto = rs.getString("nomecompleto");
  102. String email = rs.getString("email");
  103. String senha = rs.getString("senha");
  104. System.out.println(id + "\t" + login + "\t" + nomecompleto + "\t" + email + "\t" + senha);
  105. }
  106.  
  107. st.close();
  108.  
  109. } catch (SQLException ex) {
  110. ex.printStackTrace();
  111. }
  112. }
  113. }
Add Comment
Please, Sign In to add comment