Database application design help
I need to develop a application using swing and SQlserver.
Though it is not web based one,three computers connected to my computer will run the application parallely.
Problem
I have a JTable which will show the database table contents.
I have two designs:
1) To have a helper class( contains static methods)which will call interact with the database and get the values and call the ViewTable class(which draws and displays the table using the values passed by helper method)
Ex:
public static void interact()
{
//database interactions and
value got is data[][]
ViewTable abc= new ViewTable();
abc.populate(data);
}
Note that this interact will be called by another GUI(hereafter to be called as FirstGUI(is one from which user selects the option view to view)).
Problems:
1) When user wants to refresh(wants to see the newly updated contents too),he need to call interact()(helper method) which will again do necessary database access and which in turn call the same view from which it is called now and return the results.
like
(interact)-->viewfor refreshing->(interact)>view
(a cycle is formed )
Note: old view from which refresh call is made is hidden and new view which has updated values is shown
My question whether this approach has an any drawback due to this cycle?
Design 2:
2)When user selects view from FirstGUI, a JTable is drawn which in turn access the database and populate itself.
Problem:
1) I want this same JTable structure, to be used for many views,so if i access the database from this, i need to use multiple if statements, to select the database query
for example
public void drawTable(boolean today)
{
//table defn and abstractmodel defn are satisfied
1)user has selected to view only records entered today
if(today)
//call the query which will return it
else
//call the query which will return all
//in future i like to provide many options to users,and which implies this if also is expected to increase
}
Note: I cant keep a general table structure as a class and extend it as my program already extends a class and i cant keep it as interface too as i need to implement some methods then only, i can have the structure which cant be done in implementing class.
Kindly help me to solve this and
also suggest me a good design or a new design
Thank you
[2538 byte] By [
ramndra] at [2007-10-2 10:33:32]

> I need to develop a application using swing and
> SQlserver.
> Though it is not web based one,three computers
> connected to my computer will run the application
> parallely.
>
> Problem
> I have a JTable which will show the database table
> contents.
>
> I have two designs:
> 1) To have a helper class( contains static
> methods)which will call interact with the database
> and get the values and call the ViewTable class(which
> draws and displays the table using the values passed
> by helper method)
See comment below.
> Problems:
> 1) When user wants to refresh(wants to see the newly
> updated contents too),he need to call
> interact()(helper method) which will again do
> necessary database access and which in turn call the
> same view from which it is called now and return the
> results.
> like
> (interact)-->viewfor refreshing->(interact)>view
> (a cycle is formed )
> My question whether this approach has an any drawback due to this cycle?
No.
> Note: old view from which refresh call is made is
> hidden and new view which has updated values is
> shown
You don't need to do that. Indeed I even think it is not a good idea to do that.
Instead you can ask the view to refresh its display based on the updated contents.
> Design 2:
> 2)When user selects view from FirstGUI, a JTable is
> drawn which in turn access the database and populate
> itself.
>
> Problem:
> 1) I want this same JTable structure, to be used for
> many views,so if i access the database from this, i
> need to use multiple if statements, to select the
> database query
The Strategy pattern is an elegant way to avoid this kind of ifs.
> Note: I cant keep a general table structure as a
> class and extend it as my program already extends a
> class and i cant keep it as interface too as i need
> to implement some methods then only, i can have the
> structure which cant be done in implementing class.
I'm not sure I clearly understand everything you wrote, but I think an interface and a bit of delegation would do.
> Kindly help me to solve this and
> also suggest me a good design or a new design
> Thank you
Well, first, I think a JTable subclass is the wrong place to implement any logic anyway. Focus your attention on JTableModel instead, and write a custom implementation suited to hold your app's data.
I then suggest to either:
- modify your interact logic to update the model and to notify the JTable the model has changed (design 1), possibly using a strategy to get only the required data
or
- add logic to this TableModel to fetch/filter the relevant data (again, possibly delegating to a strategy)
If you want more theory around this, document yourself about MVC, especially how it tries to separate responsibilities between business logic, rendering logic, and interaction logic.
First,I would like to thank you for your wonderful replies and patience.
> I'm not sure I clearly understand everything you
> wrote, but I think an interface and a bit of
> delegation would do.
Here, what i mean is, i already extend a class so, i cant extend a general class(model from which all tables inherit behaviour and properties and change according to their needs).I have a method in model class ,which must be concrete,so i cant have that model as an interface.
hope u understood?
>
> Well, first, I think a JTable subclass is the wrong
> place to implement any logic anyway. Focus your
> attention on JTableModel instead, and write a custom
> implementation suited to hold your app's data.
> I then suggest to either:
and I use JTableModel only,ie., AbstractTableModel.
> - modify your interact logic to update the model and
> to notify the JTable the model has changed (design
> 1), possibly using a strategy to get only the
> required data
> or
> - add logic to this TableModel to fetch/filter the
> relevant data (again, possibly delegating to a
> strategy)
>
Kindly explain me in detail if possible with example.
is it like this, i need to query the db and take all the data and built the table using table model and call the JTable constructor for construction
like
keep the tablemodel building class and db in one class and after everything is fine call the jtable constructor which is in second class, and refreshing ,deletion all takes place from second class.(ie., both the class access the db)one for populating ad other for updation and deletion
is it u mean?
Kindly reply.
> > I'm not sure I clearly understand everything you
> > wrote, but I think an interface and a bit of
> > delegation would do.
> Here, what i mean is, i already extend a class so, i
> cant extend a general class(model from which all
> tables inherit behaviour and properties and change
> according to their needs).I have a method in model
> class ,which must be concrete,so i cant have that
> model as an interface.
> hope u understood?
What I mean is, that given an interface (say TableModel), you can take an existing concrete class, and edit it to implement the interface, and delegate all the interface's methods to another concrete class, which implements only the interface, and can extend whatever else it needs.
Alternately, you can write an Adapter class that implements the interface, and delegates to the existing class appropriately (changing the signature).
See below.
The fact that you already have a hierarchy does not tie you forever.
> > - modify your interact logic to update the model
> and
> > to notify the JTable the model has changed (design
> > 1), (...)
> > - add logic to this TableModel to fetch/filter the
> > relevant data (again, possibly delegating to a
> > strategy)
> >
> Kindly explain me in detail if possible with
> example.
Sorry, I won't have time to give code examples.
Here is a sketch:
Design 1:
- JTable user events are handled by a custom YourListener/Controller class
- YourListener/Controller reads (or updates) the DB
- YourListener/Controller then updates the custom YourTableModel
- Eventually YourTableModel notifies the JTable is has changed
Design 2:
- JTable user events are handled by a custom YourListener/Controller class
- YourListener/Controller relays the command to YourTableModel
- YourTableModel reads (or updates) the DB and updates itself
- Eventually YourTableModel notifies the JTable is has changed
> is it like this, i need to query the db and take all
> the data and built the table using table model and
> call the JTable constructor for construction
> like keep the tablemodel building class and db in one
> class and after everything is fine call the jtable
> constructor which is in second class, and refreshing
> ,deletion all takes place from second class.(ie.,
> both the class access the db)one for populating ad
> other for updation and deletion
That was quite a long sentence...
But in a word, no, that's not what I meant.
My points are :
- read-only and read-write operations with the DB need not be separated from one another across different classes - they aren't inherently different. You can separate them if you want to, but it's not necessary, and I even think it's a bad idea.
- but they do need to be separated from the GUI interaction per se. Both design proposals achieve that.
- once you have built the TableModel instance and the JTable instance, and wired them together, you don't need to reinstantiate them whenever the data change. Just update the model, and use the TableModel's fireXxxChanged method to let the JTable refresh its display based on the updated model contents. This is more related to a Swing forum.
- linked to your initial point (existing class hierarchy) : the (Swing-specific) TableModel class does not need to actually contain the data, it can be an Adapter over your (application specific) Model class. You specific model needs not be only a data holder, it can also implement and expose business methods.
> thank you for your (...) patience.
I'm still not sure I have understood what you need to do and what you already have.
If you want to continue discussing your design, please give us some code extracts, but please:
- format them appropriately (see the formatting tips and preview)
- post only small amount of code (a few class name and method signatures should be enough for the discussion).
Regards,
J.
I'm facing more or less the same problem, albeit with a slight difference. The updations are happening okay, and they are being reflected in the table. The problem is that after deletion, the listener goes off track, so that subsequent deletions act on the first row.
This is because I use table.changeSelection(0,0,true,false). It I don't do this, I get -1 as the return from getRowSelection...
#1
I don't understand why there is a cycle? i see one object calling another, and another object calling the one. But this does not *automatically* cause the One object to call the other object again does it?
> Design 2:
> 2)When user selects view from FirstGUI, a JTable is
> drawn which in turn access the database and populate
> itself.
>
> Problem:
> 1) I want this same JTable structure, to be used for
> many views,so if i access the database from this, i
> need to use multiple if statements, to select the
> database query
>
> for example
> public void drawTable(boolean today)
> {
> //table defn and abstractmodel defn are satisfied
> 1)user has selected to view only records entered
> d today
>
> if(today)
> //call the query which will return it
> else
> //call the query which will return all
> //in future i like to provide many options to
> users,and which implies this if also is expected to
> increase
> }
>
I don't see a problem with if statements. Whats your concern?
> Note: I cant keep a general table structure as a
> class and extend it as my program already extends a
> class and i cant keep it as interface too as i need
> to implement some methods then only, i can have the
> structure which cant be done in implementing class.
>
You have c++ background? Just guessing. I personally would not *extend* JTable anyway. Its the table model that contains all the data that you will be extending or modifying. Unless of course you are trying to add new functionality to the JTable class which I doubt.
> Kindly help me to solve this and
> also suggest me a good design or a new design
> Thank you
Just keep on your design path. Either way seems good to me as I can't really understand your problems with them. Your program design will grow and change as your understanding of your requirements grow and change. You dont have to get it perfect first time as long as you still have the source.
