简体   繁体   中英

query to get userimage path and user information from two different tables

I am developing a photo album web application in which I am using two tables USER_INFORMATION and USER_PHOTO . USER_INFORMATION contains only user record which is one record, USER_PHOTO contains more than one photo form a single user. I want to get the number of user information along with their userimage path and store it in to a Java Pojo variable like a list so that I can display it using display tag in struts 2.

The table goes like this.

        USER_PHOTO                                   USER_INFORMATION
   =======================                     =================================
   | IMAGEPATH | USER_ID |                     | USER_NAME | AGE | ADDRESS |ID |
   =======================                     =================================
   |xyz        | 1       |                     | abs       | 34  |  sdas   | 1 | 
   |sdas       | 1       |                     | asddd     | 22  | asda    | 2 |
   |qwewq      | 2       |                     | sadl      | 121 | asd     | 3 |
   | asaa      | 1       |                     ==================================
   | 121       | 3       | 
   =======================

As you can see user_id=1 has 3 photos and user_id=2 has 2 photos and 3 has one photo. Now I want to write the query to get user information along with their imagepath.

So I tried using the code below to store it in an ArrayList, my code in Action class:

 public ArrayList loadData() throws ClassNotFoundException, SQLException {
ArrayList userList = new ArrayList();
con = DriverManager.getConnection(url + dbName, userName, password);
PreparedStatement ps = null;
try {
    String name;
    String fatherName;
    int Id;
    String filePath;
    int age;
    String address;
    String query = "SELECT NAME,FATHERNAME,AGE,ADDRESS,ID,FILEPATH FROM USER_INFORMATION ,USER_PHOTO WHERE ID=USER_ID";
    ps = con.prepareStatement(query);
    ResultSet rs = ps.executeQuery();
    while (rs.next()) {
        name = rs.getString(1);
        fatherName = rs.getString(2);
        age = rs.getInt(3);
        address = rs.getString(4);
        Id = rs.getInt(5);
        UserData list = new UserData();
        list.setName(name);
        list.setFatherName(fatherName);
        list.setAge(age);
        list.setAddress(address);
        userList.add(list);
    }
    ps.close();
    con.close();

And my JSP goes like this:

<display:table id="data" name="sessionScope.UserForm.userList" requestURI="/userAction.do" pagesize="1" >
  <display:column property="name" title="NAME" sortable="true"   />
  <display:column property="fatherName" title="User Name" sortable="true"  />
  <display:column property="age" title="AGE" sortable="true"   />
  <display:column property="address" title="ADDRESS" sortable="true"  />
  <display:column property="filePath" title="PATH" sortable="true"  />
</display:table>

So while I am displaying it shows every userinformation correctly but in the path column is blank.

Expected output: I want to dispaly user information name, age, address, and IMAGEPATH (which is more than one record in another table).

A small tip : Use column names rather then their indices when reading data from the result set. Example : fatherName = rs.getString("FATHERNAME");

This will be more easy to maintain incase you change your query and is much clearer.

Now, where in your code do you read the FILEPATH column ?! You only read the first five columns (name, father_name, age, address, id) but nothing is read for the FILEPATH column.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM