annotate flys-aft/src/main/java/de/intevation/db/ConnectedStatements.java @ 4074:100c4e0a45e1

Improved infrastructure for prepared statements. flys-aft/trunk@3407 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Tue, 13 Dec 2011 14:32:55 +0000
parents
children ee0c60757a94
rev   line source
4074
100c4e0a45e1 Improved infrastructure for prepared statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
1 package de.intevation.db;
100c4e0a45e1 Improved infrastructure for prepared statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
2
100c4e0a45e1 Improved infrastructure for prepared statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
3 import java.util.HashMap;
100c4e0a45e1 Improved infrastructure for prepared statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
4 import java.util.Map;
100c4e0a45e1 Improved infrastructure for prepared statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
5
100c4e0a45e1 Improved infrastructure for prepared statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
6 import java.sql.Connection;
100c4e0a45e1 Improved infrastructure for prepared statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
7 import java.sql.SQLException;
100c4e0a45e1 Improved infrastructure for prepared statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
8
100c4e0a45e1 Improved infrastructure for prepared statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
9 public class ConnectedStatements
100c4e0a45e1 Improved infrastructure for prepared statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
10 {
100c4e0a45e1 Improved infrastructure for prepared statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
11 protected Connection connection;
100c4e0a45e1 Improved infrastructure for prepared statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
12
100c4e0a45e1 Improved infrastructure for prepared statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
13 protected Map<String, SymbolicStatement> statements;
100c4e0a45e1 Improved infrastructure for prepared statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
14
100c4e0a45e1 Improved infrastructure for prepared statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
15 protected Map<String, SymbolicStatement.Instance> boundStatements;
100c4e0a45e1 Improved infrastructure for prepared statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
16
100c4e0a45e1 Improved infrastructure for prepared statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
17 public ConnectedStatements(
100c4e0a45e1 Improved infrastructure for prepared statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
18 Connection connection,
100c4e0a45e1 Improved infrastructure for prepared statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
19 Map<String, SymbolicStatement> statements
100c4e0a45e1 Improved infrastructure for prepared statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
20 ) {
100c4e0a45e1 Improved infrastructure for prepared statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
21 this.connection = connection;
100c4e0a45e1 Improved infrastructure for prepared statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
22 this.statements = statements;
100c4e0a45e1 Improved infrastructure for prepared statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
23
100c4e0a45e1 Improved infrastructure for prepared statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
24 boundStatements = new HashMap<String, SymbolicStatement.Instance>();
100c4e0a45e1 Improved infrastructure for prepared statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
25 }
100c4e0a45e1 Improved infrastructure for prepared statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
26
100c4e0a45e1 Improved infrastructure for prepared statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
27 public SymbolicStatement.Instance getStatement(String key)
100c4e0a45e1 Improved infrastructure for prepared statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
28 throws SQLException
100c4e0a45e1 Improved infrastructure for prepared statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
29 {
100c4e0a45e1 Improved infrastructure for prepared statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
30 SymbolicStatement.Instance stmnt = boundStatements.get(key);
100c4e0a45e1 Improved infrastructure for prepared statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
31 if (stmnt != null) {
100c4e0a45e1 Improved infrastructure for prepared statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
32 return stmnt;
100c4e0a45e1 Improved infrastructure for prepared statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
33 }
100c4e0a45e1 Improved infrastructure for prepared statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
34
100c4e0a45e1 Improved infrastructure for prepared statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
35 SymbolicStatement ss = statements.get(key);
100c4e0a45e1 Improved infrastructure for prepared statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
36 if (ss == null) {
100c4e0a45e1 Improved infrastructure for prepared statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
37 return null;
100c4e0a45e1 Improved infrastructure for prepared statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
38 }
100c4e0a45e1 Improved infrastructure for prepared statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
39
100c4e0a45e1 Improved infrastructure for prepared statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
40 stmnt = ss.new Instance(connection);
100c4e0a45e1 Improved infrastructure for prepared statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
41 boundStatements.put(key, stmnt);
100c4e0a45e1 Improved infrastructure for prepared statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
42 return stmnt;
100c4e0a45e1 Improved infrastructure for prepared statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
43 }
100c4e0a45e1 Improved infrastructure for prepared statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
44
100c4e0a45e1 Improved infrastructure for prepared statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
45 public void close() {
100c4e0a45e1 Improved infrastructure for prepared statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
46 for (SymbolicStatement.Instance s: boundStatements.values()) {
100c4e0a45e1 Improved infrastructure for prepared statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
47 s.close();
100c4e0a45e1 Improved infrastructure for prepared statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
48 }
100c4e0a45e1 Improved infrastructure for prepared statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
49
100c4e0a45e1 Improved infrastructure for prepared statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
50 try {
100c4e0a45e1 Improved infrastructure for prepared statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
51 connection.close();
100c4e0a45e1 Improved infrastructure for prepared statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
52 }
100c4e0a45e1 Improved infrastructure for prepared statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
53 catch (SQLException sqle) {
100c4e0a45e1 Improved infrastructure for prepared statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
54 }
100c4e0a45e1 Improved infrastructure for prepared statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
55 }
100c4e0a45e1 Improved infrastructure for prepared statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
56 }
100c4e0a45e1 Improved infrastructure for prepared statements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
57 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org