Skip to content

Commit 0f2ca2a

Browse files
lista de arrays
1 parent 6d25a45 commit 0f2ca2a

File tree

2 files changed

+119
-0
lines changed

2 files changed

+119
-0
lines changed
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
package com.example;
2+
3+
import java.sql.Connection;
4+
import java.sql.DriverManager;
5+
import java.sql.SQLException;
6+
import java.util.ArrayList;
7+
import java.util.List;
8+
9+
public class AppList {
10+
private static final String PASSWORD = "";
11+
private static final String USERNAME = "gitpod";
12+
private static final String JDBC_URL = "jdbc:postgresql://localhost/postgres";
13+
14+
public static void main(String[] args) {
15+
16+
List<Cliente> listaClientes = new ArrayList<>();
17+
Cliente cliente1 = new Cliente();
18+
19+
cliente1.setNome("Manoel");
20+
cliente1.setRenda(1500);
21+
listaClientes.add(cliente1);
22+
23+
24+
Cliente cliente2 = new Cliente();
25+
26+
cliente2.setNome("Bento");
27+
cliente2.setRenda(7770);
28+
29+
listaClientes.add(cliente2);
30+
listaClientes.add(cliente2);
31+
listaClientes.add(cliente1);
32+
listaClientes.add(cliente2);
33+
listaClientes.add(cliente1);
34+
listaClientes.add(cliente2);
35+
36+
for (Cliente cliente : listaClientes) {
37+
System.out.println(cliente);
38+
}
39+
40+
41+
42+
}
43+
44+
45+
private void listarDadosTabela(Connection conn, String tabela) {
46+
var sql = "select * from " + tabela;
47+
//System.out.println(sql);
48+
try {
49+
var statement = conn.createStatement();
50+
var result = statement.executeQuery(sql);
51+
52+
var metadata = result.getMetaData();
53+
int cols = metadata.getColumnCount();
54+
55+
for (int i = 1; i <= cols; i++) {
56+
System.out.printf("%-25s | ", metadata.getColumnName(i));
57+
}
58+
System.out.println();
59+
60+
while(result.next()){
61+
for (int i = 1; i <= cols; i++) {
62+
System.out.printf("%-25s | ", result.getString(i));
63+
}
64+
System.out.println();
65+
}
66+
} catch (SQLException e) {
67+
System.err.println("Erro na execução da consulta: " + e.getMessage());
68+
}
69+
70+
}
71+
72+
private void localizarEstado(Connection conn, String uf) {
73+
try{
74+
//var sql = "select * from estado where uf = '" + uf + "'"; //suscetível a SQL Injection
75+
var sql = "select * from estado where uf = ?";
76+
var statement = conn.prepareStatement(sql);
77+
//System.out.println(sql);
78+
statement.setString(1, uf);
79+
var result = statement.executeQuery();
80+
if(result.next()){
81+
System.out.printf("Id: %d Nome: %s UF: %s\n", result.getInt("id"), result.getString("nome"), result.getString("uf"));
82+
}
83+
System.out.println();
84+
} catch(SQLException e){
85+
System.err.println("Erro ao executar consulta SQL: " + e.getMessage());
86+
}
87+
88+
}
89+
90+
private void listarEstados(Connection conn) {
91+
try{
92+
System.out.println("Conexão com o banco realizada com sucesso.");
93+
94+
var statement = conn.createStatement();
95+
var result = statement.executeQuery("select * from estado");
96+
while(result.next()){
97+
System.out.printf("Id: %d Nome: %s UF: %s\n", result.getInt("id"), result.getString("nome"), result.getString("uf"));
98+
}
99+
System.out.println();
100+
} catch (SQLException e) {
101+
System.err.println("Não foi possível executar a consulta ao banco: " + e.getMessage());
102+
}
103+
}
104+
105+
private Connection getConnection() throws SQLException {
106+
return DriverManager.getConnection(JDBC_URL, USERNAME, PASSWORD);
107+
}
108+
109+
private void carregarDriverJDBC() {
110+
try {
111+
Class.forName("org.postgresql.Driver");
112+
} catch (ClassNotFoundException e) {
113+
System.err.println("Não foi possível carregar a biblioteca para acesso ao banco de dados: " + e.getMessage());
114+
}
115+
}
116+
}

src/main/java/com/example/Cliente.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,10 @@ public void setEspecial(boolean especial) {
4747
this.especial = especial;
4848
}
4949

50+
public String toString(){
5051

52+
return "Nome :"+nome+" - Renda :"+renda;
53+
}
5154

5255

5356
}

0 commit comments

Comments
 (0)