Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class VideoPessoa extends javax.swing.JFrame {
- PessoaController pessoaController;
- Pessoa pessoa;
- /**
- * Creates new form Pessoa
- */
- public VideoPessoa() {
- initComponents();
- new Conexao();
- pessoaController = new PessoaController();
- pessoa = new Pessoa();
- this.carregarPessoas();
- this.novaPessoa();
- this.habilitarCampos();
- }
- public boolean alterarPessoa() {
- pessoa.setCodigo( Integer.parseInt(this.txtCodigo.getText()));
- pessoa.setNome(this.txtNome.getText());
- pessoa.setEndereco(this.txtEndereco.getText());
- pessoa.setBairro(this.txtBairro.getText());
- pessoa.setCPF(this.txtCPF.getText());
- pessoa.setSexo(this.txtSexo.getText());
- pessoa.setUf(this.txtUF.getText());
- pessoa.setCelular(this.txtCelular.getText());
- pessoa.setTelefone(this.txtTelefone.getText());
- pessoa.setCidade(this.txtCidade.getText());
- if (pessoaController.alterar(pessoa)) {
- JOptionPane.showMessageDialog(this, "Registro alterado com sucesso!");
- this.desabilitarCampos();
- this.carregarPessoas();
- } else {
- JOptionPane.showMessageDialog(this, "Erro ao alterar os dados!", "ERRO", JOptionPane.ERROR_MESSAGE);
- }
- return true;
- }
- public class PessoaController {
- private final PessoaDAO pessoaDAO;
- public PessoaController() {
- pessoaDAO = new PessoaDAO();
- }
- public boolean alterar( Pessoa pessoa ) {
- boolean retorno;
- retorno = pessoaDAO.alterar(pessoa);
- System.out.println("Pessoa: "+pessoa);
- return retorno;
- }
- public class PessoaDAO {
- private Connection con;
- private final String SQLINSERT = " INSERT INTO pessoa(nome, endereco, bairro, sexo, telefone, celular, CPF, uf, cidade)"
- + " VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?) ";
- private final String SQLPESSOAPELOCODIGO = "SELECT nome, endereco, bairro, sexo, telefone, celular, CPF, uf, cidade"
- + " FROM pessoa"
- + " WHERE codigo=? ";
- private final String SQLSELECT = " SELECT codigo, nome, endereco, bairro, sexo, telefone, celular, CPF, uf, cidade FROM PESSOA";
- private final String SQLUPDATE = " UPDATE pessoa"
- + " SET nome = ?, "
- + " endereco = ?, "
- + " bairro = ?, "
- + " sexo = ?, "
- + " telefone = ?, "
- + " celular = ?, "
- + " CPF = ?, "
- + " uf =?, "
- + " cidade =? "
- + " WHERE codigo = ?";
- private final String SQLDELETE = "DELETE FROM pessoa"
- + " WHERE codigo = ?";
- private PreparedStatement psInsert, sqlPessoaPeloCodigo, sqlSelect, sqlUpdate, sqlDelete;
- public PessoaDAO() {
- con = Conexao.getConnection();
- try {
- psInsert = con.prepareStatement(SQLINSERT);
- sqlPessoaPeloCodigo = con.prepareStatement(SQLPESSOAPELOCODIGO);
- sqlSelect = con.prepareStatement(SQLSELECT);
- sqlUpdate = con.prepareStatement(SQLUPDATE);
- sqlDelete = con.prepareStatement(SQLDELETE);
- } catch (SQLException ex) {
- Logger.getLogger(PessoaDAO.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- public Pessoa getPessoaPeloCodigo(int codigo) {
- Pessoa pessoa = null;
- try (
- PreparedStatement ps = con.prepareStatement(SQLPESSOAPELOCODIGO)
- ) {
- ps.setInt(1, codigo);
- try (ResultSet rs = ps.executeQuery()) {
- if (!rs.next()) return null; // Não encontrou.
- // Instancia a nova pessoa.
- //Instancia o novo filme
- pessoa = new Pessoa();
- //Seta as informações no filme
- pessoa.setCodigo(rs.getInt("codigo"));
- pessoa.setNome(rs.getString("nome"));
- pessoa.setEndereco(rs.getString("endereco"));
- pessoa.setBairro(rs.getString("bairro"));
- pessoa.setSexo(rs.getString("sexo"));
- pessoa.setTelefone(rs.getString("telefone"));
- pessoa.setCelular(rs.getString("celular"));
- pessoa.setCPF(rs.getString("CPF"));
- pessoa.setUf(rs.getString("uf"));
- pessoa.setCidade(rs.getString("cidade"));
- }
- } catch (SQLException ex) {
- Logger.getLogger(PessoaDAO.class.getName()).log(Level.SEVERE, null, ex);
- }
- return pessoa;
- }
- public boolean alterar(Pessoa pessoa) {
- boolean retorno = false;
- try {
- sqlUpdate.setInt(1, pessoa.getCodigo());
- sqlUpdate.setString(2,pessoa.getNome());
- sqlUpdate.setString(3, pessoa.getEndereco());
- sqlUpdate.setString(4, pessoa.getBairro());
- sqlUpdate.setString(5, pessoa.getSexo());
- sqlUpdate.setString(6, pessoa.getTelefone());
- sqlUpdate.setString(7, pessoa.getCelular());
- sqlUpdate.setString(8, pessoa.getCPF());
- sqlUpdate.setString(9, pessoa.getUf());
- sqlUpdate.setString(10, pessoa.getCidade());
- sqlUpdate.executeUpdate();
- retorno = true;
- } catch (SQLException ex) {
- Logger.getLogger(PessoaDAO.class.getName()).log(Level.SEVERE, null, ex);
- }
- return retorno;
- }
- public class Conexao {
- private static Connection con;
- public Conexao() {
- try {
- Class.forName("com.mysql.jdbc.Driver");
- con = DriverManager.getConnection("jdbc:mysql://localhost:3306/sistemavideolocadora2", "root", "1234");
- System.out.println(" Conexão obtida!!! ");
- } catch (ClassNotFoundException | SQLException ex) {
- Logger.getLogger(Conexao.class.getName()).log(Level.SEVERE, null, ex);
- System.out.println(" Conexão estabelecida com sucesso!!! ");
- }
- }
- public static Connection getConnection() {
- return con;
- }
- public static void closeConnection() {
- try {
- con.close();
- System.out.println(" Conexão fechada!!! ");
- } catch (SQLException ex) {
- Logger.getLogger(Conexao.class.getName()).log(Level.SEVERE, null, ex);
- System.out.println(" Conexão finalizada com sucessso!!! ");
- }
- }
- }
- Conexao obtida!
- out 27, 2015 3:32:41 PM dao.PessoaDAO getPessoaPeloCodigo
- GRAVE: null
- java.sql.SQLException: Column 'codigo' not found.
- Pessoa: Pessoa{nome=, endereco=, bairro=, sexo=, telefone=, celular=, CPF=, codigo=0, cidade=, uf=, pessoa=null}
- at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1074)
- at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:988)
- at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:974)
- at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:919)
- at com.mysql.jdbc.ResultSetImpl.findColumn(ResultSetImpl.java:1167)
- at com.mysql.jdbc.ResultSetImpl.getInt(ResultSetImpl.java:2851)
- at dao.PessoaDAO.getPessoaPeloCodigo(PessoaDAO.java:83)
- at controller.PessoaController.getPessoaPeloCodigo(PessoaController.java:72)
- at view.VideoPessoa.recuperarPessoas(VideoPessoa.java:461)
- at view.VideoPessoa.btnAlterarActionPerformed(VideoPessoa.java:334)
- at view.VideoPessoa.access$200(VideoPessoa.java:25)
- at view.VideoPessoa$3.actionPerformed(VideoPessoa.java:123)
- at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
- at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2346)
- at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
- at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
- at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
- at java.awt.Component.processMouseEvent(Component.java:6527)
- at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
- at java.awt.Component.processEvent(Component.java:6292)
- at java.awt.Container.processEvent(Container.java:2234)
- at java.awt.Component.dispatchEventImpl(Component.java:4883)
- at java.awt.Container.dispatchEventImpl(Container.java:2292)
- at java.awt.Component.dispatchEvent(Component.java:4705)
- at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4898)
- at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4533)
- at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4462)
- at java.awt.Container.dispatchEventImpl(Container.java:2278)
- at java.awt.Window.dispatchEventImpl(Window.java:2739)
- at java.awt.Component.dispatchEvent(Component.java:4705)
- at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:746)
- at java.awt.EventQueue.access$400(EventQueue.java:97)
- at java.awt.EventQueue$3.run(EventQueue.java:697)
- at java.awt.EventQueue$3.run(EventQueue.java:691)
- at java.security.AccessController.doPrivileged(Native Method)
- at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
- at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:86)
- at java.awt.EventQueue$4.run(EventQueue.java:719)
- at java.awt.EventQueue$4.run(EventQueue.java:717)
- at java.security.AccessController.doPrivileged(Native Method)
- at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
- at java.awt.EventQueue.dispatchEvent(EventQueue.java:716)
- at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
- at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
- at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
- at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
- at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
- at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
- PreparedStatement ps = con.prepareStatement(SQLPESSOAPELOCODIGO)
- ...
- pessoa.setCodigo(rs.getInt("codigo"));
- private final String SQLPESSOAPELOCODIGO = "SELECT nome, endereco, bairro, sexo, telefone, celular, CPF, uf, cidade"
- + " FROM pessoa"
- + " WHERE codigo=? ";
Add Comment
Please, Sign In to add comment