Your question is Legacy Code Improvements. Take a moment with it on the right.
Talk me through your thinking if you like. When you're confident, submit your answer and I'll grade it like a real screen (7/10 or better passes).
Medallia's feedback platform has a legacy servlet endpoint that customer-success reps use to search survey comments by keyword. It hasn't been touched in years and is up for review.
public class CommentSearchServlet extends HttpServlet {
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
String keyword = req.getParameter("keyword");
Vector results = new Vector();
try {
Connection conn = DriverManager.getConnection(DB_URL, DB_USER, DB_PASS);
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM comments WHERE text LIKE '%" + keyword + "%'");
while (rs.next()) {
results.addElement(rs.getString("text"));
}
} catch (Exception e) {}
PrintWriter out = resp.getWriter();
for (int i = 0; i < results.size(); i++) {
out.println((String) results.elementAt(i));
}
}
}
Propose enhancements or bug fixes for this snippet of legacy code. What's wrong with it as written, and what would you change before letting it back into production?