Advertisement
Guest User

Codigo da Classe Pessoa

a guest
Nov 23rd, 2014
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.43 KB | None | 0 0
  1. package trabalho_final_luizeduardo;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.PreparedStatement;
  5. import java.sql.ResultSet;
  6. import java.util.ArrayList;
  7.  
  8.  
  9.  
  10. public class Pessoa {
  11.  
  12. private int codigo;
  13. private String nome, sexo;
  14.  
  15.  
  16. public boolean cadastra(Connection conn){
  17. boolean ret = true;
  18. String sql = "insert into pessoa (nome, sexo) values(?,?)";
  19. try {
  20. PreparedStatement ps = conn.prepareStatement(sql);
  21. ps.setString(1, getNome());
  22. ps.setString(2, getSexo());
  23. ps.executeUpdate();
  24. } catch (Exception e) {
  25. e.printStackTrace();
  26. ret = false;
  27. }
  28. return ret;
  29. }
  30.  
  31. public boolean altera(Connection conn){
  32. boolean ret = true;
  33. String sql = "update pessoa set nome = ?, sexo = ? where codigo=?";
  34. try {
  35. PreparedStatement ps = conn.prepareStatement(sql);
  36. ps.setString(1, getNome());
  37. ps.setString(2, getSexo());
  38. ps.setInt(3, getCodigo());
  39. ps.executeUpdate();
  40. } catch (Exception e) {
  41. e.printStackTrace();
  42. ret = false;
  43. }
  44. return ret;
  45. }
  46.  
  47. public boolean exclui(Connection conn){
  48. boolean ret = true;
  49. String sql = "delete from pessoa where codigo = ?";
  50. try {
  51. PreparedStatement ps = conn.prepareStatement(sql);
  52. ps.setInt(1, getCodigo());
  53. ps.executeUpdate();
  54. } catch (Exception e) {
  55. e.printStackTrace();
  56. ret = false;
  57. }
  58. return ret;
  59. }
  60.  
  61. public static ArrayList<Pessoa> listaTodos(Connection conn, String filtro){
  62. ArrayList<Pessoa> lista = new ArrayList<Pessoa>();
  63. try {
  64. String sql = "select * from pessoa order by nome";
  65. if(filtro != null)
  66. sql = "select * from pessoa where nome like '"+filtro+"' order by nome";
  67. PreparedStatement ps = conn.prepareStatement(sql);
  68. ResultSet rs = ps.executeQuery();
  69. while(rs.next()){
  70. Pessoa p = new Pessoa();
  71. p.setCodigo(rs.getInt("codigo"));
  72. p.setNome(rs.getString("nome"));
  73. p.setSexo(rs.getString("sexo"));
  74. lista.add(p);
  75. }
  76. } catch (Exception e) {
  77. e.printStackTrace();
  78. }
  79. return lista;
  80. }
  81.  
  82. public String[] toArray(){
  83. return new String[]{getCodigo()+" ", getNome(), getSexo()};
  84. }
  85.  
  86. public int getCodigo() {
  87. return codigo;
  88. }
  89. public void setCodigo(int codigo) {
  90. this.codigo = codigo;
  91. }
  92. public String getNome() {
  93. return nome;
  94. }
  95. public void setNome(String nome) {
  96. this.nome = nome;
  97. }
  98. public String getSexo() {
  99. return sexo;
  100. }
  101. public void setSexo(String sexo) {
  102. this.sexo = sexo;
  103. }
  104.  
  105.  
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement