3

So I have a homework assignment to build a console application in Java while utilizing mySQL databases. Our teacher wants us to use objects and ArrayLists, as Java is an "object oriented language".

I am not sure why I must parse the mySQL ResultSets into objects, and not print them directly in my methods in a nice printf format. Similarly, why should I make class objects when inserting new data and not altering the database directly through my connection.

Can someone please explain and point me in the right direction. I don't need any code. Just trying to understand.

4
  • Welcome to Stack Overflow! :-) Help with homework assignments is the typical thing you'll see here. For more information check out: stackoverflow.com/help/how-to-ask Commented Apr 3, 2020 at 9:37
  • 2
    "I am not sure why I must parse the mySQL ResultSets into objects, and not print them directly" ...mostly because in a real-world application you're likely to do a lot more with the records than just print them directly to the console, and at that point it's useful to put them into a solid data structure you can move around and manipulate easily. It can also make the code easier to maintain - real-world apps get updated lots of times usually. So I imagine the purpose of this task might be to get you used to using those structures, but without making the overall goal too complicated. Commented Apr 3, 2020 at 9:42
  • Thank you for the answer. Should I make a class with a constructor for each of the tables of the database and save the objects in ArrayLists? Commented Apr 3, 2020 at 9:45
  • 2
    Without seeing exactly what data you're working with and exactly what is expected of you then it's hard to say. There's rarely one single "right" way to do things anyway. The point is to try and use the structures as you see fit, to transfer the data efficiently between the database and the user interface. Think of it as an intermediate layer between what's on screen and what's in storage (which, in more complicated applications, will not always be structured or displayed in the exact same way as each other, hence the need for an intermediary). Commented Apr 3, 2020 at 9:48

2 Answers 2

3

One printf method for a ResultSet is not the correct way to implement! A resultset can have a different amount of different types of data.

The correct way would be to parse them to an object. For each table you might have an own class with constructor. For example:

You have a table 'Person' with attributes Name, age, address. Then you create a constructor

public Person(String name, int age, String address){}

or you could create a static method to parse like this:

public static Person parseFromResultSet(ResultSet r) throws ParseException {}

and then you can even write your own 'PersonParseExcpetion'.

Sign up to request clarification or add additional context in comments.

5 Comments

So if for example I need data from 4-5 different tables and I write a Select query with a couple of joins, should I parse that into an object. Because if I parse each table, I will just use "SELECT *" and it defeats the purpose of using JOINS and more advanced DML.
No, JOINS are very useful if you want to combine data from different tables. And yes, there are many possibilities of data forms, but then just write custom objects and use inheritance if some objects match. Just to clarify: You DO NOT parse the table, you parse the result set. So the result that you get after the SQL-Statement is processed.
Yes, yes. I meant the ResultSets. So, hypothetically if I need a ResultSet from 2 tables, do I parse that in an object and save the object into an ArrayList? Or do I parse the ResultSets of all the data from each table into 2 separate objects and Arraylists and manipulate the printout through Java? What would be the best approach?
That depends on your task. If you have like 4 or 5 different selections, then write 5 classes (with inheritance if possible) and parse the dataset to the according class, so all the resultset-data into ONE object. If you have a more complex task and have like 100s of different SELECTions then you have to think about a different way. Can't tell anything for case (2) in general.
If my answer pleases you needs, then please accept it to finally close this question ;)
2

"I am not sure why I must parse the mySQL ResultSets into objects, and not print them directly in a nice printf format. Similarly, why should I make class objects when inserting new data and not altering the database directly through my connection."

...mostly because in a real-world application you're likely to do a lot more with the records than just print them directly to the console, and at that point it's useful to put them into a solid data structure you can move around and manipulate easily.

It can also make the code easier to maintain - real-world apps get updated and altered lots of times usually, and may contain a lot of different data types and structures, some of which may be complex representations consisting of several other structures put together - and it's easier to build a complex object if you already have a lot of smaller objects to make it from (imagine building furniture from ready-made sections rather than starting by sawing all the planks and making all the screws yourself).

You can think of it as an intermediate layer between what's on screen and what's in storage - which, in more complicated applications, will not always be structured or displayed in the exact same way as each other, hence the need for an intermediary. Sometimes the display and the storage will not even be on the same computer (like in websites such as this one).

So I imagine the purpose of this assignment might be to get you used to using those structures, but without making the overall goal too complicated. The true purpose of the exercise is not always the obvious one.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.