forked from manoelcampos/java-postgres-playground
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.java
More file actions
99 lines (83 loc) · 3.44 KB
/
Copy pathApp.java
File metadata and controls
99 lines (83 loc) · 3.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package com.example;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class App {
private static final String PASSWORD = "";
private static final String USERNAME = "gitpod";
private static final String JDBC_URL = "jdbc:postgresql://localhost/postgres";
public static void main(String[] args) {
new App();
}
public App(){
try(var conn = getConnection()){
carregarDriverJDBC();
listarEstados(conn);
localizarEstado(conn, "PR");
listarDadosTabela(conn, "produto");
} catch (SQLException e) {
System.err.println("Não foi possível conectar ao banco de dados: " + e.getMessage());
}
}
private void listarDadosTabela(Connection conn, String tabela) {
var sql = "select * from " + tabela;
//System.out.println(sql);
try {
var statement = conn.createStatement();
var result = statement.executeQuery(sql);
var metadata = result.getMetaData();
int cols = metadata.getColumnCount();
for (int i = 1; i <= cols; i++) {
System.out.printf("%-25s | ", metadata.getColumnName(i));
}
System.out.println();
while(result.next()){
for (int i = 1; i <= cols; i++) {
System.out.printf("%-25s | ", result.getString(i));
}
System.out.println();
}
} catch (SQLException e) {
System.err.println("Erro na execução da consulta: " + e.getMessage());
}
}
private void localizarEstado(Connection conn, String uf) {
try{
//var sql = "select * from estado where uf = '" + uf + "'"; //suscetível a SQL Injection
var sql = "select * from estado where uf = ?";
var statement = conn.prepareStatement(sql);
//System.out.println(sql);
statement.setString(1, uf);
var result = statement.executeQuery();
if(result.next()){
System.out.printf("Id: %d Nome: %s UF: %s\n", result.getInt("id"), result.getString("nome"), result.getString("uf"));
}
System.out.println();
} catch(SQLException e){
System.err.println("Erro ao executar consulta SQL: " + e.getMessage());
}
}
private void listarEstados(Connection conn) {
try{
System.out.println("Conexão com o banco realizada com sucesso.");
var statement = conn.createStatement();
var result = statement.executeQuery("select * from estado");
while(result.next()){
System.out.printf("Id: %d Nome: %s UF: %s\n", result.getInt("id"), result.getString("nome"), result.getString("uf"));
}
System.out.println();
} catch (SQLException e) {
System.err.println("Não foi possível executar a consulta ao banco: " + e.getMessage());
}
}
private Connection getConnection() throws SQLException {
return DriverManager.getConnection(JDBC_URL, USERNAME, PASSWORD);
}
private void carregarDriverJDBC() {
try {
Class.forName("org.postgresql.Driver");
} catch (ClassNotFoundException e) {
System.err.println("Não foi possível carregar a bibli para acesso ao banco de dados: " + e.getMessage());
}
}
}