jsp显示数据库中的随机行数 [英] jsp display random amount of rows from database

查看:147
本文介绍了jsp显示数据库中的随机行数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用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.

推荐答案

您可以使用joinsnested 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆