1
|
package eu.dnetlib.data.claimsDemo;
|
2
|
|
3
|
/**
|
4
|
* Created by Eri on 18/11/2015.
|
5
|
*/
|
6
|
|
7
|
|
8
|
import org.apache.log4j.Logger;
|
9
|
|
10
|
import java.sql.ResultSet;
|
11
|
import java.util.ArrayList;
|
12
|
|
13
|
/**
|
14
|
* @author eri
|
15
|
*/
|
16
|
|
17
|
public class SqlDAO {
|
18
|
private SqlStore sqlStore;
|
19
|
private QueryGenerator queryGenerator;
|
20
|
|
21
|
private Logger log = Logger.getLogger(this.getClass());
|
22
|
private long startTime;
|
23
|
private long endtime;
|
24
|
|
25
|
public SqlDAO() throws Exception {
|
26
|
|
27
|
}
|
28
|
|
29
|
|
30
|
|
31
|
/**
|
32
|
* Example of calling a stored procedure
|
33
|
*
|
34
|
* @throws Exception
|
35
|
*/
|
36
|
public void callStoredProcedure() throws Exception {
|
37
|
log.info(" Executing Stored Procedure...");
|
38
|
sqlStore.getConnection();
|
39
|
startTime = System.currentTimeMillis();
|
40
|
|
41
|
String q = queryGenerator.callProcedure("create_views");
|
42
|
sqlStore.executeStoredProcedure(q);
|
43
|
sqlStore.closeConnection();
|
44
|
endtime = System.currentTimeMillis();
|
45
|
log.info("Total time for Procedure : " + ((endtime - startTime) / 60000) + " minutes ");
|
46
|
}
|
47
|
|
48
|
|
49
|
/**
|
50
|
* Example of executing a prepared query with a result set
|
51
|
* with Query and parameters given as arguements
|
52
|
*
|
53
|
* @throws Exception
|
54
|
*/
|
55
|
public String executePreparedQuery(String q, ArrayList<String> data) throws Exception {
|
56
|
try {
|
57
|
|
58
|
ResultSet rs = sqlStore.executeQuery(q, data);
|
59
|
return sqlStore.getResults(rs);
|
60
|
|
61
|
} catch (Exception e)
|
62
|
|
63
|
{
|
64
|
log.error(e.getMessage());
|
65
|
throw new Exception(e);
|
66
|
}
|
67
|
}
|
68
|
|
69
|
/**
|
70
|
* Example of executing a prepared query with a result set
|
71
|
* with query set from Query Generator class
|
72
|
*
|
73
|
* @throws Exception
|
74
|
*/
|
75
|
public String executePreparedQuery(ArrayList<String> data) throws Exception {
|
76
|
try {
|
77
|
ResultSet rs = sqlStore.executeQuery(queryGenerator.selectClaims(), data);
|
78
|
return sqlStore.getResults(rs);
|
79
|
|
80
|
} catch (Exception e)
|
81
|
|
82
|
{
|
83
|
log.error(e.getMessage());
|
84
|
throw new Exception(e);
|
85
|
}
|
86
|
|
87
|
}
|
88
|
|
89
|
/**
|
90
|
* Example of executing a custom prepared query
|
91
|
*
|
92
|
* @throws Exception
|
93
|
*/
|
94
|
public ResultSet executePreparedQuery(String q) throws Exception {
|
95
|
try {
|
96
|
ResultSet rs = sqlStore.executeQuery(q);
|
97
|
return rs;
|
98
|
|
99
|
} catch (Exception e)
|
100
|
|
101
|
{
|
102
|
log.error(e.getMessage());
|
103
|
throw new Exception(e);
|
104
|
}
|
105
|
|
106
|
}
|
107
|
|
108
|
public void executeUpdateQuery(String q) throws Exception {
|
109
|
try {
|
110
|
sqlStore.executeUpdate(q);
|
111
|
|
112
|
|
113
|
} catch (Exception e)
|
114
|
|
115
|
{
|
116
|
log.error(e.getMessage()+"\n Query is:"+q);
|
117
|
throw new Exception(e);
|
118
|
}
|
119
|
|
120
|
}
|
121
|
|
122
|
public SqlStore getSqlStore() {
|
123
|
return sqlStore;
|
124
|
}
|
125
|
|
126
|
public void setSqlStore(SqlStore sqlStore) {
|
127
|
this.sqlStore = sqlStore;
|
128
|
}
|
129
|
|
130
|
public QueryGenerator getQueryGenerator() {
|
131
|
return queryGenerator;
|
132
|
}
|
133
|
|
134
|
public void setQueryGenerator(QueryGenerator queryGenerator) {
|
135
|
this.queryGenerator = queryGenerator;
|
136
|
}
|
137
|
}
|