Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- package javaapplication3;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import javax.swing.JOptionPane;
- /**
- *
- * @author rodrigo
- */
- public class JavaApplication3 {
- private static final String DB_URL = "jdbc:mysql://localhost/devweb2";
- private static final String DB_USER = "root";
- private static final String DB_PASSWD = "root";
- public static void main(String[] args) throws GerenciadorException {
- try {
- Connection conexao = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWD);
- Statement st = conexao.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
- ResultSet rs = st.executeQuery("SELECT * FROM usuario");
- Seguranca Seguranca = new Seguranca();
- Funcoes f = new Funcoes();
- //Captura os dados do usuário e coloca em um array de string
- String msg, m1, m2, mc;
- int resposta;
- msg = JOptionPane.showInputDialog("Para cadastrar um novo usuário digite os dados precedios por"
- + " vírgula na seguinte ordem (Login, Nome Completo, E-mail, Senha,) \n "
- + "Para alterar a senha de um usuário ja existente, digite apenas o login:");
- String s[] = msg.split("\\,");
- //entra no while para Verificar se o login já existe na base de dados
- while (rs.next()) {
- try {
- //Verifica se o login já existe na base de dados
- if (rs.getString("login").equals(s[0])) {
- resposta = JOptionPane.showConfirmDialog(null, "O login \"" + s[0] + "\" já existe na base de dados!"
- + " \n Para modificar a senha digite OK, para sair clique em NO ou CANCEL.");
- if (resposta == JOptionPane.YES_OPTION) {
- mc = Seguranca.criptografar(f.validaSenha()); //usa a classe para criptografar a senha
- int id2 = f.retornaIDdeLogin(s[0]); //descobre o id da linha do login que ja existe
- rs.absolute(id2); //vai até alinha do id fornecido anteriormente
- rs.updateString("senha", mc); //realiza a modificação na senha
- rs.updateRow();
- JOptionPane.showMessageDialog(null, "Sua senha foi alterada com sucesso!");
- System.exit(0); //sai
- }
- if (resposta == JOptionPane.NO_OPTION) {
- System.exit(0); //sai
- }
- // ********** if (resposta == JOptionPane.CANCEL_OPTION){
- // ver o q fazer nestes outros botoes e ver como alterar apenas a senha na inserção
- // }
- }
- } catch (NullPointerException e) {
- e.printStackTrace();
- }
- }
- //sai do while, CASO FOI DIGITADO TODOS OS CAMPOS PARA A INSERÇÃO DE UM NOVO CADASTRO
- //Criptografa a senha
- s[3] = Seguranca.criptografar(s[3]);
- // Insere os dados na tabela
- rs.moveToInsertRow(); // moves cursor to the insert row
- rs.updateString("login", s[0]); // updates the
- rs.updateString("nomecompleto", s[1]);
- rs.updateString("email", s[2]);
- rs.updateString("senha", s[3]);
- // first column of the insert row to be AINSWORTH
- // rs.updateInt(1, 2); // updates the second column to be 35
- // rs.updateBoolean(3, true); // updates the third column to true
- rs.insertRow();
- // rs.moveToCurrentRow();
- //Mostra na tela o cabeçalho da tabela.
- System.out.println("ID\tLogin\tNome Completo\tE-mail\tsenha");
- //Mostra na tela os dados da tabela
- rs.beforeFirst();
- while (rs.next()) {
- int id = rs.getInt("id");
- String login = rs.getString("login");
- String nomecompleto = rs.getString("nomecompleto");
- String email = rs.getString("email");
- String senha = rs.getString("senha");
- System.out.println(id + "\t" + login + "\t" + nomecompleto + "\t" + email + "\t" + senha);
- }
- st.close();
- } catch (SQLException ex) {
- ex.printStackTrace();
- }
- }
- }
Add Comment
Please, Sign In to add comment