- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataBase.java
More file actions
Latest commit
224 lines (199 loc) · 8.57 KB
/
Copy pathDataBase.java
File metadata and controls
224 lines (199 loc) · 8.57 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
importjava.sql.*;
importjava.util.List;
importjava.util.concurrent.ConcurrentLinkedQueue;
publicclassDataBase {
/**
* JDBC
* 1-Import JDBC Packages (import com.Microsoft.sqlserver.jdbc.SQLServerDriver;)
* 2-Register JDBC Driver
* 3-Database URL Formulation
*/
// JDBC driver name and database URL
privatestaticfinalStringdriver = ("com.microsoft.sqlserver.jdbc.SQLServerDriver");
privatestaticfinalStringdBURL = "jdbc:sqlserver://localhost:1433;databaseName=SearchEngine"; //to create a properly formatted address that points to the database to which you wish to connect
privatestaticfinalStringusername = "sa";
privatestaticfinalStringpassword = "1234";
privatestaticjava.sql.Connectionconnection;
publicDataBase() {
try {
Class.forName(driver).newInstance();// force it to be included in the final war,
connection = DriverManager.getConnection(dBURL,username,password);
System.out.println("Connecting to database...");
} catch (Exceptione) {
e.printStackTrace();
// System.out.println("Error: unable to load driver class!");
System.exit(1);
}
}
/**
* Retreves nonvisited links to a queue
*
* @return a queue of urls
* @throws java.sql.SQLException
*/
publicsynchronizedConcurrentLinkedQueue<Url> RetriveNonVisited() throwsSQLException {
ConcurrentLinkedQueue<Url> seedSet = newConcurrentLinkedQueue<>();
CallableStatementcStmt = connection.prepareCall("{call RetriveNonVisited}"); //throws sqlexception -->check later
cStmt.execute();
ResultSetresult = cStmt.executeQuery();
while (result.next()) {//while there are rows in returned result set
seedSet.add(newUrl(result.getInt("id"), result.getString("url"))); //adding all non visited to urlqueues
}
result.close();
cStmt.close();
returnseedSet;
}
publicsynchronizedvoidInsertLink(intsrcId, StringdestUrl) {
try {
CallableStatementcStmt = connection.prepareCall("{call InsertLink(?,?)}"); //throws sqlexception -->check later
cStmt.setInt("srcId", srcId);
cStmt.setString("destUrl", destUrl);
cStmt.execute();
} catch (SQLExceptione) {
System.out.println("SQLException: " + e.getMessage());
}
}
publicsynchronizedvoidInsertLinks(IntegersrcId, List<String> destUrls) {
try {
connection.setAutoCommit(false);
CallableStatementcStmt = connection.prepareCall("{call InsertLink(?,?)}"); //throws sqlexception -->check later
for(StringdestUrl: destUrls){
cStmt.setInt("srcId", srcId);
cStmt.setString("destUrl", destUrl);
cStmt.addBatch();
}
cStmt.executeBatch();
connection.commit();
connection.setAutoCommit(true);
} catch (SQLExceptione) {
System.out.println("SQLException: " + e.getMessage());
}
}
publicsynchronizedvoidInsertLinks(ConcurrentLinkedQueue<OutgoingLinks> outgoingLinks) {
try {
connection.setAutoCommit(false);
CallableStatementcStmt = connection.prepareCall("{call InsertLink(?,?)}"); //throws sqlexception -->check later
while (!outgoingLinks.isEmpty()) {
OutgoingLinksoutgoingLink = outgoingLinks.poll();
IntegersrcUrl = outgoingLink.getSrcId();
for (StringdestUrl : outgoingLink.getDestUrls()) {
cStmt.setInt("srcId", srcUrl);
cStmt.setString("destUrl", destUrl);
cStmt.addBatch();
}
}
cStmt.executeBatch();
connection.commit();
connection.setAutoCommit(true);
} catch (SQLExceptione) {
System.out.println("SQLException: " + e.getMessage());
}
}
publicsynchronizedvoidMarkVisited(IntegerurlId) {
try {
CallableStatementcStmt = connection.prepareCall("{call MarkVisited(?)}"); //throws sqlexception -->check later
cStmt.setInt("urlId", urlId);
cStmt.execute();
} catch (SQLExceptione) {
System.out.println("SQLException: " + e.getMessage());
}
}
publicsynchronizedvoidMarkVisited(ConcurrentLinkedQueue<Integer> visitedUrls) {
try {
connection.setAutoCommit(false);
CallableStatementcStmt = connection.prepareCall("{call MarkVisited(?)}"); //throws sqlexception -->check later
while (!visitedUrls.isEmpty()) {
cStmt.setInt("urlId", visitedUrls.poll());
cStmt.addBatch();
}
cStmt.executeBatch();
connection.commit();
connection.setAutoCommit(true);
} catch (SQLExceptione) {
System.out.println("SQLException: " + e.getMessage());
}
}
publicsynchronizedvoidUnmarkParseable(IntegerurlId) {
try {
CallableStatementcStmt = connection.prepareCall("{call UnmarkParseable(?)}"); //throws sqlexception -->check later
cStmt.setInt("urlId", urlId);
cStmt.execute();
} catch (SQLExceptione) {
System.out.println("SQLException: " + e.getMessage());
}
}
publicsynchronizedvoidUnmarkParseable(ConcurrentLinkedQueue<Integer> notParseable) {
try {
connection.setAutoCommit(false);
CallableStatementcStmt = connection.prepareCall("{call UnmarkParseable(?)}"); //throws sqlexception -->check later
while (!notParseable.isEmpty()) {
cStmt.setInt("urlId", notParseable.poll());
cStmt.addBatch();
}
cStmt.executeBatch();
connection.commit();
connection.setAutoCommit(true);
} catch (SQLExceptione) {
System.out.println("SQLException: " + e.getMessage());
}
}
publicsynchronizedvoidUnmarkVerified(IntegerurlId) {
try {
CallableStatementcStmt = connection.prepareCall("{call UnmarkVerified(?)}"); //throws sqlexception -->check later
cStmt.setInt("urlId", urlId);
cStmt.execute();
} catch (SQLExceptione) {
System.out.println("SQLException: " + e.getMessage());
}
}
publicsynchronizedvoidUnmarkVerified(ConcurrentLinkedQueue<Integer> notVerified) {
try {
connection.setAutoCommit(false);
CallableStatementcStmt = connection.prepareCall("{call UnmarkVerified(?)}"); //throws sqlexception -->check later
while (!notVerified.isEmpty()) {
cStmt.setInt("urlId", notVerified.poll());
cStmt.addBatch();
}
cStmt.executeBatch();
connection.commit();
connection.setAutoCommit(true);
} catch (SQLExceptione) {
System.out.println("SQLException: " + e.getMessage());
}
}
publicsynchronizedvoidUnmarkRobotallowed(IntegerurlId) {
try {
CallableStatementcStmt = connection.prepareCall("{call UnmarkRobotallowed(?)}"); //throws sqlexception -->check later
cStmt.setInt("urlId", urlId);
cStmt.execute();
} catch (SQLExceptione) {
System.out.println("SQLException: " + e.getMessage());
}
}
publicsynchronizedvoidUnmarkRobotallowed(ConcurrentLinkedQueue<Integer> notRobotallowed) {
try {
connection.setAutoCommit(false);
CallableStatementcStmt = connection.prepareCall("{call UnmarkRobotallowed(?)}"); //throws sqlexception -->check later
while (!notRobotallowed.isEmpty()) {
cStmt.setInt("urlId", notRobotallowed.poll());
cStmt.addBatch();
}
cStmt.executeBatch();
connection.commit();
connection.setAutoCommit(true);
} catch (SQLExceptione) {
System.out.println("SQLException: " + e.getMessage());
}
}
publicsynchronizedintGetVisitedCount() {
try {
CallableStatementcStmt = connection.prepareCall("{call CountVisited(?)}"); //throws sqlexception -->check later
cStmt.registerOutParameter(1, java.sql.Types.INTEGER);
cStmt.execute();
returncStmt.getInt(1);
} catch (SQLExceptione) {
System.out.println("SQLException: " + e.getMessage());
}
return0;
}
}