Skip to content

Commit 1aa6801

Browse files
committed
Refatoracao
1 parent 1adfc5c commit 1aa6801

File tree

3 files changed

+167
-14
lines changed

3 files changed

+167
-14
lines changed

join.sql

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
-- Active: 1690746463990@@127.0.0.1@5432@postgres
2+
drop table if exists palette_a;
3+
drop table if exists palette_b;
4+
CREATE TABLE palette_a (
5+
id INT PRIMARY KEY,
6+
color VARCHAR (100) NOT NULL
7+
);
8+
CREATE TABLE palette_b (
9+
id INT PRIMARY KEY,
10+
color VARCHAR (100) NOT NULL
11+
);
12+
INSERT INTO palette_a (id, color)
13+
VALUES (1, 'Red');
14+
INSERT INTO palette_a (id, color)
15+
VALUES (2, 'Green');
16+
INSERT INTO palette_a (id, color)
17+
VALUES (3, 'Blue');
18+
INSERT INTO palette_a (id, color)
19+
VALUES (4, 'Purple');
20+
21+
-- insert data for the palette_b
22+
INSERT INTO palette_b (id, color)
23+
VALUES (1, 'Green');
24+
25+
INSERT INTO palette_b (id, color)
26+
VALUES (2, 'Red');
27+
28+
INSERT INTO palette_b (id, color)
29+
VALUES (3, 'Cyan');
30+
31+
INSERT INTO palette_b (id, color)
32+
VALUES (4, 'Brown');

sefaz.sql

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
-- Active: 1690746463990@@127.0.0.1@5432@postgres
2+
drop table if exists Xvendedor;
3+
drop table if exists Xproduto;
4+
drop table if exists Xvendas;
5+
6+
CREATE TABLE Xvendedor (
7+
nome VARCHAR (100) NOT NULL
8+
);
9+
CREATE TABLE Xproduto (
10+
tipo VARCHAR (100) NOT NULL,
11+
valor FLOAT
12+
);
13+
CREATE TABLE Xvendas (
14+
nome VARCHAR (100) NOT NULL,
15+
tipo VARCHAR (100) NOT NULL,
16+
quantidade INT
17+
);
18+
19+
20+
INSERT INTO Xvendedor (nome)
21+
VALUES ('Gabriela');
22+
INSERT INTO Xvendedor (nome)
23+
VALUES ('Joao');
24+
INSERT INTO Xvendedor (nome)
25+
VALUES ('Maria');
26+
INSERT INTO Xvendedor (nome)
27+
VALUES ('Pedro');
28+
INSERT INTO Xproduto (tipo, valor)
29+
VALUES ('Cadeira',800.0);
30+
INSERT INTO Xproduto (tipo, valor)
31+
VALUES ('Mesa',1400.0);
32+
INSERT INTO Xproduto (tipo, valor)
33+
VALUES ('Sofa',2600.0);
34+
INSERT INTO Xvendas (nome,tipo, quantidade)
35+
VALUES ('Gabriela','Cadeira',8);
36+
INSERT INTO Xvendas (nome,tipo, quantidade)
37+
VALUES ('Gabriela','Mesa',2);
38+
INSERT INTO Xvendas (nome,tipo, quantidade)
39+
VALUES ('Gabriela','Sofa',3);
40+
41+
INSERT INTO Xvendas (nome,tipo, quantidade)
42+
VALUES ('Joao','Cadeira',4);
43+
INSERT INTO Xvendas (nome,tipo, quantidade)
44+
VALUES ('Joao','Mesa',1);
45+
INSERT INTO Xvendas (nome,tipo, quantidade)
46+
VALUES ('Joao','Sofa',3);
47+
48+
INSERT INTO Xvendas (nome,tipo, quantidade)
49+
VALUES ('Maria','Cadeira',2);
50+
INSERT INTO Xvendas (nome,tipo, quantidade)
51+
VALUES ('Maria','Mesa',1);
52+
53+
54+

src/main/java/com/example/AppDB.java

Lines changed: 81 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,101 @@
11
package com.example;
22

3+
import java.sql.Connection;
34
import java.sql.DriverManager;
45
import java.sql.SQLException;
5-
import java.sql.Statement;
6+
67

78
public class AppDB {
8-
public static void main(String[] args) {
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+
14+
public static void main(String[] args) {
15+
new AppDB();
16+
}
17+
18+
public AppDB() {
19+
carregarDriver();
20+
try(var conn=getConnection()){
21+
/* listarEstados(conn);
22+
System.out.println();
23+
localizarEstado(conn, "TO");*/
24+
listasDados(conn,"marca");
25+
}
26+
catch(SQLException e) {
27+
System.out.println("Nao foi possivel connectar ao banco de dados!!!!" + e.getMessage());
28+
}
29+
}
30+
31+
32+
private void carregarDriver() {
933
try {Class.forName("org.postgresql.Driver");
1034
}
1135
catch (ClassNotFoundException e) {
1236
System.err.println("Nao foi possivel carregar o DB driver!!"+e.getMessage());
1337
}
14-
Statement statement=null;
15-
try(var conn=DriverManager.getConnection("jdbc:postgresql://localhost/postgres",
16-
"gitpod","")){
17-
System.out.println("Connexo com sucesso!!!!");
18-
statement = conn.createStatement();
19-
var result = statement.executeQuery("Select * fro estado");
38+
}
39+
40+
private Connection getConnection() throws SQLException {
41+
return DriverManager.getConnection(JDBC_URL,USERNAME,PASSWORD);
42+
}
43+
private void listarEstados(Connection conn) {
44+
try{
45+
// Statement query=null;
46+
var query = conn.createStatement();
47+
var sql = "Select * from estado";
48+
var result = query.executeQuery(sql);
2049
while (result.next()) {
21-
System.out.printf("ID: %d Nome : %s UF: %s\n",result.getInt("id"),result.getString("Nome"), result.getString("uf"));
50+
System.out.printf("ID: %d Nome : %s UF: %s\n",result.getInt("id"),result.getString("Nome"), result.getString("uf"));
2251
}
2352
}
2453
catch(SQLException e){
25-
if (statement==null) {
26-
System.out.println("Nao foi possivel connectar ao banco de dados!!!!");
54+
System.out.println("Erro no SQL:"+ e.getMessage());
55+
}
56+
}
57+
58+
private void localizarEstado(Connection conn,String uf) {
59+
try{
60+
// Statement query=null;
61+
// query = conn.createStatement();
62+
// var sql="Select * from estado where uf=" + "'"+uf+"'"; //SQL injection issue
63+
var sql= "Select * from estado where uf=?";
64+
var query =conn.prepareStatement(sql);
65+
query.setString(1,uf);
66+
var result = query.executeQuery();
67+
if (result.next()) {
68+
System.out.printf("ID: %d Nome : %s UF: %s\n",result.getInt("id"),result.getString("Nome"), result.getString("uf"));
2769
}
28-
else {
29-
System.out.println("Erro no SQL:"+ e.getMessage());
70+
}
71+
catch(SQLException e){
72+
System.out.println("Erro no SQL:"+ e.getMessage());
3073
}
31-
}
3274
}
75+
76+
private void listasDados(Connection conn, String tabela) {
77+
try{
78+
// Statement query=null;
79+
var query = conn.createStatement();
80+
var sql = "Select * from " + tabela;
81+
var result = query.executeQuery(sql);
82+
int cols=result.getMetaData().getColumnCount();
83+
for (int i = 1; i < cols; i++) {
84+
System.out.printf("%-25s",result.getMetaData().getColumnName(i));
85+
}
86+
System.out.println();
87+
88+
while (result.next()) {
89+
for (int i = 1; i < cols; i++) {
90+
System.out.printf("%-25s",result.getString(i));
91+
}
92+
System.out.println();
93+
}
94+
}
95+
catch(SQLException e){
96+
System.out.println("Erro no SQL:"+ e.getMessage());
97+
}
98+
}
99+
33100
}
34101

0 commit comments

Comments
 (0)