-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFederalConnection.java
More file actions
39 lines (34 loc) · 1.27 KB
/
FederalConnection.java
File metadata and controls
39 lines (34 loc) · 1.27 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
package com.acme.search;
import java.sql.PreparedStatement;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
@Service
public class FederalConnection {
@Autowired
private FederalConnectionLoader fedConnectionLoader;
// connect to the federal database and search the forecasts table for entries with the given query
String doSearch(final String searchTerm) throws SQLException {
// connect to the federal database
Connection conn = fedConnectionLoader.getConnection();
// search the forecasts table for entries with the given query
String query = "SELECT * FROM forecasts WHERE entry_desc LIKE ?";
PreparedStatement stmt = conn.prepareStatement(query);
stmt.setString(1, "%" + searchTerm + "%");
ResultSet rs = stmt.executeQuery();
List<String> ids = new ArrayList<>();
while(rs.next()) {
String id = rs.getString("entry_id");
ids.add(id);
}
rs.close();
stmt.close();
conn.close();
return String.join(",", ids);
}
}