1 package ejava.projects.eleague.jdbc;
2
3 import java.lang.reflect.Method;
4 import java.sql.Connection;
5 import java.sql.PreparedStatement;
6 import java.sql.ResultSet;
7 import java.sql.SQLException;
8 import java.sql.Statement;
9 import java.util.List;
10
11 import org.slf4j.Logger;
12 import org.slf4j.LoggerFactory;
13
14 import ejava.projects.eleague.dao.ClubDAO;
15 import ejava.projects.eleague.dao.ClubDAOException;
16 import ejava.projects.eleague.bo.Address;
17 import ejava.projects.eleague.bo.Venue;
18
19
20
21
22
23 public class JDBCClubDAO implements ClubDAO {
24 private static Logger logger = LoggerFactory.getLogger(JDBCClubDAO.class);
25 private Connection connection;
26
27
28
29
30
31 public void setConnection(Connection connection) {
32 this.connection = connection;
33 }
34
35 @Override
36 public void createVenue(Venue venue) {
37 PreparedStatement statement1 = null;
38 Statement statement2 = null;
39 PreparedStatement statement3 = null;
40 Statement statement4 = null;
41 ResultSet rs = null;
42
43 Address address = venue.getAddress();
44 if (address == null) {
45 throw new ClubDAOException("Venue must have address");
46 }
47
48 try {
49 statement1 = connection.prepareStatement(
50 "INSERT INTO ELEAGUE_ADDR (ID, CITY) " +
51 "VALUES (null, ?)");
52 statement1.setString(1, venue.getAddress().getCity());
53 statement1.execute();
54
55 Method setId = Address.class.getDeclaredMethod(
56 "setId", new Class[]{long.class});
57 setId.setAccessible(true);
58
59
60 statement2 = connection.createStatement();
61 rs = statement2.executeQuery("call identity()");
62 if (rs.next()) {
63 long id = rs.getLong(1);
64 setId.invoke(venue.getAddress(), id);
65 }
66 rs.close();
67
68 statement3 = connection.prepareStatement(
69 "INSERT INTO ELEAGUE_VEN (ID, NAME, ADDR_ID) " +
70 "VALUES (null, ?, ?)");
71 statement3.setString(1, venue.getName());
72 statement3.setLong(2, venue.getAddress().getId());
73 statement3.execute();
74
75 setId = Venue.class.getDeclaredMethod(
76 "setId", new Class[]{long.class});
77 setId.setAccessible(true);
78
79
80 statement4 = connection.createStatement();
81 rs = statement4.executeQuery("call identity()");
82 if (rs.next()) {
83 long id = rs.getLong(1);
84 setId.invoke(venue, id);
85 }
86 }
87 catch (SQLException ex) {
88 logger.error("SQL error creating account", ex);
89 throw new ClubDAOException("error creating account"+ex, ex);
90 }
91 catch (Exception ex) {
92 logger.error("error creating account", ex);
93 throw new ClubDAOException("error creating account"+ex, ex);
94 }
95 finally {
96 try { rs.close(); } catch (Exception ignored) {}
97 try { statement1.close(); } catch (Exception ignored) {}
98 try { statement2.close(); } catch (Exception ignored) {}
99 try { statement3.close(); } catch (Exception ignored) {}
100 try { statement4.close(); } catch (Exception ignored) {}
101 }
102 }
103
104
105
106
107 @Override
108 public List<Venue> getVenues(int index, int count)
109 throws ClubDAOException {
110 throw new ClubDAOException("not implemented", null);
111 }
112 }