-
Notifications
You must be signed in to change notification settings - Fork 0
/
AcademiaBD.java
121 lines (109 loc) · 4.29 KB
/
AcademiaBD.java
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
package br.com.raphsousa.academia.modelo.bd;
import br.com.raphsousa.academia.Base;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class AcademiaBD {
public static void inicializar() {
String sql;
Connection con = conectar();
sql = "Create database if not exists academia";
execute(sql, true);
sql = "Use academia";
execute(sql, true);
sql = "Drop table if exists treino";
execute(sql, true);
sql = "Drop table if exists aluno";
execute(sql, true);
sql = "Drop table if exists professor";
execute(sql, true);
sql = "Create table if not exists aluno ("
+ "matricula int not null auto_increment primary key,\n"
+ "nome varchar(50) not null,\n"
+ "dataNasc date not null,\n"
+ "genero varchar(20) not null)";
execute(sql, true);
sql = "Create table if not exists professor ("
+ "id int not null auto_increment primary key,\n"
+ "nome varchar(50) not null)";
execute(sql, true);
sql = "create table if not exists treino ("
+ "id int not null auto_increment primary key,\n"
+ "matriculaAluno int not null,\n"
+ "idProfessor int not null,\n"
+ "titulo varchar(40) not null,\n"
+ "descricao varchar(250) not null,\n"
+ "foreign key (matriculaAluno) references aluno(matricula),\n"
+ "foreign key (idProfessor) references professor(id))";
execute(sql, true);
}
public static void popular() {
String sql;
Connection con = conectar();
// insers aluno
sql = "insert into aluno(nome, dataNasc, genero)"
+ " values('Raphael Sousa', '2002/01/09', 'Masculino')";
execute(sql, true);
sql = "insert into aluno(nome, dataNasc, genero)"
+ " values('Cid andrade', '2000/01/01', 'Masculino')";
execute(sql, true);
sql = "insert into aluno(nome, dataNasc, genero)"
+ " values('Fernanda Montenegro', '1960/10/30', 'Feminino')";
execute(sql, true);
// inserts professor
sql = "insert into professor (nome) values ('Alexandre Mattos')";
execute(sql, true);
sql = "insert into professor (nome) values ('Thiago Silva')";
execute(sql, true);
sql = "insert into professor (nome) values ('Mateus Andrade')";
execute(sql, true);
// inserts treino
sql = "insert into treino(matriculaAluno, idProfessor, titulo, descricao)"
+ " values(1, 1, 'Peito', "
+ "'Supino, Supino com alteres, Crucifixo')";
execute(sql, true);
sql = "insert into treino(matriculaAluno, idProfessor, titulo, descricao)"
+ " values(2, 3, 'Costas', "
+ "'Pulley, Remada, Remada Corda')";
execute(sql, true);
sql = "insert into treino(matriculaAluno, idProfessor, titulo, descricao)"
+ " values(3, 2, 'Biceps', "
+ "'Rosca Scott, Rosca barra W, Rosca Martelo')";
execute(sql, true);
}
public static boolean execute(String sql, boolean continuaNoErro) {
Connection con = conectar();
boolean retorno = false;
try {
con.createStatement().executeUpdate(sql);
retorno = true;
} catch (SQLException e) {
Base.mensagemDeErro("Não pode ser executado " + e.getLocalizedMessage());
if (!continuaNoErro) {
System.exit(1);
}
}
desconectar(con);
return retorno;
}
public static Connection conectar() {
Connection con = null;
final String USUARIO = "root";
final String SENHA = "";
final String URL = "jdbc:mysql://localhost/academia";
try {
con = DriverManager.getConnection(URL,
USUARIO, SENHA);
} catch (SQLException ex) {
System.out.println(ex.getLocalizedMessage());
System.exit(1);
}
return con;
}
public static void desconectar(Connection c) {
try {
c.close();
} catch (SQLException ex) {
}
}
}