From 46a8ab01d61275d3ddccc2f9cb92c3eab61f3d6f Mon Sep 17 00:00:00 2001
From: William Borelli <williamborellif@gmail.com>
Date: Thu, 24 Aug 2023 15:46:09 +0000
Subject: [PATCH 1/2] =?UTF-8?q?Atualiza=20t=C3=ADtulo=20do=20README?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 README.md | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/README.md b/README.md
index 19a888a9..cac6430f 100644
--- a/README.md
+++ b/README.md
@@ -1,4 +1,4 @@
-## Postgres Playground
+## Postgres e Java Playground
 
 [![](https://gitpod.io/button/open-in-gitpod.svg)](https://gitpod.io/#https://github.com/manoelcampos/java-postgres-playground)
 

From b473540909b8f6f814f0eb617dca8ed741110a12 Mon Sep 17 00:00:00 2001
From: William Borelli <williamborellif@gmail.com>
Date: Fri, 25 Aug 2023 22:55:41 +0000
Subject: [PATCH 2/2] Trabalho

---
 java-postgres-playground             |  1 +
 src/main/java/com/example/AppBd.java | 87 ++++++++++++++++++++++++++++
 2 files changed, 88 insertions(+)
 create mode 160000 java-postgres-playground
 create mode 100644 src/main/java/com/example/AppBd.java

diff --git a/java-postgres-playground b/java-postgres-playground
new file mode 160000
index 00000000..46a8ab01
--- /dev/null
+++ b/java-postgres-playground
@@ -0,0 +1 @@
+Subproject commit 46a8ab01d61275d3ddccc2f9cb92c3eab61f3d6f
diff --git a/src/main/java/com/example/AppBd.java b/src/main/java/com/example/AppBd.java
new file mode 100644
index 00000000..ecc0151f
--- /dev/null
+++ b/src/main/java/com/example/AppBd.java
@@ -0,0 +1,87 @@
+package com.example;
+import java.sql.Connection;
+import java.sql.DriverManager;
+import java.sql.SQLException;
+import java.sql.Statement;
+
+public class AppBd {
+    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 AppBd();
+    }
+ 
+    public AppBd(){
+        try(var conn = getConnection()){  
+            listarEstados();
+            carregarDriverJDBC(conn);
+            localizarEstado(conn, "SP");
+            listarDadosTabela(conn,"cliente");
+        }catch (SQLException e) {
+            System.err.println("Não foi possível conectar ao Bando 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);
+        while(result.next()){
+            int cols = result.getMetaData().getColumnCount();
+            for (int i = 1; i <= cols; i++) {
+                System.out.printf("%s | ", result.getString(i));
+            }
+            System.out.println();
+        }
+    } catch (SQLException e) {
+        System.out.println("Erro na execução da consulta");
+    }
+}
+
+    private void localizarEstado(Connection conn, String uf) {
+        try{
+            var sql ="SELECT * FROM estado WHERE uf = ?";
+            var statement = conn.prepareStatement(sql);
+            //var sql ="SELECT * FROM estado WHERE uf = '"+ uf + "'"; //Sucetível a SQL Injection
+
+            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"));
+           }
+        }catch(SQLException e){
+            System.err.println("Erro ao executar consulta SQL" + e.getMessage());
+        }
+        
+    }
+
+    private void listarEstados() {
+        try(var conn = getConnection()){ 
+        System.out.println("Conexão 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"));
+            }
+        }  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(Connection conn) {
+        try {
+            Class.forName("org.postgresql.Driver");
+        } catch(ClassNotFoundException e) {
+            System.err.println("Não foi possível carregar a biblioteca para acesso ao banco de dados"+ e.getMessage());
+        }
+    }
+}
