Skip to content

Commit cf28096

Browse files
committed
adicionado appteste
1 parent 267cb61 commit cf28096

File tree

4 files changed

+148
-0
lines changed

4 files changed

+148
-0
lines changed

src/main/java/com/example/AppBd.java

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
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+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.example;
2+
public class AppOpLogicos {
3+
4+
boolean a(boolean valor){
5+
System.out.println(valor);
6+
return valor;
7+
}
8+
boolean b(boolean valor){
9+
System.out.println(valor);
10+
return valor;
11+
}
12+
boolean c(boolean valor){
13+
System.out.println(valor);
14+
return valor;
15+
}
16+
17+
AppOpLogicos(){
18+
System.out.println(a(true) || b(false) || c(true));
19+
}
20+
21+
public static void main(String[] args) {
22+
23+
new AppOpLogicos();
24+
25+
26+
}
27+
28+
29+
30+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.example;
2+
3+
public class AppTeste {
4+
public static void main(String[] args) {
5+
System.out.println("App de teste");
6+
}
7+
}

src/main/java/com/example/Aula05.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.example;
2+
3+
public class Aula05 {
4+
public static void main(String[] args) {
5+
String teste="teste";
6+
System.out.println(teste);
7+
}
8+
}

0 commit comments

Comments
 (0)