Skip to content

Trazendo as mundas do readme da aula-12 para aula-13 #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: Aula-12
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .DS_Store
Binary file not shown.
Binary file added bancoAgenda.db
Binary file not shown.
7 changes: 7 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@
<groupId>ifb1</groupId>
<artifactId>javaInterface</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.xerial</groupId>
<artifactId>sqlite-jdbc</artifactId>
<version>3.44.0.0</version>
</dependency>
</dependencies>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
Expand Down
Binary file added src/.DS_Store
Binary file not shown.
111 changes: 111 additions & 0 deletions src/main/java/br/edu/ifb/DAO/ContatoDAO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package br.edu.ifb.DAO;

import br.edu.ifb.classes.Contato;
import br.edu.ifb.conexao.Conexao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
*
* @author dariopintor CRUD pequeno teste
*/
public class ContatoDAO {

public ArrayList<Contato> lerBanco() {

Connection con = Conexao.getConnection();
PreparedStatement stmt = null;
ResultSet rs = null;
ArrayList<Contato> contatos = new ArrayList<>();
//teste
try {
stmt = con.prepareStatement("SELECT * FROM Contato");
rs = stmt.executeQuery();

while (rs.next()) {
Contato contato = new Contato();
contato.setId(rs.getInt("id"));
contato.setIdUsuario(rs.getInt("usuarioId"));
contato.setNome(rs.getString("nome"));
contato.setTelefone(rs.getString("telefone"));
contato.setEmail(rs.getString("email"));
contato.setDataAniversario(rs.getString("aniversario"));
contatos.add(contato);
}
} catch (SQLException ex) {
System.out.println("Não foi possível ler a tabela Contato");
} finally {
Conexao.closeConnection(con, stmt, rs);
}
return contatos;
}

public void inserirBanco(Contato c) {
Connection con = Conexao.getConnection();
PreparedStatement stmt = null;
try {
stmt = con.prepareStatement("INSERT INTO Contato (usuarioId, nome, telefone, email, aniversario) VALUES (?, ?, ?, ?, ?)");
stmt.setInt(1, c.getIdUsuario());
stmt.setString(2, c.getNome());
stmt.setString(3, c.getTelefone());
stmt.setString(4, c.getEmail());
stmt.setString(5, c.getDataAniversario());
stmt.executeUpdate();

} catch (SQLException ex) {
System.out.println("Erro ao inserir na tabela Contato");
} finally {
Conexao.closeConnection(con, stmt);
}

}

public void atualizarBanco(Contato c) {
Connection con = Conexao.getConnection();
PreparedStatement stmt = null;
try {
stmt = con.prepareStatement("UPDATE Contato SET nome = ?, telefone = ?, email = ? , aniversario = ? WHERE id = ?");

stmt.setString(1, c.getNome());
stmt.setString(2, c.getTelefone());
stmt.setString(3, c.getEmail());
stmt.setString(4, c.getDataAniversario());
stmt.setInt(5, c.getId());
stmt.executeUpdate();

} catch (SQLException ex) {
System.out.println("Erro ao atualizar na tabela Contato");
} finally {
Conexao.closeConnection(con, stmt);
}




}
public void deletarNoBanco(Contato c) {
Connection con = Conexao.getConnection();
PreparedStatement stmt = null;
try {
stmt = con.prepareStatement(" DELETE FROM Contato WHERE id = ? AND usuarioID = ?");
stmt.setInt(1,c.getId());
stmt.setInt(2,c.getIdUsuario());
stmt.executeUpdate();

} catch (SQLException ex) {
System.out.println("Erro ao deletar na tabela Contato");
} finally {
Conexao.closeConnection(con, stmt);
}

}
}
13 changes: 13 additions & 0 deletions src/main/java/br/edu/ifb/DAO/LoginDAO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package br.edu.ifb.DAO;

/**
*
* @author dariopintor
*/
public class LoginDAO {

}
13 changes: 13 additions & 0 deletions src/main/java/br/edu/ifb/DAO/RegistrarDAO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package br.edu.ifb.DAO;

/**
*
* @author dariopintor
*/
public class RegistrarDAO {

}
13 changes: 13 additions & 0 deletions src/main/java/br/edu/ifb/DAO/UsuarioDAO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package br.edu.ifb.DAO;

/**
*
* @author dariopintor
*/
public class UsuarioDAO {

}
85 changes: 85 additions & 0 deletions src/main/java/br/edu/ifb/classes/Contato.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package br.edu.ifb.classes;

/**
*
* @author dariopintor
*/
public class Contato {

private int id;
private int idUsuario;
private String nome;
private String telefone;
private String email;
private String dataAniversario;

public Contato(){

}
public Contato(int id, int idUsuario, String nome, String telefone, String email, String dataAniversario) {
this.id = id;
this.idUsuario = idUsuario;
this.nome = nome;
this.telefone = telefone;
this.email = email;
this.dataAniversario = dataAniversario;
}

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public int getIdUsuario() {
return idUsuario;
}

public void setIdUsuario(int idUsuario) {
this.idUsuario = idUsuario;
}

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 String getDataAniversario() {
return dataAniversario;
}

public void setDataAniversario(String dataAniversario) {
this.dataAniversario = dataAniversario;
}

@Override
public String toString() {
return "Contato{" + "id=" + id + ", idUsuario=" + idUsuario + ", nome=" + nome + ", telefone=" + telefone + ", email=" + email + ", dataAniversario=" + dataAniversario + '}';
}

}
13 changes: 13 additions & 0 deletions src/main/java/br/edu/ifb/classes/Login.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package br.edu.ifb.classes;

/**
*
* @author dariopintor
*/
public class Login {

}
13 changes: 13 additions & 0 deletions src/main/java/br/edu/ifb/classes/Registrar.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package br.edu.ifb.classes;

/**
*
* @author dariopintor
*/
public class Registrar {

}
13 changes: 13 additions & 0 deletions src/main/java/br/edu/ifb/classes/Usuario.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package br.edu.ifb.classes;

/**
*
* @author dariopintor
*/
public class Usuario {

}
61 changes: 61 additions & 0 deletions src/main/java/br/edu/ifb/conexao/Conexao.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package br.edu.ifb.conexao;


import br.edu.ifb.DAO.ContatoDAO;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Conexao {

private static final String URL = "jdbc:sqlite:bancoAgenda.db";
public static Connection getConnection() {

try {
return DriverManager.getConnection(URL);
} catch (SQLException ex) {
System.out.println("Banco não conectado!");
Logger.getLogger(Conexao.class.getName()).log(Level.SEVERE, null, ex);
}
return null;
}

public static void closeConnection(Connection con){
try {
con.close();
} catch (SQLException ex) {
Logger.getLogger(Conexao.class.getName()).log(Level.SEVERE, null, ex);
}
}

public static void closeConnection(Connection con, PreparedStatement stmt){
closeConnection(con);
try {

stmt.close();
} catch (SQLException ex) {
Logger.getLogger(Conexao.class.getName()).log(Level.SEVERE, null, ex);
}
}

public static void closeConnection(Connection con, PreparedStatement stmt, ResultSet rs){
closeConnection(con, stmt);
try {
rs.close();
} catch (SQLException ex) {
Logger.getLogger(Conexao.class.getName()).log(Level.SEVERE, null, ex);
}

}


public static void main(String[] args) {
ContatoDAO ver = new ContatoDAO();
ver.lerBanco();
}
}
Loading