• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JDBC assessment part#1 Attempt re-vamped! Would love feedback!

 
Ranch Hand
Posts: 146
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is the first part of my solution to the coding assignment. The instructions are (more or less), to read a 'Person data file' (it's a text file
containing 3 rows (3 people) with 5 columns, an id# column, first, last names, a street and a city column). The task is then to populate a database table with this information.

First, I created a Person class to represent the people and info in the database:



I then created a class with a method to connect to the database:



I then made an interface with the methods that I'll need to complete the assigment. You can see the createPersonTable() & insert(Person person)!



I knocked also knocked up a DataFileReader class to read the file:


I was advised to 'close' the scanner in a finally block after use. But wouldn't the scanner close once the try block exits?


Next, I implement my interface by completing the createPersonTable() method, and the insert(Person person) method.



Finally, a class with a main method to create a table and then populate it:



I have a part 2 to this assignment. That will be to create yet another table and populate it with data from an 'Order data file'. 3 columns and 3 fields for each person in the Data file.
I have a couple of questions about that:

#1 Should I make another FileReader class -:OrderFileReader(?), or just use the DataFileReader class that I already have to do that, and just rename it?
#2 Same question about making another interface and class to implement it for the Order table.
Should I just use the interface and PersonDaoImpl class to do that (and just rename them)?
Or should I make a special seperate interface and class for the Order table?

I'd also love some feedback on my code thus far! I know there is quite a bit of code, and lots of questions, so...yeh....quite a tall order!
Apologies for that(: but if you can help out....much appreciated!


 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In ConnectionConfiguration, think about what happens if an exception is thrown. The program prints an exception leaving the connection as null. Then the code blows up later with a null pointer. This is why swallowing exceptions is a bad idea. The DAO has a similar problem with error handling/swallowing exceptions. And consider putting the close logic in a helper class so it isn’t repeated.

Billy Sclater wrote:I was advised to 'close' the scanner in a finally block after use. But wouldn't the scanner close once the try block exits?


Not automatically, no. If you are using Java 7, you can use the "try with resources" syntax where it will close automatically.

Billy Sclater wrote:#1 Should I make another FileReader class -:OrderFileReader(?), or just use the DataFileReader class that I already have to do that, and just rename it?
#2 Same question about making another interface and class to implement it for the Order table.
Should I just use the interface and PersonDaoImpl class to do that (and just rename them)?
Or should I make a special seperate interface and class for the Order table?


If you are just renaming without changing the code, it is really the same file. I think you'd be making significant changes though. So just look for code you can reuse. Maybe put it in a helper class.
 
Billy Sclater
Ranch Hand
Posts: 146
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for that! I've made a few changes. Here is my ConnectionConfiguration class, and below it my DaoImpl class with (hopefully) some better exception handling and the use of a 'closeLogic' method from a helper class.



Here is my helper class with the closeLogic method & a method validateData (along with an 'isNumeric' method and an 'isAlpha' method) to validate the Person.data file.


And this is what my readDataFile class now looks like:



I just have a question about the validateData method. Should I leave it as it is? Or should I convert the logic into a custom exception?
 
Jeanne Boyarsky
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Billy Sclater wrote:I just have a question about the validateData method. Should I leave it as it is? Or should I convert the logic into a custom exception?


There's no right/wrong answser. It depends on your requirements. You actually have a third choice - throw the built in IllegalArgumentException.
 
Billy Sclater
Ranch Hand
Posts: 146
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm now writing the JUnit test cases for my JDBC assessment project. Here are the methods I'm testing, I've included a brief explanation of what each of these test methods will do:

#1 public void insertTest(): inserts a Person object into a table, retrieves it from the table and then runs an assertEquals on the 2 Person objects.

#2 public void validatePersonTest(): There a 3 versions of this each puts an invalid field into a person object followed by assert(false). validatePerson method should exit before the assert(false) executes.

#3 public List<Person> fetchPersonDataTest(): simply checks to see if it returns a list. If method does not return a list of person objects then assert(false).

#4, #5, #6 I will test the same corresponding Order methods in the same way.

Do you think I've got it right?

Also there are several methods I will not be testing, such as getters and setters. Is it appropriate to skip the testing of these? I've also not tested methods that solely consist of simpler methods that 'have already been tested'. Is this appropriate?
 
Jeanne Boyarsky
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Getters and setters would be tested as part of other tests. (If they aren't called elsewhere, there is no reason to exist.) For example, your tests typically call getters in asserting the values are correct.

As for whether to test methods made up of others, it depends. It is good to try to do so. Sometimes we skip them because they are hard to test together. But you wrote the code so shouldn't have a "legacy code" problem.
 
Billy Sclater
Ranch Hand
Posts: 146
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks guys! I'll crack on and finish my test classes. I have to hand in the assessment tomorrow morning, I'll post back and let you all know if I passed!
 
Billy Sclater
Ranch Hand
Posts: 146
Eclipse IDE Java Linux
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wow! Not only did I pass the coding exercise, they also said that out of all the candidates that mine (along with one other) was the strongest! I have an interview in 2 days, the interview will cover core Java, database, SQL, and my previous Java roles. Not feeling overly confident as I only learnt enough SQL and database to complete the coding exercise. And also I only have 'a few months' commercial experience doing a remote project on a voluntary basis. I'll just have to be honest, go for it and see what happens! Thanks for your help guys! Really appreciate it!
 
Jeanne Boyarsky
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good job! And good call on being honest. It's better to not have experience and know things than lie and be caught. Or lie and look like you didn't learn much.
 
Billy Sclater
Ranch Hand
Posts: 146
Eclipse IDE Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I took the interview last week, admitted having no prior database, SQL or design pattern experience, but generally felt I did ok. They questioned me on the code, gave me a pretty easy test with questions like 'Describe inheritance', then topped it off with a fluffy HR interview. I got a call today, and apparently I've got the job 'Associate Java Software Engineer''!. And I'm feeling pretty chuffed!
Thanks again for your help guys!! Very much appreciated!!
 
Sheriff
Posts: 3837
66
Netbeans IDE Oracle Firefox Browser
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Congratulations!
 
reply
    Bookmark Topic Watch Topic
  • New Topic