Guest User

Untitled

a guest
Nov 23rd, 2017
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.16 KB | None | 0 0
  1. public class VideoPessoa extends javax.swing.JFrame {
  2.  
  3.  
  4. PessoaController pessoaController;
  5. Pessoa pessoa;
  6.  
  7.  
  8.  
  9.  
  10. /**
  11. * Creates new form Pessoa
  12. */
  13. public VideoPessoa() {
  14. initComponents();
  15.  
  16. new Conexao();
  17. pessoaController = new PessoaController();
  18. pessoa = new Pessoa();
  19. this.carregarPessoas();
  20. this.novaPessoa();
  21. this.habilitarCampos();
  22.  
  23. }
  24.  
  25. public boolean alterarPessoa() {
  26. pessoa.setCodigo( Integer.parseInt(this.txtCodigo.getText()));
  27. pessoa.setNome(this.txtNome.getText());
  28. pessoa.setEndereco(this.txtEndereco.getText());
  29. pessoa.setBairro(this.txtBairro.getText());
  30. pessoa.setCPF(this.txtCPF.getText());
  31. pessoa.setSexo(this.txtSexo.getText());
  32. pessoa.setUf(this.txtUF.getText());
  33. pessoa.setCelular(this.txtCelular.getText());
  34. pessoa.setTelefone(this.txtTelefone.getText());
  35. pessoa.setCidade(this.txtCidade.getText());
  36.  
  37. if (pessoaController.alterar(pessoa)) {
  38.  
  39. JOptionPane.showMessageDialog(this, "Registro alterado com sucesso!");
  40. this.desabilitarCampos();
  41. this.carregarPessoas();
  42. } else {
  43.  
  44. JOptionPane.showMessageDialog(this, "Erro ao alterar os dados!", "ERRO", JOptionPane.ERROR_MESSAGE);
  45.  
  46. }
  47.  
  48. return true;
  49.  
  50. }
  51.  
  52. public class PessoaController {
  53.  
  54. private final PessoaDAO pessoaDAO;
  55.  
  56.  
  57. public PessoaController() {
  58. pessoaDAO = new PessoaDAO();
  59.  
  60.  
  61. }
  62.  
  63. public boolean alterar( Pessoa pessoa ) {
  64. boolean retorno;
  65.  
  66.  
  67.  
  68. retorno = pessoaDAO.alterar(pessoa);
  69.  
  70. System.out.println("Pessoa: "+pessoa);
  71.  
  72. return retorno;
  73. }
  74.  
  75. public class PessoaDAO {
  76.  
  77. private Connection con;
  78.  
  79. private final String SQLINSERT = " INSERT INTO pessoa(nome, endereco, bairro, sexo, telefone, celular, CPF, uf, cidade)"
  80. + " VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?) ";
  81.  
  82. private final String SQLPESSOAPELOCODIGO = "SELECT nome, endereco, bairro, sexo, telefone, celular, CPF, uf, cidade"
  83. + " FROM pessoa"
  84. + " WHERE codigo=? ";
  85.  
  86. private final String SQLSELECT = " SELECT codigo, nome, endereco, bairro, sexo, telefone, celular, CPF, uf, cidade FROM PESSOA";
  87.  
  88. private final String SQLUPDATE = " UPDATE pessoa"
  89. + " SET nome = ?, "
  90. + " endereco = ?, "
  91. + " bairro = ?, "
  92. + " sexo = ?, "
  93. + " telefone = ?, "
  94. + " celular = ?, "
  95. + " CPF = ?, "
  96. + " uf =?, "
  97. + " cidade =? "
  98. + " WHERE codigo = ?";
  99.  
  100. private final String SQLDELETE = "DELETE FROM pessoa"
  101. + " WHERE codigo = ?";
  102.  
  103. private PreparedStatement psInsert, sqlPessoaPeloCodigo, sqlSelect, sqlUpdate, sqlDelete;
  104.  
  105. public PessoaDAO() {
  106.  
  107. con = Conexao.getConnection();
  108. try {
  109. psInsert = con.prepareStatement(SQLINSERT);
  110. sqlPessoaPeloCodigo = con.prepareStatement(SQLPESSOAPELOCODIGO);
  111. sqlSelect = con.prepareStatement(SQLSELECT);
  112. sqlUpdate = con.prepareStatement(SQLUPDATE);
  113. sqlDelete = con.prepareStatement(SQLDELETE);
  114. } catch (SQLException ex) {
  115. Logger.getLogger(PessoaDAO.class.getName()).log(Level.SEVERE, null, ex);
  116. }
  117.  
  118. }
  119.  
  120. public Pessoa getPessoaPeloCodigo(int codigo) {
  121.  
  122. Pessoa pessoa = null;
  123. try (
  124.  
  125. PreparedStatement ps = con.prepareStatement(SQLPESSOAPELOCODIGO)
  126. ) {
  127. ps.setInt(1, codigo);
  128. try (ResultSet rs = ps.executeQuery()) {
  129. if (!rs.next()) return null; // Não encontrou.
  130. // Instancia a nova pessoa.
  131. //Instancia o novo filme
  132. pessoa = new Pessoa();
  133.  
  134. //Seta as informações no filme
  135. pessoa.setCodigo(rs.getInt("codigo"));
  136. pessoa.setNome(rs.getString("nome"));
  137. pessoa.setEndereco(rs.getString("endereco"));
  138. pessoa.setBairro(rs.getString("bairro"));
  139. pessoa.setSexo(rs.getString("sexo"));
  140. pessoa.setTelefone(rs.getString("telefone"));
  141. pessoa.setCelular(rs.getString("celular"));
  142. pessoa.setCPF(rs.getString("CPF"));
  143.  
  144. pessoa.setUf(rs.getString("uf"));
  145. pessoa.setCidade(rs.getString("cidade"));
  146.  
  147. }
  148. } catch (SQLException ex) {
  149. Logger.getLogger(PessoaDAO.class.getName()).log(Level.SEVERE, null, ex);
  150. }
  151. return pessoa;
  152. }
  153.  
  154. public boolean alterar(Pessoa pessoa) {
  155. boolean retorno = false;
  156. try {
  157. sqlUpdate.setInt(1, pessoa.getCodigo());
  158. sqlUpdate.setString(2,pessoa.getNome());
  159. sqlUpdate.setString(3, pessoa.getEndereco());
  160. sqlUpdate.setString(4, pessoa.getBairro());
  161. sqlUpdate.setString(5, pessoa.getSexo());
  162. sqlUpdate.setString(6, pessoa.getTelefone());
  163. sqlUpdate.setString(7, pessoa.getCelular());
  164. sqlUpdate.setString(8, pessoa.getCPF());
  165. sqlUpdate.setString(9, pessoa.getUf());
  166. sqlUpdate.setString(10, pessoa.getCidade());
  167.  
  168.  
  169. sqlUpdate.executeUpdate();
  170.  
  171. retorno = true;
  172.  
  173. } catch (SQLException ex) {
  174. Logger.getLogger(PessoaDAO.class.getName()).log(Level.SEVERE, null, ex);
  175. }
  176. return retorno;
  177. }
  178.  
  179. public class Conexao {
  180.  
  181. private static Connection con;
  182.  
  183. public Conexao() {
  184. try {
  185. Class.forName("com.mysql.jdbc.Driver");
  186. con = DriverManager.getConnection("jdbc:mysql://localhost:3306/sistemavideolocadora2", "root", "1234");
  187.  
  188. System.out.println(" Conexão obtida!!! ");
  189.  
  190. } catch (ClassNotFoundException | SQLException ex) {
  191. Logger.getLogger(Conexao.class.getName()).log(Level.SEVERE, null, ex);
  192.  
  193. System.out.println(" Conexão estabelecida com sucesso!!! ");
  194. }
  195. }
  196.  
  197. public static Connection getConnection() {
  198. return con;
  199.  
  200.  
  201. }
  202.  
  203. public static void closeConnection() {
  204. try {
  205. con.close();
  206.  
  207. System.out.println(" Conexão fechada!!! ");
  208. } catch (SQLException ex) {
  209. Logger.getLogger(Conexao.class.getName()).log(Level.SEVERE, null, ex);
  210.  
  211. System.out.println(" Conexão finalizada com sucessso!!! ");
  212. }
  213. }
  214.  
  215.  
  216. }
  217.  
  218. Conexao obtida!
  219. out 27, 2015 3:32:41 PM dao.PessoaDAO getPessoaPeloCodigo
  220. GRAVE: null
  221. java.sql.SQLException: Column 'codigo' not found.
  222. Pessoa: Pessoa{nome=, endereco=, bairro=, sexo=, telefone=, celular=, CPF=, codigo=0, cidade=, uf=, pessoa=null}
  223. at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1074)
  224. at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:988)
  225. at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:974)
  226. at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:919)
  227. at com.mysql.jdbc.ResultSetImpl.findColumn(ResultSetImpl.java:1167)
  228. at com.mysql.jdbc.ResultSetImpl.getInt(ResultSetImpl.java:2851)
  229. at dao.PessoaDAO.getPessoaPeloCodigo(PessoaDAO.java:83)
  230. at controller.PessoaController.getPessoaPeloCodigo(PessoaController.java:72)
  231. at view.VideoPessoa.recuperarPessoas(VideoPessoa.java:461)
  232. at view.VideoPessoa.btnAlterarActionPerformed(VideoPessoa.java:334)
  233. at view.VideoPessoa.access$200(VideoPessoa.java:25)
  234. at view.VideoPessoa$3.actionPerformed(VideoPessoa.java:123)
  235. at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
  236. at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2346)
  237. at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
  238. at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
  239. at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
  240. at java.awt.Component.processMouseEvent(Component.java:6527)
  241. at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
  242. at java.awt.Component.processEvent(Component.java:6292)
  243. at java.awt.Container.processEvent(Container.java:2234)
  244. at java.awt.Component.dispatchEventImpl(Component.java:4883)
  245. at java.awt.Container.dispatchEventImpl(Container.java:2292)
  246. at java.awt.Component.dispatchEvent(Component.java:4705)
  247. at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4898)
  248. at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4533)
  249. at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4462)
  250. at java.awt.Container.dispatchEventImpl(Container.java:2278)
  251. at java.awt.Window.dispatchEventImpl(Window.java:2739)
  252. at java.awt.Component.dispatchEvent(Component.java:4705)
  253. at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:746)
  254. at java.awt.EventQueue.access$400(EventQueue.java:97)
  255. at java.awt.EventQueue$3.run(EventQueue.java:697)
  256. at java.awt.EventQueue$3.run(EventQueue.java:691)
  257. at java.security.AccessController.doPrivileged(Native Method)
  258. at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
  259. at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:86)
  260. at java.awt.EventQueue$4.run(EventQueue.java:719)
  261. at java.awt.EventQueue$4.run(EventQueue.java:717)
  262. at java.security.AccessController.doPrivileged(Native Method)
  263. at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
  264. at java.awt.EventQueue.dispatchEvent(EventQueue.java:716)
  265. at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
  266. at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
  267. at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
  268. at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
  269. at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
  270. at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
  271.  
  272. PreparedStatement ps = con.prepareStatement(SQLPESSOAPELOCODIGO)
  273. ...
  274. pessoa.setCodigo(rs.getInt("codigo"));
  275.  
  276. private final String SQLPESSOAPELOCODIGO = "SELECT nome, endereco, bairro, sexo, telefone, celular, CPF, uf, cidade"
  277. + " FROM pessoa"
  278. + " WHERE codigo=? ";
Add Comment
Please, Sign In to add comment