简体   繁体   English

查询以从两个不同的表中获取userimage路径和用户信息

[英]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 . 我正在开发一个相册Web应用程序,其中使用了两个表USER_INFORMATIONUSER_PHOTO USER_INFORMATION contains only user record which is one record, USER_PHOTO contains more than one photo form a single user. USER_INFORMATION仅包含一个记录的用户记录, USER_PHOTO包含单个用户一张以上的照片。 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. 我想获取用户信息的数量以及他们的userimage路径,并将其存储在Java Pojo变量(如列表)中,以便可以在struts 2中使用display标记显示它。

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. 如您所见,user_id = 1有3张照片,user_id = 2有2张照片,3张有一张照片。 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: 所以我尝试使用下面的代码将其存储在ArrayList中,我的代码在Action类中:

 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: 我的JSP如下所示:

<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. 因此,当我显示它时,它会正确显示每个用户信息,但在path列中为空白。

Expected output: I want to dispaly user information name, age, address, and IMAGEPATH (which is more than one record in another table). 预期的输出:我想显示用户信息名称,年龄,地址和IMAGEPATH(在另一个表中有多个记录)。

A small tip : Use column names rather then their indices when reading data from the result set. 一个小技巧:从结果集中读取数据时,请使用列名而不是其索引。 Example : fatherName = rs.getString("FATHERNAME"); 示例: 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 ?! 现在,您在代码中的哪里读取FILEPATH列? You only read the first five columns (name, father_name, age, address, id) but nothing is read for the FILEPATH column. 您仅读取前五列(名称,父亲名称,年龄,地址,ID),但FILEPATH列未读取任何内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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