|
| 1 | +package com.example; |
| 2 | + |
| 3 | +import java.sql.Connection; |
| 4 | +import java.sql.DriverManager; |
| 5 | +import java.sql.SQLException; |
| 6 | +import java.sql.Statement; |
| 7 | + |
| 8 | +public class AppBd { |
| 9 | + private static final String PASSWORD = ""; |
| 10 | + private static final String USERNAME = "gitpod"; |
| 11 | + private static final String JDBC_URL = "jdbc:postgresql://localhost/postgres"; |
| 12 | + private Connection conn; |
| 13 | + |
| 14 | + public static void main(String[] args) { |
| 15 | + new AppBd(); |
| 16 | + } |
| 17 | + |
| 18 | + public AppBd(){ |
| 19 | + try(var conn = getConnection()) { |
| 20 | + listarEstados(conn); |
| 21 | + System.out.println(); |
| 22 | + localizarEstado(conn,"BA"); |
| 23 | + listaDadosTabela(conn,"cliente"); |
| 24 | + System.out.println(); |
| 25 | + listaDadosTabela(conn,"produto"); |
| 26 | + } catch (SQLException e){ |
| 27 | + System.out.println("Não foi possivel conectar ao banco de dados"); |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + |
| 32 | + private void listaDadosTabela(Connection conn, String tabela) { |
| 33 | + var sql = "select * from " + tabela; |
| 34 | + System.out.println(sql); |
| 35 | + try { |
| 36 | + var statement= conn.createStatement(); |
| 37 | + var result = statement.executeQuery(sql); |
| 38 | + var metadata = result.getMetaData(); |
| 39 | + |
| 40 | + int cols = metadata.getColumnCount(); |
| 41 | + |
| 42 | + for (int i = 1; i <= cols; i++) { |
| 43 | + System.out.printf("%-25s | ", metadata.getColumnName(i)); |
| 44 | + } |
| 45 | + System.out.println(); |
| 46 | + System.out.println(); |
| 47 | + |
| 48 | + while( result.next()){ |
| 49 | + |
| 50 | + for (int i = 1; i <= cols; i++) { |
| 51 | + System.out.printf("%-25s | ", result.getString(i)); |
| 52 | + } |
| 53 | + System.out.println(); |
| 54 | + } |
| 55 | + } catch (SQLException e) { |
| 56 | + // TODO Auto-generated catch block |
| 57 | + System.err.println("erro na execução da consulta " + e.getMessage()); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + private void localizarEstado(Connection conn, String uf) { |
| 62 | + try { |
| 63 | + //var sql ="select * from estado where uf = '" + uf + "'"; não deve ser feito, |
| 64 | + var sql ="select * from estado where uf = ?"; |
| 65 | + var statement = conn.prepareStatement(sql); |
| 66 | + System.out.println(sql); |
| 67 | + statement.setString(1, uf); |
| 68 | + var result = statement.executeQuery(); |
| 69 | + if(result.next()){ |
| 70 | + System.out.printf("Id: %d Nome: %s UF: %s\n", result.getInt("id"),result.getString("nome"), result.getString("uf")); |
| 71 | + } |
| 72 | + } catch (SQLException e) { |
| 73 | + System.err.println("erro ao executar consulta SQL"); |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | + private void listarEstados(Connection conn) { |
| 78 | + try { |
| 79 | + System.out.println("Conexão Estabelecida"); |
| 80 | + |
| 81 | + var statement = conn.createStatement(); |
| 82 | + var result = statement.executeQuery("select * from estado"); |
| 83 | + while(result.next()) { |
| 84 | + System.out.printf("id: %d Nome: %s UF: %s\n", result.getInt("id"), result.getString("nome"), result.getString("uf")); |
| 85 | + } |
| 86 | + } catch (SQLException e){ |
| 87 | + System.err.println("Não foi possivel conectar ao banco de dados "); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + private Connection getConnection() throws SQLException { |
| 92 | + return DriverManager.getConnection(JDBC_URL, USERNAME, PASSWORD); |
| 93 | + } |
| 94 | + |
| 95 | + private void carregarDriveJDBC() { |
| 96 | + try { |
| 97 | + Class.forName("org.postgresql.Driver"); |
| 98 | + |
| 99 | + } catch (ClassNotFoundException e) { |
| 100 | + System.err.println("Não foi possivel carregar O BD "); |
| 101 | + } |
| 102 | + } |
| 103 | +} |
0 commit comments