-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathContatoDao.php
52 lines (50 loc) · 2.03 KB
/
ContatoDao.php
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
<?php
require('ConnectionFactory.php');
class ContatoDao {
// a conexão com o banco de dados
private Connection $connection;
public function __construct() {
$this.connection = ConnectionFactory::getConnection();
}
public function adiciona(Contato $contato) {
String sql = "insert into contatos (nome,email,endereco,dataNascimento) values (?,?,?,?)";
try {
PreparedStatement stmt = con.prepareStatement(sql);
// seta os valores
stmt.setString(1,contato.getNome());
stmt.setString(2,contato.getEmail());
stmt.setString(3,contato.getEndereco());
stmt.setDate(4, new Date(contato.getDataNascimento().getTimeInMillis()));
// executa
stmt.execute();
stmt.close();
}catch (PDOException $e) {
throw new RuntimeException($e);
}
}
public function remove(Contato $contato) {
try {
$stmt = connection.prepareStatement("delete from contatos where id=?");
$stmt.setLong(1, $contato->getId());
$stmt.execute();
$stmt.close();
}catch (PDOException e) {
throw new RuntimeException($e);
}
}
public function altera(Contato $contato) {
$sql = "update contatos set nome=?, email=?, endereco=?, dataNascimento=? where id=?";
try {
$stmt = connection.prepareStatement(sql);
stmt.setString(1, contato.getNome());
stmt.setString(2, contato.getEmail());
stmt.setString(3, contato.getEndereco());
stmt.setDate(4, new Date(contato.getDataNascimento().getTimeInMillis()));
stmt.setLong(5, contato.getId());
stmt.execute();
stmt.close();
}catch (SQLException e) {
throw new RuntimeException(e);
}
}
}