Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.sql.Connection;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.util.ArrayList;
- /**
- *
- * @author Professor Hercio Neto
- */
- public class Contatos {
- private int idContato;
- private String nome, telefone, email;
- public int getIdContato() {
- return idContato;
- }
- public void setIdContato(int idContato) {
- this.idContato = idContato;
- }
- public String getNome() {
- return nome;
- }
- public void setNome(String nome) {
- this.nome = nome;
- }
- public String getTelefone() {
- return telefone;
- }
- public void setTelefone(String telefone) {
- this.telefone = telefone;
- }
- public String getEmail() {
- return email;
- }
- public void setEmail(String email) {
- this.email = email;
- }
- public void listarUnico(int idContato) {
- Conexao conexao = new Conexao();
- Connection conn = conexao.conectar();
- try {
- String sql = "Select * from contatos where idContato = "+ idContato;
- Statement stmt = conn.createStatement();
- ResultSet rs = stmt.executeQuery(sql);
- while (rs.next()) {
- this.idContato = rs.getInt("idContato");
- this.nome = rs.getString("nome");
- this.telefone = rs.getString("telefone");
- this.email = rs.getString("email");
- //System.out.println("Código: " + codigoCategoria + "\ncategoria: " + categoria + "\nPosição: " + posicao + "\n ------\n");
- }
- stmt.close();
- conexao.desconectar(conn);
- } catch (SQLException ex) {
- System.out.println("Erro " + ex);
- }
- }
- public ArrayList listar() {
- Conexao conexao = new Conexao();
- Connection conn = conexao.conectar();
- try {
- String sql = "Select * from contatos";
- Statement stmt = conn.createStatement();
- ResultSet rs = stmt.executeQuery(sql);
- stmt.close();
- conexao.desconectar(conn);
- ArrayList<Contatos> contatos = new ArrayList<>();
- while(rs.next())
- {
- Contatos c = new Contatos();
- c.setIdContato(rs.getInt("idContato"));
- c.setNome(rs.getString("nome"));
- c.setTelefone(rs.getString("telefone"));
- c.setEmail(rs.getString("email"));
- contatos.add(c);
- }
- return contatos;
- } catch (SQLException ex) {
- System.out.println("Erro " + ex);
- ArrayList<Contatos> contatos = new ArrayList<>();
- return contatos;
- }
- }
- public void inserir() {
- Conexao conexao = new Conexao();
- Connection conn = conexao.conectar();
- try {
- String sql = "INSERT INTO `contatos` (`idcontato`, `nome`, `telefone`,`email`) VALUES (NULL, '" + this.nome + "', '" + this.telefone +"', '" + this.email + "');";
- Statement stmt = conn.createStatement();
- ResultSet rs = stmt.executeQuery(sql);
- stmt.close();
- conexao.desconectar(conn);
- } catch (SQLException ex) {
- System.out.println("Erro " + ex);
- }
- }
- public void alterar() {
- Conexao conexao = new Conexao();
- Connection conn = conexao.conectar();
- try {
- String sql = "UPDATE `contatos` SET `nome` = '"+this.nome+"', `telefone` = '"+this.telefone+"', `email` = '"+this.email+"' WHERE `contatos`.`idcontato` = "+this.idContato;
- Statement stmt = conn.createStatement();
- ResultSet rs = stmt.executeQuery(sql);
- stmt.close();
- conexao.desconectar(conn);
- } catch (SQLException ex) {
- System.out.println("Erro " + ex);
- }
- }
- public void excluir() {
- Conexao conexao = new Conexao();
- Connection conn = conexao.conectar();
- try {
- String sql = "Delete from `contatos` WHERE `contatos`.`idcontato` = "+this.idContato;
- Statement stmt = conn.createStatement();
- ResultSet rs = stmt.executeQuery(sql);
- stmt.close();
- conexao.desconectar(conn);
- } catch (SQLException ex) {
- System.out.println("Erro " + ex);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement