CLASSE USUARIODAO
Autor: Profº Jeferson  Leon | Publicado: 2016-11-01T16:26:00-07:00

public void inserir(Usuario obj) {
&nbsp; &nbsp; &nbsp; &nbsp; ConexaoMySQL objBanco = new ConexaoMySQL();
//os valore dos atributos

&nbsp; &nbsp; &nbsp; &nbsp; //int lcodigo = obj.getCodigo();
&nbsp; &nbsp; &nbsp; &nbsp; String lnome = obj.getNome();
&nbsp; &nbsp; &nbsp; &nbsp; String llogin = obj.getLogin();
&nbsp; &nbsp; &nbsp; &nbsp; String lsenha = obj.getSenha();
&nbsp; &nbsp; &nbsp; &nbsp; objBanco.conectar();

&nbsp; &nbsp; &nbsp; &nbsp; try {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String sql = "INSERT into usuario(id, nome,login,senha)"
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + " VALUES (null,?,?,?)";

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PreparedStatement objPst =
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; objBanco.conexao.prepareStatement(sql);
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // &nbsp;objPst.setInt(1, lcodigo);
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; objPst.setString(1, lnome);
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; objPst.setString(2, llogin);
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; objPst.setString(3, lsenha);
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; objPst.execute();
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; JOptionPane.showMessageDialog(null,lnome+ " - Cadastrado com sucesso!!");
&nbsp; &nbsp; &nbsp; &nbsp; } catch (SQLException ex) {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; JOptionPane.showMessageDialog(null, "Erro de execução no insert:" + ex.getMessage());
&nbsp; &nbsp; &nbsp; &nbsp; }
&nbsp; &nbsp; }

&nbsp; &nbsp; public void alterar(Usuario obj) {
&nbsp; &nbsp; &nbsp; ConexaoMySQL objBanco = new ConexaoMySQL();
//os valore dos atributos

&nbsp; &nbsp; &nbsp; &nbsp; int lcodigo = obj.getId();
&nbsp; &nbsp; &nbsp; &nbsp; String lnome = obj.getNome();
&nbsp; &nbsp; &nbsp; &nbsp; String llogin = obj.getLogin();
&nbsp; &nbsp; &nbsp; &nbsp; String lsenha = obj.getSenha();
&nbsp; &nbsp; &nbsp; &nbsp; objBanco.conectar();

&nbsp; &nbsp; &nbsp; &nbsp; try {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String sql = "UPDATE &nbsp;usuario SET nome = &nbsp;?, login = &nbsp;?"
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + ", senha = &nbsp;? WHERE &nbsp;id = &nbsp;?";

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PreparedStatement objPst =
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; objBanco.conexao.prepareStatement(sql);

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; objPst.setString(1, lnome);
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; objPst.setString(2, llogin);
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; objPst.setString(3, lsenha);
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; objPst.setInt(4, lcodigo);
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; objPst.execute();
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; JOptionPane.showMessageDialog(null, "Alterado com sucesso!!!");
&nbsp; &nbsp; &nbsp; &nbsp; } catch (SQLException ex) {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; JOptionPane.showMessageDialog(null,
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "Erro de execução no update:" + ex.getMessage());
&nbsp; &nbsp; &nbsp; &nbsp; }
&nbsp; &nbsp; }

&nbsp; &nbsp; public void deletar(Usuario obj) {
&nbsp; &nbsp; &nbsp; &nbsp;ConexaoMySQL objBanco = new ConexaoMySQL();
//os valore dos atributos
&nbsp; &nbsp; &nbsp; &nbsp; int lcodigo = obj.getId();
&nbsp; &nbsp; &nbsp; &nbsp; objBanco.conectar();
&nbsp; &nbsp; &nbsp; &nbsp; try {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String sql = "DELETE FROM usuario WHERE id = &nbsp;?";
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PreparedStatement objPst =
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; objBanco.conexao.prepareStatement(sql);
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; objPst.setInt(1, lcodigo);
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; objPst.execute();
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; JOptionPane.showMessageDialog(null, "Excluido com sucesso!!!");
&nbsp; &nbsp; &nbsp; &nbsp; } catch (SQLException ex) {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; JOptionPane.showMessageDialog(null,
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "Erro de execução no delete:" + ex.getMessage());
&nbsp; &nbsp; &nbsp; &nbsp; }
&nbsp; &nbsp; }
&nbsp; &nbsp; public ResultSet selecionar() {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ConexaoMySQL objBanco = new ConexaoMySQL();
//os valore dos atributos
&nbsp; &nbsp; &nbsp; &nbsp; objBanco.conectar();
&nbsp; &nbsp; &nbsp; &nbsp; try {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String sql = "SELECT usuario.* FROM usuario";
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PreparedStatement objPst =
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; objBanco.conexao.prepareStatement(sql);
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ResultSet objRst = objPst.executeQuery();
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return objRst;
&nbsp; &nbsp; &nbsp; &nbsp; } catch (SQLException ex) {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; JOptionPane.showMessageDialog(null,
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "Erro de execução no Select:" + ex.getMessage());
&nbsp; &nbsp; &nbsp; &nbsp; }
&nbsp; &nbsp; &nbsp; &nbsp; return null;
&nbsp; &nbsp; }

&nbsp; &nbsp; public ResultSet selecionarUsuario(Usuario objAcesso) {
&nbsp; &nbsp; &nbsp; &nbsp; ConexaoMySQL objBanco = new ConexaoMySQL();
//os valore dos atributos
&nbsp; &nbsp; &nbsp; &nbsp; objBanco.conectar();
&nbsp; &nbsp; &nbsp; &nbsp; try {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String sql = "SELECT usuario.* FROM usuario where login = ?";
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PreparedStatement objPst =
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; objBanco.conexao.prepareStatement(sql);
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; objPst.setString(1, objAcesso.getLogin());
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ResultSet objRst = objPst.executeQuery();
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return objRst;
&nbsp; &nbsp; &nbsp; &nbsp; } catch (SQLException ex) {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; JOptionPane.showMessageDialog(null,
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "Erro de execução no Select:" + ex.getMessage());
&nbsp; &nbsp; &nbsp; &nbsp; }
&nbsp; &nbsp; &nbsp; &nbsp; return null;
&nbsp; &nbsp; }
&nbsp;public ResultSet selecionarCodigo(String text) {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ConexaoMySQL objBanco = new ConexaoMySQL();
//os valore dos atributos
&nbsp; &nbsp; &nbsp; &nbsp; objBanco.conectar();
&nbsp; &nbsp; &nbsp; &nbsp; try {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String sql = "SELECT usuario.* FROM usuario where id = ?";
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PreparedStatement objPst =
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; objBanco.conexao.prepareStatement(sql);
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //objPst.setString(1, text);
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //objPst.setString(2, objPst.getNome());
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //objPst.setString(3, objAcesso.getLogin());
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //objPst.setString(4, objAcesso.getSenha());
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ResultSet objRst = objPst.executeQuery();
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return objRst;
&nbsp; &nbsp; &nbsp; &nbsp; } catch (SQLException ex) {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; JOptionPane.showMessageDialog(null,
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "Erro de execução no Select:" + ex.getMessage());
&nbsp; &nbsp; &nbsp; &nbsp; }
&nbsp; &nbsp; &nbsp; &nbsp; return null;
&nbsp; &nbsp; }
&nbsp; &nbsp;
&nbsp; &nbsp; public ResultSet selecionar(String text) {
&nbsp; &nbsp; &nbsp; &nbsp; ConexaoMySQL objBanco = new ConexaoMySQL();
//os valore dos atributos
&nbsp; &nbsp; &nbsp; &nbsp; objBanco.conectar();
&nbsp; &nbsp; &nbsp; &nbsp; try {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String sql = "SELECT usuario.* FROM usuario where nome like ? or login = ?";
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PreparedStatement objPst =
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; objBanco.conexao.prepareStatement(sql);
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; objPst.setString(1, "%"+text+"%");
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; objPst.setString(2, text);
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ResultSet objRst = objPst.executeQuery();
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return objRst;
&nbsp; &nbsp; &nbsp; &nbsp; } catch (SQLException ex) {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; JOptionPane.showMessageDialog(null,
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "Erro de execução no Select:" + ex.getMessage());
&nbsp; &nbsp; &nbsp; &nbsp; }
&nbsp; &nbsp; &nbsp; &nbsp; return null;
&nbsp; &nbsp; }