franta-hg@23
|
1 |
package cz.frantovo.nekurak.dao;
|
franta-hg@23
|
2 |
|
franta-hg@23
|
3 |
import cz.frantovo.nekurak.dto.Podnik;
|
franta-hg@23
|
4 |
import java.sql.Connection;
|
franta-hg@23
|
5 |
import java.sql.PreparedStatement;
|
franta-hg@23
|
6 |
import java.sql.ResultSet;
|
franta-hg@23
|
7 |
import java.util.ArrayList;
|
franta-hg@23
|
8 |
import java.util.Collection;
|
franta-hg@23
|
9 |
import java.util.logging.Level;
|
franta-hg@23
|
10 |
|
franta-hg@23
|
11 |
/**
|
franta-hg@23
|
12 |
* Stará se o načítání a ukládání podniků (hospod) z a do databáze.
|
franta-hg@23
|
13 |
* @author fiki
|
franta-hg@23
|
14 |
*/
|
franta-hg@23
|
15 |
public class PodnikDAO extends NekurakSuperDAO {
|
franta-hg@23
|
16 |
|
franta-hg@23
|
17 |
private enum SQL {
|
franta-hg@23
|
18 |
|
franta-hg@23
|
19 |
SELECT_VSECHNY,
|
franta-hg@23
|
20 |
INSERT,
|
franta-hg@23
|
21 |
UPDATE,
|
franta-hg@23
|
22 |
NEXTVAL
|
franta-hg@23
|
23 |
}
|
franta-hg@23
|
24 |
|
franta-hg@23
|
25 |
public Collection<Podnik> getPodniky() {
|
franta-hg@23
|
26 |
Connection db = getSpojeni();
|
franta-hg@23
|
27 |
PreparedStatement ps = null;
|
franta-hg@23
|
28 |
ResultSet rs = null;
|
franta-hg@23
|
29 |
try {
|
franta-hg@23
|
30 |
ps = db.prepareStatement(getSQL(SQL.SELECT_VSECHNY));
|
franta-hg@23
|
31 |
rs = ps.executeQuery();
|
franta-hg@23
|
32 |
Collection<Podnik> vysledek = new ArrayList<Podnik>();
|
franta-hg@23
|
33 |
|
franta-hg@23
|
34 |
while (rs.next()) {
|
franta-hg@23
|
35 |
vysledek.add(new Podnik(rs.getInt("id"), rs.getString("nazev")));
|
franta-hg@23
|
36 |
}
|
franta-hg@23
|
37 |
|
franta-hg@23
|
38 |
return vysledek;
|
franta-hg@23
|
39 |
} catch (Exception e) {
|
franta-hg@23
|
40 |
log.log(Level.SEVERE, "Chyba při získávání podniků.", e);
|
franta-hg@23
|
41 |
return null;
|
franta-hg@23
|
42 |
} finally {
|
franta-hg@23
|
43 |
zavri(db, ps, rs);
|
franta-hg@23
|
44 |
}
|
franta-hg@23
|
45 |
}
|
franta-hg@23
|
46 |
|
franta-hg@23
|
47 |
/**
|
franta-hg@23
|
48 |
* Uloží podnik do databáze.
|
franta-hg@23
|
49 |
* @param p Podnik k uložení. Pokud má být vložen nový, má id = 0.
|
franta-hg@23
|
50 |
* @return id vloženého nebo uloženého podniku.
|
franta-hg@23
|
51 |
*/
|
franta-hg@23
|
52 |
public int ulozPodnik(Podnik p) {
|
franta-hg@23
|
53 |
Connection db = getSpojeni();
|
franta-hg@23
|
54 |
PreparedStatement psUpdate = null;
|
franta-hg@23
|
55 |
PreparedStatement psId = null;
|
franta-hg@23
|
56 |
ResultSet rsId = null;
|
franta-hg@23
|
57 |
try {
|
franta-hg@23
|
58 |
if (p.getId() == 0) {
|
franta-hg@23
|
59 |
psId = db.prepareStatement(getSQL(SQL.NEXTVAL));
|
franta-hg@23
|
60 |
rsId = psId.executeQuery();
|
franta-hg@23
|
61 |
rsId.next();
|
franta-hg@23
|
62 |
p.setId(rsId.getInt(1));
|
franta-hg@23
|
63 |
|
franta-hg@23
|
64 |
psUpdate = db.prepareStatement(getSQL(SQL.INSERT));
|
franta-hg@23
|
65 |
} else {
|
franta-hg@23
|
66 |
psUpdate = db.prepareStatement(getSQL(SQL.UPDATE));
|
franta-hg@23
|
67 |
}
|
franta-hg@23
|
68 |
|
franta-hg@23
|
69 |
psUpdate.setString(1, p.getNazev());
|
franta-hg@23
|
70 |
psUpdate.setInt(2, p.getId());
|
franta-hg@23
|
71 |
|
franta-hg@23
|
72 |
psUpdate.executeUpdate();
|
franta-hg@23
|
73 |
|
franta-hg@23
|
74 |
db.commit();
|
franta-hg@23
|
75 |
return p.getId();
|
franta-hg@23
|
76 |
} catch (Exception e) {
|
franta-hg@23
|
77 |
rollback(db);
|
franta-hg@23
|
78 |
log.log(Level.SEVERE, "Chyba při ukládání podniku.", e);
|
franta-hg@23
|
79 |
return -1;
|
franta-hg@23
|
80 |
} finally {
|
franta-hg@23
|
81 |
zavri(null, psId, rsId);
|
franta-hg@23
|
82 |
zavri(db, psUpdate, null);
|
franta-hg@23
|
83 |
}
|
franta-hg@23
|
84 |
}
|
franta-hg@23
|
85 |
}
|