#195 Code Review of a File Parser Saving Data to a Database
Perform a code review:
public class Parser {
static private File file;
static public void setFile(File file) {
Parser.file = file;
}
public static String parse() throws Exception {
Connection connection = DriverManager.getConnection("jdbc:pg:@production.internal/proddb", "admin", "pass");
connection.setAutoCommit(true);
BufferedReader reader = new BufferedReader(new FileReader(file));
String parsedText = "";
String line;
while ((line = reader.readLine()) != null) {
for (int i = 0; i < line.length(); i++) {
int charCode = line.codePointAt(i);
// charCode >= A && charCode <= z charCode == ' '
if ((charCode > 0x41 && charCode < 0x7A) || charCode == 0x20) {
parsedText = parsedText + line.charAt(i);
}
}
}
Arrays.stream(parsedText.split("\\s"))
.parallel()
.forEach(word -> {
try {
ResultSet resultSet = connection.prepareCall("select blocked_word from blocked_words").executeQuery();
Boolean blockedWordFound = false;
while (resultSet.next()) {
if (Objects.equals(resultSet.getString(1), word)) {
blockedWordFound = true;
}
}
if (!blockedWordFound) {
connection.prepareStatement("insert into data values ('" + word + "')").execute();
}
} catch (SQLException ex) {
}
});
return "SUCCESS";
}
}
Question review
Sign in to start the dialogue
The mentor keeps the context of the current question and your previous messages.
Sign in
Available with Premium
This is just one of the solutions available with Premium. And more:
- 250+ advanced lectures
- 1300+ practice exercises
- 500+ solutions to problems from real interviews
- Personalized Java Hub certificates
- Expanded access to the AI mentor