-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBanco De Dados
219 lines (151 loc) · 7.01 KB
/
Banco De Dados
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package Model;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.JOptionPane;
public class Conexao_bd {
public String url = "jdbc:postgresql://localhost:5432/Escola";
public String usuario = "postgres";
public String senha = "root";
private Connection conexao = null;
private Statement stmt = null;
public boolean conecta (){
try {
conexao = DriverManager.getConnection(url, usuario, senha);
JOptionPane.showMessageDialog(null, "conexao com sucesso");
return true;
}
catch (SQLException e) {
JOptionPane.showMessageDialog(null, "nao deu certo a conexao");
return false;
}
}
public boolean tabela_creates(){
try {
conexao = DriverManager.getConnection(url, usuario, senha);
stmt = conexao.createStatement();
String sql = "CREATE TABLE IF NOT EXISTS ALUNO ("+
"cod_aluno serial, "+
"nome_aluno varchar NOT NULL, "+
"sobrenome varchar NOT NULL, "+
"rg_aluno varchar NOT NULL, "+
"data_nasc DATE, "+
"resultado varchar, "+
"falta_aluno integer NOT NULL, "+
"CONSTRAINT COD_ALUNO PRIMARY KEY (cod_aluno) "+
");"+
"CREATE TABLE IF NOT EXISTS PER_ALUNO("+
"cod_per serial,"+
"cod_aluno serial,"+
");"+
" create type tipo_periodo as ENUM ('B','T','S');"+
"CREATE TABLE IF NOT EXISTS PERIODO("+
"cod_per serial primary key,"+
"nota_aluno decimal(2,1) NOT NULL,"+
"tipo_per tipo_periodo NOT NULL,"+
"ordinal_per int(1) NOT NULL"+
");"+
"CREATE TABLE IF NOT EXISTS PER_DISC("+
"cod_per serial NOT NULL,"+
"cod_disc serial NOT NULL);"+
"create type tipo_usuario as ENUM ('P','D','S');"+
"CREATE TABLE IF NOT EXISTS PROFISSIONAL ("+
" cod_prof SERIAL, "+
" nome_prof varchar NOT NULL, "+
" email_prof varchar NOT NULL, "+
" senha varchar NOT NULL, "+
" tipo_usu tipo_usuario NOT NULL, "+
" cpf varchar(11) NOT NULL, "+
" CONSTRAINT cod_prof PRIMARY KEY (cod_prof) "+
");"+
"CREATE TABLE IF NOT EXISTS PROF_DISC("+
"cod_prof serial NOT NULL,"+
"cod_disc serial NOT NULL"+
");"+
"CREATE TYPE turn AS ENUM ('M','V','N');"+
" CREATE TABLE IF NOT EXISTS TURNO ("+
" cod_turno SERIAL PRIMARY KEY, "+
" descricao_turno turno NOT NULL "+
");"+
"CREATE TABLE IF NOT EXISTS TUR_DISC("+
"cod_disc serial NOT NULL"+
"cod_tur serial NOT NULL"+
");"+
" CREATE TABLE IF NOT EXISTS BLOCO ("+
" cod_blo SERIAL, "+
"descricao_blo varchar NOT NULL, "+
" CONSTRAINT COD_BLO PRIMARY KEY(cod_blo) "+
");"+
" CREATE TABLE IF NOT EXISTS SALA ("+
" cod_sala SERIAL, "+
" cod_bloc serial NOT NULL, "+
" descricao_sala varchar NOT NULL, "+
" CONSTRAINT cod_sala PRIMARY KEY(cod_sala) "+
");"+
"CREATE TABLE IF NOT EXISTS SALA_DISC("+
"cod_sala serial NOT NULL,"+
"cod_disc serial NOT NULL"+
");"+
"CREATE TABLE IF NOT EXISTS TURMA("+
"cod_turma serial primary key,"+
"turma_descricao varchar"+
");"+
"CREATE TABLE IF NOT EXISTS TURMA_DISC"+
"cod_turma serial NOT NULL,"+
"cod_disc serial NOT NULL"+
");"+
" CREATE TABLE IF NOT EXISTS DISCIPLINA ("+
" cod_disc SERIAL, "+
" nome_disc varchar, "+
" CONSTRAINT cod_disc PRIMARY KEY (cod_disc) "+
");";
stmt.executeUpdate(sql);
JOptionPane.showMessageDialog(null, "deu certo ao criar tabela");
stmt.close();
conexao.close();
return true;
} catch (SQLException e) {
JOptionPane.showMessageDialog(null, "nao deu certo ao criar tabela");
return false;
}
}
public boolean modify(){
try {
conexao = DriverManager.getConnection(url, usuario, senha);
stmt = conexao.createStatement();
String sql =
"ALTER TABLE SALA ADD CONSTRAINT cod_bloco_fk\n" +
"FOREIGN KEY (cod_bloc)\n" +
"REFERENCES bloco (COD_BLO);\n" +
"ALTER TABLE PER_ALUNO ADD foreign key (cod_aluno)references"+
"ALUNO(cod_aluno); "+
"ALTER TABLE PER_ALUNO ADD foreign key (cod_per) references"+
"PERIODO(cod_per); "+
"ALTER TABLE PROF_DISC ADD foreign key (cod_prof) references"+
"PROFISSIONAL(cod_prof);"+
"ALTER TABLE PROF_DISC ADD foreign key (cod_disc) references"+
"DISCIPLINA(cod_disc);"+
"ALTER TABLE TUR_DISC ADD foreign key (cod_disc) references"+
"DISCIPLINA(cod_disc);"+
"ALTER TABLE TUR_DISC ADD foreign key (cod_tur) references"+
"TURNO(cod_tur);"+
"ALTER TABLE SALA_DISC ADD foreign key (cod_sala) references"+
"SALA(cod_sala);"+
"ALTER TABLE SALA_DISC ADD foreign key (cod_disc) references"+
"DISCIPLINA(cod_disc);"+
"ALTER TABLE TURMA_DISC ADD foreign key (cod_turma) references"+
"TURMA(cod_turma)"+
"ALTER TABLE TURMA_DISC ADD foreign key (cod_disc) references"+
"DISCIPLINA(cod_disc)"
+"insert into profissional (cod_prof, nome_prof,email_prof, senha,tipo_usu,cpf) VALUES(1,'diretor','[email protected]','admin','D','');";
stmt.executeUpdate(sql);
stmt.close();
conexao.close();
return true;
} catch (SQLException e) {
JOptionPane.showMessageDialog(null, "nao deu certo no modify");
return false;
}
}
}