Skip to content

Commit 9aaf7e8

Browse files
committed
Exercicio testado e finalizado
1 parent 851f8e1 commit 9aaf7e8

File tree

4 files changed

+107
-89
lines changed

4 files changed

+107
-89
lines changed

ConnectionFactory.java

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,21 @@
11
import java.sql.DriverManager;
22
import java.sql.Connection;
3+
34
public class ConnectionFactory {
45
private static String host = "localhost";
56
private static String port = "3306";
67
private static String db = "20221_fatec_ipi_poo_pessoas";
78
private static String user = "root";
8-
private static String password = "1234";
9+
private static String password = "12345678";
910

10-
public static Connection getConnection() throws Exception{
11+
public static Connection getConnection() throws Exception {
1112
return DriverManager.getConnection(
12-
String.format(
13-
"jdbc:mysql://%s:%s/%s?useTimezone=true&serverTimezone=UTC",
14-
host,
15-
port,
16-
db
17-
),
18-
user,
19-
password
20-
);
13+
String.format(
14+
"jdbc:mysql://%s:%s/%s?useTimezone=true&serverTimezone=UTC",
15+
host,
16+
port,
17+
db),
18+
user,
19+
password);
2120
}
22-
}
21+
}

Pessoa.java

Lines changed: 75 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,110 +1,127 @@
11
import java.sql.Connection;
22
import java.sql.PreparedStatement;
33

4-
import com.mysql.cj.x.protobuf.MysqlxPrepare.Prepare;
5-
64
public class Pessoa {
75
private int codigo;
86
private String nome;
97
private String fone;
108
private String email;
119

12-
//mapeamento objeto relacional
13-
//ORM: Hibernate, EclipseLink
14-
public void inserir() throws Exception{
15-
//1. Definir o comando SQL
10+
// mapeamento objeto relacional
11+
// ORM: Hibernate, EclipseLink
12+
public void inserir() throws Exception {
13+
// 1. Definir o comando SQL
1614
String sql = "INSERT INTO tb_pessoa (nome, fone, email) VALUES (?, ?, ?)";
17-
//2. Abrir uma conexão com o MySQL Server
18-
ConnectionFactory factory = new ConnectionFactory();
19-
Connection conexao = factory.getConnection();
20-
//3. Preparar o comando (solicitar ao MySQL Server que compile o comando SQL previamente)
15+
Connection conexao = ConnectionFactory.getConnection();
16+
// 3. Preparar o comando (solicitar ao MySQL Server que compile o comando SQL
17+
// previamente)
2118
PreparedStatement ps = conexao.prepareStatement(sql);
22-
//4. Substituir os eventuais placeholders
19+
// 4. Substituir os eventuais placeholders
2320
ps.setString(1, nome);
2421
ps.setString(2, fone);
2522
ps.setString(3, email);
26-
//5. Executar o comando
23+
// 5. Executar o comando
2724
ps.execute();
28-
//6. Fechar os recursos (conexão e o comando preparado)
25+
// 6. Fechar os recursos (conexão e o comando preparado)
2926
ps.close();
3027
conexao.close();
3128
}
3229

33-
public void atualizar() throws Exception{
34-
//1. Especificar o comando SQL (UPDATE)
30+
public void atualizar() throws Exception {
31+
// 1. Especificar o comando SQL (UPDATE)
3532
String sql = "UPDATE tb_pessoa SET nome = ?, fone = ?, email = ? WHERE cod_pessoa = ?";
36-
//2. Abrir uma conexão com o MySQL Server
37-
ConnectionFactory factory = new ConnectionFactory();
38-
//try-with-resources (desde a versão 7 do Java SE)
39-
try(Connection conexao = factory.getConnection();
40-
//3. Preparar o comando
41-
PreparedStatement ps = conexao.prepareStatement(sql)){
42-
//4. Substituir os placeholders
33+
// try-with-resources (desde a versão 7 do Java SE)
34+
try (Connection conexao = ConnectionFactory.getConnection();
35+
// 3. Preparar o comando
36+
PreparedStatement ps = conexao.prepareStatement(sql)) {
37+
// 4. Substituir os placeholders
4338
ps.setString(1, nome);
4439
ps.setString(2, fone);
4540
ps.setString(3, email);
4641
ps.setInt(4, codigo);
47-
//5. Executar o comando
42+
// 5. Executar o comando
4843
ps.execute();
49-
//6. Fechar os recursos: já está feito pelo try-with-resources
44+
// 6. Fechar os recursos: já está feito pelo try-with-resources
5045
}
5146
}
5247

53-
public void apagar() throws Exception{
54-
//1. Especificar o comando SQL (DELETE por código)
48+
public void apagar() throws Exception {
49+
// 1. Especificar o comando SQL (DELETE por código)
5550
String sql = "DELETE FROM tb_pessoa WHERE cod_pessoa = ?";
56-
//2. Abrir uma conexão com o MySQL Server (Usando try-with-resources do Java 7)
57-
try(
58-
Connection conexao = new ConnectionFactory().getConnection();
59-
//3. Preparar o comando
60-
PreparedStatement ps = conexao.prepareStatement(sql);
61-
){
62-
//4. Substituir os placeholders
51+
new ConnectionFactory();
52+
// 2. Abrir uma conexão com o MySQL Server (Usando try-with-resources do Java 7)
53+
try (
54+
Connection conexao = ConnectionFactory.getConnection();
55+
// 3. Preparar o comando
56+
PreparedStatement ps = conexao.prepareStatement(sql);) {
57+
// 4. Substituir os placeholders
6358
ps.setInt(1, codigo);
64-
//5. Executar o comando
59+
// 5. Executar o comando
6560
ps.execute();
6661
}
6762
}
6863

69-
public static void listar() throws Exception{
70-
//1. Especificar o comando SQL (SELECT)
64+
public static String listar() throws Exception {
65+
// 1. Especificar o comando SQL (SELECT)
7166
String sql = "SELECT * FROM tb_pessoa";
72-
//2. Abrir uma conexão (usando try-with-resources)
73-
//3. Preparar o comando
74-
try(
75-
Connection conexao = ConnectionFactory.getConnection();
76-
PreparedStatement ps = conexao.prepareStatement(sql);
77-
//4. Substituir os eventuais placeholders
78-
//5. Executar o comando
79-
java.sql.ResultSet rs = ps.executeQuery();
80-
){
81-
//6. Tratar o resultado (pois ele é uma tabela)
82-
while(rs.next()){
67+
String msg = "\n";
68+
// 2. Abrir uma conexão (usando try-with-resources)
69+
// 3. Preparar o comando
70+
try (
71+
Connection conexao = ConnectionFactory.getConnection();
72+
PreparedStatement ps = conexao.prepareStatement(sql);
73+
// 4. Substituir os eventuais placeholders
74+
// 5. Executar o comando
75+
java.sql.ResultSet rs = ps.executeQuery();) {
76+
// 6. Tratar o resultado (pois ele é uma tabela)
77+
while (rs.next()) {
8378
int codigo = rs.getInt("cod_pessoa");
8479
String nome = rs.getString("nome");
8580
String fone = rs.getString("fone");
8681
String email = rs.getString("email");
87-
System.out.printf(
88-
"código: %d, nome: %s, fone: %s, e-mail: %s\n",
89-
codigo, nome, fone, email
90-
);
91-
}
9282

83+
msg += String.format("Codigo: %s \nNome: %s \nFone: %s \nEmail: %s\n\n", codigo, nome, fone, email);
84+
85+
}
86+
return msg;
9387
}
9488
}
9589

96-
public Pessoa (String nome, String fone, String email){
97-
this(0, nome, fone, email);
90+
// Buscar por cod_pessoa
91+
public static Pessoa buscar(int codigo) throws Exception {
92+
// Especificar comando SQL
93+
String sql = "SELECT * FROM tb_pessoa WHERE cod_pessoa = ?";
94+
Pessoa p = new Pessoa(null, null, null);
95+
// Abrir conexão e preparar comando
96+
try (
97+
Connection conexao = ConnectionFactory.getConnection();
98+
PreparedStatement ps = conexao.prepareStatement(sql);) {
99+
// Substituir placeholder
100+
ps.setInt(1, codigo);
101+
// Executar comando
102+
java.sql.ResultSet rs = ps.executeQuery();
103+
if (rs.next()) {
104+
p.setNome(rs.getString("nome"));
105+
p.setFone(rs.getString("fone"));
106+
p.setEmail(rs.getString("email"));
107+
System.out.println("\n" + p.getNome() + "\n" + p.getFone() + "\n" + p.getEmail() + "\n");
108+
}
109+
}
110+
return p;
111+
}
112+
113+
public Pessoa(String nome, String fone, String email) {
114+
this(0, nome, fone, email);
98115
}
99116

100-
public Pessoa(int codigo, String nome, String fone, String email){
117+
public Pessoa(int codigo, String nome, String fone, String email) {
101118
setCodigo(codigo);
102119
setNome(nome);
103120
setFone(fone);
104121
setEmail(email);
105122
}
106123

107-
public Pessoa(int codigo){
124+
public Pessoa(int codigo) {
108125
this(codigo, null, null, null);
109126
}
110127

@@ -115,6 +132,7 @@ public int getCodigo() {
115132
public String getEmail() {
116133
return email;
117134
}
135+
118136
public String getFone() {
119137
return fone;
120138
}
@@ -138,4 +156,4 @@ public void setFone(String fone) {
138156
public void setNome(String nome) {
139157
this.nome = nome;
140158
}
141-
}
159+
}

Principal.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,12 @@ public static void main(String[] args) {
1919
break;
2020
}
2121
case 2:{
22-
String nome = JOptionPane.showInputDialog("Digite o nome");
23-
String fone = JOptionPane.showInputDialog("Digite o fone");
24-
String email = JOptionPane.showInputDialog("Digite o e-mail");
25-
int codigo = parseInt(JOptionPane.showInputDialog("Digite o código"));
26-
Pessoa p = new Pessoa(codigo, nome, fone, email);
22+
int codigo = parseInt(JOptionPane.showInputDialog(Pessoa.listar() + "Digite o código:"));
23+
Pessoa pessoa = Pessoa.buscar(codigo);
24+
String nome = JOptionPane.showInputDialog( Pessoa.listar() + "Digite o nome:", pessoa.getNome());
25+
String fone = JOptionPane.showInputDialog(Pessoa.listar() + "Digite o fone:",pessoa.getFone());
26+
String email = JOptionPane.showInputDialog(Pessoa.listar() + "Digite o e-mail:",pessoa.getEmail());
27+
Pessoa p = new Pessoa(codigo,nome,fone,email);
2728
p.atualizar();
2829
JOptionPane.showMessageDialog(null, "Pessoa atualizada");
2930
break;
@@ -35,7 +36,7 @@ public static void main(String[] args) {
3536
break;
3637
}
3738
case 4:
38-
Pessoa.listar();
39+
JOptionPane.showMessageDialog(null, Pessoa.listar());
3940
break;
4041
case 0:
4142
break;

TesteConexao.java

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,29 @@
11
import java.sql.Connection;
22
import java.sql.DriverManager;
3-
public class TesteConexao{
3+
4+
public class TesteConexao {
45
public static void main(String[] args) throws Exception {
5-
//jdbc:mysql://localhost:3306/20221_fatec_ipi_poo_pessoas?useTimezone=true&serverTimezone=UTC
6+
// jdbc:mysql://localhost:3306/20221_fatec_ipi_poo_pessoas?useTimezone=true&serverTimezone=UTC
67
String host = "localhost";
78
String port = "3306";
89
String db = "20221_fatec_ipi_poo_pessoas";
910
String user = "root";
10-
String password = "1234";
11+
String password = "12345678";
1112

1213
String stringDeConexao = String.format(
13-
"jdbc:mysql://%s:%s/%s?useTimezone=true&serverTimezone=UTC",
14-
host,
15-
port,
16-
db
17-
);
18-
//cláusula catch or declare
14+
"jdbc:mysql://%s:%s/%s?useTimezone=true&serverTimezone=UTC",
15+
host,
16+
port,
17+
db);
18+
// cláusula catch or declare
1919
Connection conexao = DriverManager.getConnection(
20-
stringDeConexao,
21-
user,
22-
password
23-
);
20+
stringDeConexao,
21+
user,
22+
password);
2423
if (conexao != null)
2524
System.out.println("Conexão OK!");
2625
else
27-
System.out.println("Conexão NOK!");
26+
System.out.println("Conexão falhou!");
27+
conexao.close();
2828
}
2929
}

0 commit comments

Comments
 (0)