Suppose in controller you get usertype in variable type, then you can manually create query and fetch the result from db like this :
String query = "Select * from "+type+" where <Your condition>";
But This is really bad idea to do this. Instead you should look at inheritance in db. and I guess this will help you to restructure your database.
Why this is bad idea?
Lets suppose you have 3 type of user currently in your system say : admin, employee and user.
Now after some time, if you need to add another type of user in system say HR, then what steps you have to perform:
1) Create another table for HR with same duplicate columns.
2) Change your logic in java files for HR user.
3) To scan the whole project for the same effect if you have hard-coded the query.
And benefits of inheritance in db:
Just insert the record in db with type = "HR" and you are done. Isn't is quite simple?
And FYI :
Advantages:
- Simple approach.
- Easy to add new classes, you just need to add new columns for the additional data.
- Supports polymorphism by simply changing the type of the row.
- Data access is fast because the data is in one table.
- Ad-hoc reporting is very easy because all of the data is found in one table.
Disadvantages:
- Coupling within the class hierarchy is increased because all classes are directly coupled to the same table. A change in one class can affect the table which can then affect the other classes in the hierarchy.
- Space potentially wasted in the database.
- Indicating the type becomes complex when significant overlap between types exists.
- Table can grow quickly for large hierarchies.
When to use:
- This is a good strategy for simple and/or shallow class hierarchies where there is little or no overlap between the types within the hierarchy.