aspose file tools
The moose likes JDBC and Relational Databases and the fly likes JDBC assessment part #1 complete, feedback appreciated Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "JDBC assessment part #1 complete, feedback appreciated" Watch "JDBC assessment part #1 complete, feedback appreciated" New topic

JDBC assessment part #1 complete, feedback appreciated

Billy Sclater
Ranch Hand

Joined: Nov 18, 2012
Posts: 142

As I mentioned, I am currently doing a coding exercise for a job opportunity. I have included a brief explanation of part 1 of the exercise along with 'some' of the code (just 2 methods). They tell me that I will evaluated on: java coding standards, use of control statements (if, for, while, switch, etc.), use of the Collection API, Utilisation of OOPs concepts, the efficiency of the code, exception Handling, Usage of Design Patterns and Test Cases (preferably in JUnit). I'd really appreciate some feedback on the quality of the code with regards to the criteria mentioned!

I'll just explain what I'm trying to do first. My objective is to read some data about a number of people from a file. The data is as follows: an id number, the persons first and last name, their street name and city name. After I have read the data and stored it in a collection, I wish to supply a getter method to allow the data about each person to be read in String format. Eventually, but not here, the getter will be used by another class.

I decided to make 2 classes: a Person class, each person object has instance variables containing the data. This class also contains getter and setter methods for reading and writing this data. The 2nd class is a DataFileReader class which supplies a method for putting the file into a collection. This method utilises the Person class's set methods.

Here is the readPersonFile method from the DataFileReader class. What it does is to read a file '' which contains 4 rows & 5 columns of info. The first line contains the column headings 'id, FirstName, LastName and so on'. The next 3 lines contain all the juicy data! As you can see, a String[] array stores each row, and then each array(row) is added to an ArrayList. I chose an ArrayList as it is dynamically sized (the data file could be any size), and also I can generically type the Arraylist to <Person> to ensure nothing inappropriate is added to it. The method returns the ArrayList.

Here is the getter method of the Person class. It takes the ArrayList of Person objects from the readPersonFile method, and converts it into an ArrayList of String (ArrayLists). Each String ArrayList represents each individual Person object containing that Person's data eg/their data 'id, lastname etc'. The Person objects are converted to Strings so that they can be read from the console if desired.

Here is a test case method for the readPersonFile method:

Jeanne Boyarsky
author & internet detective

Joined: May 26, 2003
Posts: 31447

Two things to improve:
  • Variable names - "l" and "l2" are not good variable names. They don't communicate much and require the reader to remember which is which.
  • The JUnit test only checks the code doesn't blow up. It could use some assertEquals() statements to confirmt eh correct data was read/parsed

  • None of this has to do with JDBC though. Was there a JDBC part to the assignment? Or did you assume you should use a file instead of a database?

    [OCA 8 book] [Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
    Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
    E Armitage

    Joined: Mar 17, 2012
    Posts: 953
    ArrayList<ArrayList<String>> is bad. You don't need that getPerson method at all (It's also not a getter method). Just write a reasonable toString method in your Person class.
    Also, test questions that involve IO usually have a significant weight devoted to checking if you are able to close all streams that you opened correctly in a finally block or using try with resources.
    Programs that read data from files usually break when the files are not in the expected format. You should therefore add checks that ensure that the split array has at least 5 entries before doing columns[4].
    You also mentioned having getters and setters but you have code like which is not using the getters and shows that you have not defined the fields as private. You are probably expected to define the fields as private.
    Billy Sclater
    Ranch Hand

    Joined: Nov 18, 2012
    Posts: 142

    Thanks for the feedback! Actually, I came across a great YT tutorial last night all about JDBC. I going to watch it through, and then carry on with the coding exercise later tonight (:
    jQuery in Action, 2nd edition
    subject: JDBC assessment part #1 complete, feedback appreciated