jsp显示数据库中的随机行数 [英] jsp display random amount of rows from database
问题描述
我正在使用JSP和Servlets创建Web应用程序,但遇到了这个问题:
我有一个表users
和一个myeducations
表.这些表关系在user_id上建立.
I'm making a web-application with JSP and Servlets and I came accross this problem:
I have a table users
and a myeducations
table. These table relationship is established on user_id.
这是表格:
自我教育
----------------------------------------------------------------------------------
| ed_id | user_id | school_name | year_attended_from | year_attended_to | degree |
----------------------------------------------------------------------------------
现在,我想在网站(jsp)上显示所有用户的教育程度,但是用户接受的教育程度是随机的:1个用户可能只完成了1所学校,其他用户可能已经完成3所学校,依此类推...
Now, I want to display on a website (jsp) all of the user educations, but the number of educations user has is random: 1 user could have finished just 1 school, other could 3 and so on...
我了解如何只检索1行,SQL是:
I understand how to retrieve just 1 row, the SQL is:
SELECT school_name
FROM myeducation
INNER JOIN users ON users.user_id = myeducation.user_id;
但是如何使该语句循环运行,以便让我了解用户的所有知识,比如说他是否拥有超过1的知识.然后将它们全部放入会话变量中并显示在jsp的div中. /p>
But how to make this statement run in a loop, so that I get all the educations of a user, say if he has more than just 1. Then put them all in a session variable and display in a jsp's div.
推荐答案
您可以使用joins
或nested Query
根据您当前的数据库设计,您可以通过一个简单的查询本身来实现此目的,因为Education表包含USER_ID
As per your current DB Design, you can achieve this by a simple query itself, Since Education table holds the USER_ID
SELECT school_name FROM myeducation myedu where user_id="SOME_ID_HERE"
要在servlet中循环字符串数组,只需这样做
For looping String Array in servlet, simply do like this
ArrayList<String> arr = new ArrayList<String>();
while (rs.next()) {
arr.add(rs.getString("school_name "));
}
这篇关于jsp显示数据库中的随机行数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!