#195 Code Review of a File Parser Saving Data to a Database

Companies:
Digitalleague
Themes:
JavaJDBCCode Review

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

Mentor
Answer as you would in a real interview. I’ll ask follow-up questions, then compare your answer with the reference answer and show your accuracy as a %.
Sign in to start the dialogue The mentor keeps the context of the current question and your previous messages.
Sign in