How to get the value of the reference table according to the foreign key?

advertisements

I have two tables with the foreign key relating them: enter image description here

I have used a SQL query to retrieve the value form both tables:

User u = new User();

String sql = "SELECT * FROM user INNER JOIN account ON user.id = account.user WHERE user.id = 1 ";

try {
        Statement stm = con.createStatement();

        ResultSet rsu = stm.executeQuery(sql);

        while(rsu.next()){
            u.setFname(rsu.getString("fname"));
            u.setLname(rsu.getString("lname"));
            u.setMname(rsu.getString("mname"));
            u.setGender(rsu.getString("gender"));
            u.setAddress(rsu.getString("address"));
            u.setCitizenship(rsu.getLong("citizenship"));

            /**
            *
            *   Here i want to get values of account table and set it on
            *   user object to return u
            *
            */
        }

        return u;

    } catch (SQLException e) {
        e.printStackTrace();
    }


It is easy to get the values of table. All you need to do is provide the column name of account table which you already got in your ResultSet.

u.setAccountID(rsu.getLong("accId"));

Here account id is stored in Result set object which you already retrieved from database in result set