Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package trabalho_final_luizeduardo;
- import java.sql.Connection;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.util.ArrayList;
- public class Pessoa {
- private int codigo;
- private String nome, sexo;
- public boolean cadastra(Connection conn){
- boolean ret = true;
- String sql = "insert into pessoa (nome, sexo) values(?,?)";
- try {
- PreparedStatement ps = conn.prepareStatement(sql);
- ps.setString(1, getNome());
- ps.setString(2, getSexo());
- ps.executeUpdate();
- } catch (Exception e) {
- e.printStackTrace();
- ret = false;
- }
- return ret;
- }
- public boolean altera(Connection conn){
- boolean ret = true;
- String sql = "update pessoa set nome = ?, sexo = ? where codigo=?";
- try {
- PreparedStatement ps = conn.prepareStatement(sql);
- ps.setString(1, getNome());
- ps.setString(2, getSexo());
- ps.setInt(3, getCodigo());
- ps.executeUpdate();
- } catch (Exception e) {
- e.printStackTrace();
- ret = false;
- }
- return ret;
- }
- public boolean exclui(Connection conn){
- boolean ret = true;
- String sql = "delete from pessoa where codigo = ?";
- try {
- PreparedStatement ps = conn.prepareStatement(sql);
- ps.setInt(1, getCodigo());
- ps.executeUpdate();
- } catch (Exception e) {
- e.printStackTrace();
- ret = false;
- }
- return ret;
- }
- public static ArrayList<Pessoa> listaTodos(Connection conn, String filtro){
- ArrayList<Pessoa> lista = new ArrayList<Pessoa>();
- try {
- String sql = "select * from pessoa order by nome";
- if(filtro != null)
- sql = "select * from pessoa where nome like '"+filtro+"' order by nome";
- PreparedStatement ps = conn.prepareStatement(sql);
- ResultSet rs = ps.executeQuery();
- while(rs.next()){
- Pessoa p = new Pessoa();
- p.setCodigo(rs.getInt("codigo"));
- p.setNome(rs.getString("nome"));
- p.setSexo(rs.getString("sexo"));
- lista.add(p);
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- return lista;
- }
- public String[] toArray(){
- return new String[]{getCodigo()+" ", getNome(), getSexo()};
- }
- public int getCodigo() {
- return codigo;
- }
- public void setCodigo(int codigo) {
- this.codigo = codigo;
- }
- public String getNome() {
- return nome;
- }
- public void setNome(String nome) {
- this.nome = nome;
- }
- public String getSexo() {
- return sexo;
- }
- public void setSexo(String sexo) {
- this.sexo = sexo;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement