|
1 | 1 | package com.example;
|
2 | 2 |
|
| 3 | +import java.sql.Connection; |
3 | 4 | import java.sql.DriverManager;
|
4 | 5 | import java.sql.SQLException;
|
5 |
| -import java.sql.Statement; |
6 | 6 |
|
7 | 7 | public class App {
|
8 |
| - public static void main(String[] args){ |
9 |
| - System.out.println(); |
10 |
| - System.out.println("Aplicação Java de Exemplo\n"); |
11 |
| - listarEstados(); |
| 8 | + private static final String PASSWORD = ""; |
| 9 | + private static final String USERNAME = "gitpod"; |
| 10 | + private static final String JDBC_URL = "jdbc:postgresql://localhost/postgres"; |
| 11 | + |
| 12 | + public static void main(String[] args) { |
| 13 | + new App(); |
| 14 | + } |
| 15 | + |
| 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 | + } |
12 | 25 | }
|
13 | 26 |
|
14 |
| - public static void listarEstados() { |
15 |
| - System.out.println("Listando estados cadastrados no banco de dados"); |
| 27 | + private void listarDadosTabela(Connection conn, String tabela) { |
| 28 | + var sql = "select * from " + tabela; |
| 29 | + //System.out.println(sql); |
16 | 30 | try {
|
17 |
| - Class.forName("org.postgresql.Driver"); |
18 |
| - } catch (ClassNotFoundException e) { |
19 |
| - System.err.println("Não foi possível carregar a biblioteca para acesso ao banco de dados: " + e.getMessage()); |
| 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()); |
20 | 68 | }
|
| 69 | + |
| 70 | + } |
21 | 71 |
|
22 |
| - Statement statement = null; |
23 |
| - try(var conn = DriverManager.getConnection("jdbc:postgresql://localhost/postgres", "gitpod", "")){ |
| 72 | + private void listarEstados(Connection conn) { |
| 73 | + try{ |
24 | 74 | System.out.println("Conexão com o banco realizada com sucesso.");
|
25 | 75 |
|
26 |
| - statement = conn.createStatement(); |
| 76 | + var statement = conn.createStatement(); |
27 | 77 | var result = statement.executeQuery("select * from estado");
|
28 | 78 | while(result.next()){
|
29 | 79 | System.out.printf("Id: %d Nome: %s UF: %s\n", result.getInt("id"), result.getString("nome"), result.getString("uf"));
|
30 | 80 | }
|
| 81 | + System.out.println(); |
31 | 82 | } catch (SQLException e) {
|
32 |
| - if(statement == null) |
33 |
| - System.err.println("Não foi possível conectar ao banco de dados: " + e.getMessage()); |
34 |
| - else System.err.println("Não foi possível executar a consulta ao banco: " + e.getMessage()); |
35 |
| - } |
| 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 | + } |
36 | 97 | }
|
37 | 98 | }
|
0 commit comments