Skip to content

Commit 6d25a45

Browse files
aplicação scanner
1 parent c1ad5a7 commit 6d25a45

File tree

1 file changed

+122
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)