Java JDBC - ResultSet and encapsulation. Many fields, way too many setters and getters to manage. Is there a better technique?

StackOverflow https://stackoverflow.com/questions/23553368

سؤال

I am writing a java program that reads a database file and shows the records on a gui screen, using JDBC and ResultSet. The database file has hundreds of fields. That means that if I separate gui and database processing in two class files and use encapsulation, I’d end up with hundreds of setter and getter methods (one for each field). Is there a better way of doing this?

Note that I have used setters and getters for this purpose and found out that it can get really hard to manage. I also have seen articles that advocate avoiding to use setters and getters altogether but did not find a concrete example of such technique for database management.

Thanks I appreciate your help.

هل كانت مفيدة؟

المحلول

One way is to just store each row into a Map.

To do this, you can retrieve the column names using ResultSetMetaData (key = column name) or you can just retrieve the column values using the column index (key = index). ResultSet can deal with it both ways.

Then, your result will look like a List<Map> structure.

نصائح أخرى

As mentioned, if your DB literally has hundreds of fields then something went wrong during the design.

In that case what i would do / advise is - try to categorize whatever is in your database. So for example imagine you have these columns

ID | FirstName | MiddleName | Surname | Gender | Age | HairColor | EyeColor | BuildingNO | StreetName | City | State | PostCode | Country | CarMake | CarPlate | etc...

I would create a class for Person, Address, Car and put queries in these classes that will extract info from database relevant to this particular object.

so in Person class i would have something like this

public class Person {
   private FirstName;
   private MiddleName;
   private Surname; 

   appropriate query here and plug the whole object into a list - so you will end up with a List of Objects 'Person' with relevant data in it. 
   /* Setters and Getters */

same for the other class Address

public class Address {


public class Car {

Hopefully you get what i mean

I’ve implemented the Map strategy as suggested by Leo. Thought it’s not a unanimous solution, it is something I can use immediately and I do think it is a pretty good alternative. It not only allows the elimination of excessive setters and getters but you can also create ArrayList’s (or any other collection) to hold each field dynamically. There’s no need to know the instance name of each list because you can access them by the Map’s key (key = column name), therefore avoiding the need to code a list for each field. I truly think this is a major advantage. Then it’s possible to iterate each field straight to a string (or any other type) making the code leaner.

For example:

Class LoadBigFile connects to the database and loads the file’s contents to a map.

Something like this (please note: this is just a very vague example to illustrate what can be done):

    Class LoadBigFile{

       private HashMap<String, ArrayList<String>> filemap = new HashMap<String,ArrayList<String>>();

       ////// ...Code to connect to database and load the file contents into the map

       ////// Setter and getter for the map:

       public void setFileMap(HashMap<String, ArrayList<String>> filemap){this.filemap=filemap;}
       public HashMap<String, ArrayList<String>> getFileMap(){return this.filemap;}

    }

Then in the class you’re going use the data (unpack the map):

      LoadBigFile loadBigFile = new LoadBigFile();
      String customer = loadBigFile.getFileMap().get("CUSTOMER").get(iterator));

NOTE: I will not go over the iterator because it would be off topic. All I can say is that it works.

Even thought I am still looking into other techniques such as ORM and JPA, I’ll try the map approach as I develop my skills and learn more sophisticated methods.

Thank you all. I hope this question helps other java enthusiasts out there.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top