Skip to content

Commit e10aaa0

Browse files
committed
App Banco de Dados com Clean Code
1 parent 8385c35 commit e10aaa0

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

src/main/java/com/example/AppBd.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,13 @@ public static void main(String[] args) {
1111
// parametros 1o= o drive:banco://endereço/nomedo banco, usuario , senha
1212
var conn = DriverManager.getConnection("jdbc:postgresql://localhost/postgres", "gitpod", "");
1313
System.out.println("COnexão com o banco realizada com sucesso!");
14+
var statement = conn.createStatement();
15+
var result = statement.executeQuery(" select * from estado");
16+
while(result.next()){
17+
System.out.printf("Id: %d Nome: %s UF: %s \n ", result.getInt("id"), result.getString("nome"), result.getString("uf"));
18+
19+
}
20+
1421
} catch (ClassNotFoundException e) {
1522
// TODO Auto-generated catch block
1623
//e.printStackTrace();
@@ -19,6 +26,8 @@ public static void main(String[] args) {
1926
// TODO Auto-generated catch block
2027
// e.printStackTrace();
2128
System.err.println("Não foi possível conectar ao banco de dados: " + e.getMessage());
29+
} finally {
30+
2231
}
2332

2433
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.example;
2+
3+
import java.sql.DriverManager;
4+
import java.sql.SQLException;
5+
import java.sql.Statement;
6+
7+
public class AppBdClean {
8+
public static void main(String[] args) {
9+
try {
10+
Class.forName("org.postgresql.Driver");
11+
} catch (ClassNotFoundException e) {
12+
System.err.println("Não foi possível carregar a biblioteca para acesso ao banco de dados" + e.getMessage());
13+
}
14+
15+
Statement statement = null;
16+
try (var conn = DriverManager.getConnection("jdbc:postgresql://localhost/postgres", "gitpod", "")){
17+
// parametros 1o= o drive:banco://endereço/nomedo banco, usuario , senha
18+
System.out.println("COnexão com o banco realizada com sucesso!");
19+
20+
statement = conn.createStatement();
21+
var result = statement.executeQuery(" select * from estado");
22+
while(result.next()){
23+
System.out.printf("Id: %d Nome: %s UF: %s \n ", result.getInt("id"), result.getString("nome"), result.getString("uf"));
24+
}
25+
} catch (SQLException e) {
26+
if (statement == null)
27+
System.err.println("Não foi possível conectar ao banco de dados: " + e.getMessage());
28+
else System.err.println("Não foi possível executar a consulta no banco de dados: " + e.getMessage());
29+
}
30+
}
31+
}

0 commit comments

Comments
 (0)