Skip to content

Commit e1608ac

Browse files
committed
ArquivoHelloWold
1 parent 958271f commit e1608ac

File tree

1 file changed

+76
-75
lines changed

1 file changed

+76
-75
lines changed

src/main/java/com/example/App.java

Lines changed: 76 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -5,94 +5,95 @@
55
import java.sql.SQLException;
66

77
public class App {
8-
private static final String PASSWORD = "";
9-
private static final String USERNAME = "gitpod";
10-
private static final String JDBC_URL = "jdbc:postgresql://localhost/postgres";
8+
// private static final String PASSWORD = "";
9+
// private static final String USERNAME = "gitpod";
10+
// private static final String JDBC_URL = "jdbc:postgresql://localhost/postgres";
1111

1212
public static void main(String[] args) {
13-
new App();
13+
System.out.println("Hello world");
14+
// new App();
1415
}
1516

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-
}
25-
}
17+
// public App(){
18+
// try(var conn = getConnection()){
19+
// carregarDriverJDBC();
20+
// listarEstados(conn);
21+
// localizarEstado(conn, "PR");
22+
// listarDadosTabela(conn, "produto");
23+
// } catch (SQLException e) {
24+
// System.err.println("Não foi possível conectar ao banco de dados: " + e.getMessage());
25+
// }
26+
// }
2627

27-
private void listarDadosTabela(Connection conn, String tabela) {
28-
var sql = "select * from " + tabela;
29-
//System.out.println(sql);
30-
try {
31-
var statement = conn.createStatement();
32-
var result = statement.executeQuery(sql);
28+
// private void listarDadosTabela(Connection conn, String tabela) {
29+
// var sql = "select * from " + tabela;
30+
// //System.out.println(sql);
31+
// try {
32+
// var statement = conn.createStatement();
33+
// var result = statement.executeQuery(sql);
3334

34-
var metadata = result.getMetaData();
35-
int cols = metadata.getColumnCount();
35+
// var metadata = result.getMetaData();
36+
// int cols = metadata.getColumnCount();
3637

37-
for (int i = 1; i <= cols; i++) {
38-
System.out.printf("%-25s | ", metadata.getColumnName(i));
39-
}
40-
System.out.println();
38+
// for (int i = 1; i <= cols; i++) {
39+
// System.out.printf("%-25s | ", metadata.getColumnName(i));
40+
// }
41+
// System.out.println();
4142

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-
}
43+
// while(result.next()){
44+
// for (int i = 1; i <= cols; i++) {
45+
// System.out.printf("%-25s | ", result.getString(i));
46+
// }
47+
// System.out.println();
48+
// }
49+
// } catch (SQLException e) {
50+
// System.err.println("Erro na execução da consulta: " + e.getMessage());
51+
// }
5152

52-
}
53+
// }
5354

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());
68-
}
55+
// private void localizarEstado(Connection conn, String uf) {
56+
// try{
57+
// //var sql = "select * from estado where uf = '" + uf + "'"; //suscetível a SQL Injection
58+
// var sql = "select * from estado where uf = ?";
59+
// var statement = conn.prepareStatement(sql);
60+
// //System.out.println(sql);
61+
// statement.setString(1, uf);
62+
// var result = statement.executeQuery();
63+
// if(result.next()){
64+
// System.out.printf("Id: %d Nome: %s UF: %s\n", result.getInt("id"), result.getString("nome"), result.getString("uf"));
65+
// }
66+
// System.out.println();
67+
// } catch(SQLException e){
68+
// System.err.println("Erro ao executar consulta SQL: " + e.getMessage());
69+
// }
6970

70-
}
71+
// }
7172

72-
private void listarEstados(Connection conn) {
73-
try{
74-
System.out.println("Conexão com o banco realizada com sucesso.");
73+
// private void listarEstados(Connection conn) {
74+
// try{
75+
// System.out.println("Conexão com o banco realizada com sucesso.");
7576

76-
var statement = conn.createStatement();
77-
var result = statement.executeQuery("select * from estado");
78-
while(result.next()){
79-
System.out.printf("Id: %d Nome: %s UF: %s\n", result.getInt("id"), result.getString("nome"), result.getString("uf"));
80-
}
81-
System.out.println();
82-
} catch (SQLException e) {
83-
System.err.println("Não foi possível executar a consulta ao banco: " + e.getMessage());
84-
}
85-
}
77+
// var statement = conn.createStatement();
78+
// var result = statement.executeQuery("select * from estado");
79+
// while(result.next()){
80+
// System.out.printf("Id: %d Nome: %s UF: %s\n", result.getInt("id"), result.getString("nome"), result.getString("uf"));
81+
// }
82+
// System.out.println();
83+
// } catch (SQLException e) {
84+
// System.err.println("Não foi possível executar a consulta ao banco: " + e.getMessage());
85+
// }
86+
// }
8687

87-
private Connection getConnection() throws SQLException {
88-
return DriverManager.getConnection(JDBC_URL, USERNAME, PASSWORD);
89-
}
88+
// private Connection getConnection() throws SQLException {
89+
// return DriverManager.getConnection(JDBC_URL, USERNAME, PASSWORD);
90+
// }
9091

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-
}
97-
}
92+
// private void carregarDriverJDBC() {
93+
// try {
94+
// Class.forName("org.postgresql.Driver");
95+
// } catch (ClassNotFoundException e) {
96+
// System.err.println("Não foi possível carregar a biblioteca para acesso ao banco de dados: " + e.getMessage());
97+
// }
98+
// }
9899
}

0 commit comments

Comments
 (0)