Skip to content

Commit a0ae9c1

Browse files
Recomecando Aula 03
1 parent a99ac24 commit a0ae9c1

File tree

1 file changed

+21
-89
lines changed

1 file changed

+21
-89
lines changed

src/main/java/com/example/App.java

Lines changed: 21 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -1,98 +1,30 @@
11
package com.example;
22

3-
import java.sql.Connection;
43
import java.sql.DriverManager;
54
import java.sql.SQLException;
65

76
public class App {
8-
private static final String PASSWORD = "";
9-
private static final String USERNAME = "gitpod";
10-
private static final String JDBC_URL = "jdbc:postgresql://localhost/postgres";
117

12-
public static void main(String[] args) {
13-
new App();
8+
public static void main(String[] args){
9+
System.out.println();
10+
System.out.println("Aplicação Java OVO de Exemplo\n");
11+
// listarEstados();
1412
}
1513

16-
public App(){
17-
try(var conn = getConnection()){
18-
carregarDriverJDBC();
19-
listarEstados(conn);
20-
localizarEstado(conn, "PR");
21-
listarDadosTabela(conn, "produto");
22-
} catch (SQLException e) {
23-
System.err.println("Não foi possível conectar ao banco de dados: " + e.getMessage());
24-
}
25-
}
26-
27-
private void listarDadosTabela(Connection conn, String tabela) {
28-
var sql = "select * from " + tabela;
29-
//System.out.println(sql);
30-
try {
31-
var statement = conn.createStatement();
32-
var result = statement.executeQuery(sql);
33-
34-
var metadata = result.getMetaData();
35-
int cols = metadata.getColumnCount();
36-
37-
for (int i = 1; i <= cols; i++) {
38-
System.out.printf("%-25s | ", metadata.getColumnName(i));
39-
}
40-
System.out.println();
41-
42-
while(result.next()){
43-
for (int i = 1; i <= cols; i++) {
44-
System.out.printf("%-25s | ", result.getString(i));
45-
}
46-
System.out.println();
47-
}
48-
} catch (SQLException e) {
49-
System.err.println("Erro na execução da consulta: " + e.getMessage());
50-
}
51-
52-
}
53-
54-
private void localizarEstado(Connection conn, String uf) {
55-
try{
56-
//var sql = "select * from estado where uf = '" + uf + "'"; //suscetível a SQL Injection
57-
var sql = "select * from estado where uf = ?";
58-
var statement = conn.prepareStatement(sql);
59-
//System.out.println(sql);
60-
statement.setString(1, uf);
61-
var result = statement.executeQuery();
62-
if(result.next()){
63-
System.out.printf("Id: %d Nome: %s UF: %s\n", result.getInt("id"), result.getString("nome"), result.getString("uf"));
64-
}
65-
System.out.println();
66-
} catch(SQLException e){
67-
System.err.println("Erro ao executar consulta SQL: " + e.getMessage());
68-
}
69-
70-
}
71-
72-
private void listarEstados(Connection conn) {
73-
try{
74-
System.out.println("Conexão com o banco realizada com sucesso.");
75-
76-
var statement = conn.createStatement();
77-
var result = statement.executeQuery("select * from estado");
78-
while(result.next()){
79-
System.out.printf("Id: %d Nome: %s UF: %s\n", result.getInt("id"), result.getString("nome"), result.getString("uf"));
80-
}
81-
System.out.println();
82-
} catch (SQLException e) {
83-
System.err.println("Não foi possível executar a consulta ao banco: " + e.getMessage());
84-
}
85-
}
86-
87-
private Connection getConnection() throws SQLException {
88-
return DriverManager.getConnection(JDBC_URL, USERNAME, PASSWORD);
89-
}
90-
91-
private void carregarDriverJDBC() {
92-
try {
93-
Class.forName("org.postgresql.Driver");
94-
} catch (ClassNotFoundException e) {
95-
System.err.println("Não foi possível carregar a biblioteca para acesso ao banco de dados: " + e.getMessage());
96-
}
97-
}
98-
}
14+
// public static void listarEstados() {
15+
// System.out.println("Listando estados cadastrados no banco de dados");
16+
// try {
17+
// Class.forName("org.postgresql.Driver");
18+
// try(var conn = DriverManager.getConnection("jdbc:postgresql://localhost/meubanco", "postgres", "")){
19+
// var stm = conn.createStatement();
20+
// var result = stm.executeQuery("select * from estado");
21+
// while(result.next()) {
22+
// System.out.println(result.getString("nome"));
23+
// }
24+
// }
25+
// } catch (ClassNotFoundException | SQLException e) {
26+
// System.out.println("Erro: " + e.getMessage());
27+
// }
28+
// }
29+
30+
}

0 commit comments

Comments
 (0)