├── .classpath
├── .gitignore
├── .project
├── README.md
├── db.properties
├── lib
└── postgresql-42.2.18.jar
└── src
├── app
└── Program.java
└── db
├── DB.java
└── DbException.java
/.classpath:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | # Compiled class file
2 | *.class
3 |
4 | # Log file
5 | *.log
6 |
7 | # BlueJ files
8 | *.ctxt
9 |
10 | # Mobile Tools for Java (J2ME)
11 | .mtj.tmp/
12 |
13 | # Package Files #
14 | *.war
15 | *.nar
16 | *.ear
17 | *.zip
18 | *.tar.gz
19 | *.rar
20 |
21 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
22 | hs_err_pid*
23 |
24 | # Eclipse
25 |
26 | .settings
27 | /bin
28 | /target
29 |
--------------------------------------------------------------------------------
/.project:
--------------------------------------------------------------------------------
1 |
2 |
3 | jdbc-postgres
4 |
5 |
6 |
7 |
8 |
9 | org.eclipse.jdt.core.javabuilder
10 |
11 |
12 |
13 |
14 |
15 | org.eclipse.jdt.core.javanature
16 |
17 |
18 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Revisão de OO e SQL
2 |
3 | ## Nesta aula vamos revisar
4 | - Chave primária, chave estrangeira
5 | - DDL (create table, alter table)
6 | - SQL
7 | - INSERT
8 | - SELECT
9 | - INNER JOIN
10 | - Classes e objetos
11 | - Encapsulamento, get/set
12 | - Tipos enumerados
13 | - Composição de objetos
14 | - Coleções (list, map)
15 | - Acessar dados em BD relacional e instanciar objetos correspondentes
16 |
17 | ## Pré-requisitos
18 | - Git
19 | - JDK
20 | - STS (ou outra IDE)
21 | - Servidor Postgres
22 | - pgAdmin
23 |
24 | [Guia de instalação para Windows](https://github.com/devsuperior/sds1/tree/master/ferramentas/windows)
25 |
26 | [Guia de instalação para Linux](https://github.com/devsuperior/sds1/tree/master/ferramentas/linux)
27 |
28 | [Guia de instalação para Mac](https://github.com/devsuperior/sds1/tree/master/ferramentas/mac)
29 |
30 |
31 | ## Criação e instanciação da base de dados
32 | ```sql
33 | create table tb_order (
34 | id int8 generated by default as identity,
35 | latitude float8,
36 | longitude float8,
37 | moment TIMESTAMP WITHOUT TIME ZONE,
38 | status int4,
39 | primary key (id)
40 | );
41 |
42 | create table tb_order_product (
43 | order_id int8 not null,
44 | product_id int8 not null,
45 | primary key (order_id, product_id)
46 | );
47 |
48 | create table tb_product (
49 | id int8 generated by default as identity,
50 | description TEXT,
51 | image_uri varchar(255),
52 | name varchar(255),
53 | price float8,
54 | primary key (id)
55 | );
56 |
57 | alter table if exists tb_order_product add constraint fk_tb_order_product_tb_product
58 | foreign key (product_id) references tb_product;
59 |
60 | alter table if exists tb_order_product add constraint fk_tb_order_product_tb_order
61 | foreign key (order_id) references tb_order;
62 |
63 | INSERT INTO tb_product (name, price, image_Uri, description) VALUES
64 | ('Pizza de Calabresa', 50.0, 'https://github.com/devsuperior/1.png', 'Pizza calabresa com queijo, molho e massa especial'),
65 | ('Pizza Quatro Queijos', 40.0, 'https://github.com/devsuperior/2.png', 'Pizza quatro queijos muito boa'),
66 | ('Pizza de Escarola', 60.0, 'https://github.com/devsuperior/3.png', 'Pizza escarola muito boa');
67 |
68 | INSERT INTO tb_order (status, latitude, longitude, moment) VALUES
69 | (0, 213123, 12323, TIMESTAMP WITH TIME ZONE '2021-01-04T11:00:00Z'),
70 | (1, 3453453, 3534534, TIMESTAMP WITH TIME ZONE '2021-01-05T11:00:00Z');
71 |
72 | INSERT INTO tb_order_product (order_id, product_id) VALUES
73 | (1 , 1),
74 | (1 , 2),
75 | (2 , 2),
76 | (2 , 3);
77 | ```
78 |
79 | ## Consulta para recuperar os pedidos com seus produtos
80 | ```sql
81 | SELECT * FROM tb_order
82 | INNER JOIN tb_order_product ON tb_order.id = tb_order_product.order_id
83 | INNER JOIN tb_product ON tb_product.id = tb_order_product.product_id
84 | ```
85 |
--------------------------------------------------------------------------------
/db.properties:
--------------------------------------------------------------------------------
1 | user=postgres
2 | password=1234567
3 | dburl=jdbc:postgresql://localhost:5432/dsdeliver
4 | useSSL=false
5 |
--------------------------------------------------------------------------------
/lib/postgresql-42.2.18.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/devsuperior/jdbc-postgres/58862ed2c8ad32b780a0ca3c71dce067deea183e/lib/postgresql-42.2.18.jar
--------------------------------------------------------------------------------
/src/app/Program.java:
--------------------------------------------------------------------------------
1 | package app;
2 |
3 | import java.sql.Connection;
4 | import java.sql.ResultSet;
5 | import java.sql.SQLException;
6 | import java.sql.Statement;
7 |
8 | import db.DB;
9 |
10 | public class Program {
11 |
12 | public static void main(String[] args) throws SQLException {
13 |
14 | Connection conn = DB.getConnection();
15 |
16 | Statement st = conn.createStatement();
17 |
18 | ResultSet rs = st.executeQuery("select * from tb_product");
19 |
20 | while (rs.next()) {
21 | System.out.println(rs.getLong("Id") + ", " + rs.getString("Name"));
22 | }
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/src/db/DB.java:
--------------------------------------------------------------------------------
1 | package db;
2 |
3 | import java.io.FileInputStream;
4 | import java.io.IOException;
5 | import java.sql.Connection;
6 | import java.sql.DriverManager;
7 | import java.sql.SQLException;
8 | import java.util.Properties;
9 |
10 | public class DB {
11 |
12 | private static Connection conn = null;
13 |
14 | public static Connection getConnection() {
15 | if (conn == null) {
16 | try {
17 | Properties props = loadProperties();
18 | String url = props.getProperty("dburl");
19 | conn = DriverManager.getConnection(url, props);
20 | }
21 | catch (SQLException e) {
22 | throw new DbException(e.getMessage());
23 | }
24 | }
25 | return conn;
26 | }
27 |
28 | public static void closeConnection() {
29 | if (conn != null) {
30 | try {
31 | conn.close();
32 | } catch (SQLException e) {
33 | throw new DbException(e.getMessage());
34 | }
35 | }
36 | }
37 |
38 | private static Properties loadProperties() {
39 | try (FileInputStream fs = new FileInputStream("db.properties")) {
40 | Properties props = new Properties();
41 | props.load(fs);
42 | return props;
43 | }
44 | catch (IOException e) {
45 | throw new DbException(e.getMessage());
46 | }
47 | }
48 | }
49 |
--------------------------------------------------------------------------------
/src/db/DbException.java:
--------------------------------------------------------------------------------
1 | package db;
2 |
3 | public class DbException extends RuntimeException {
4 | private static final long serialVersionUID = 1L;
5 |
6 | public DbException(String msg) {
7 | super(msg);
8 | }
9 | }
10 |
--------------------------------------------------------------------------------