I am a student of Computer Science. Now I am working on a project with jdbc. I have to table in database - USER, ROLE.
Where each USER have a one or multiple ROLE. I save the ROLE.ID (primary key or ROLE) in USER.ROLE_ID column.
In java code level I have two entity class also - User.java and Role.java. I can make the simple SQL query with joining these tables.
Please see the following queries -
1. Selecting USER.NAME and ROLE.NAME for USER.NAME='admin' -
SELECT USER.NAME, ROLE.NAME FROM USER, ROLE
WHERE USER.ROLE_ID = ROLE.ROLE_ID
AND USER.NAME='admin';
2. Selecting USER.ID and ROLE.ID for the USER.NAME='admin' -
SELECT USER.ID, ROLE.ID FROM USER, ROLE
WHERE USER.ROLE_ID = ROLE.ROLE_ID
AND USER.NAME='admin';
I have to make 2 different queries ONLY for the different columns I am selecting. Here most of the query is same. My question is can I do something so that I can dynamically select different (in first case - USER.NAME, ROLE.NAME and Second case USER.ID, ROLE.ID) types of column using a single query?