简体   繁体   English

使用主键在jsp中显示mysql数据库中的所有数据

[英]Display all data from mysql database in a jsp using primary key

I am creating an e-commerce project as part of my studies. 我正在研究中创建一个电子商务项目。 I want to display all products in a category on same JSP page. 我想在同一JSP页面上显示一个类别中的所有产品。 For example, if the user clicks the "Laptop" category, I have to display all products available in that category. 例如,如果用户单击“笔记本电脑”类别,则必须显示该类别中的所有可用产品。 All products for all categories are together in one large table. 所有类别的所有产品都放在一个大表中。

My database scheme is: 我的数据库方案是:

create table products(product_id varchar(20),
product_name varchar(20), 
product_prize varchar(20),
product_category varchar(20),
primary key(product_id) );

My primary key is product_id . 我的主键是product_id However, my current implementation needs separate JSP pages for each individual product_id . 但是,我当前的实现需要为每个单独的product_id单独的JSP页面。 This isn't going to be practical when I have many products. 当我有很多产品时,这将不切实际。

An example of a category page, lapcategory.jsp : 类别页面的示例lapcategory.jsp

<% int sec1=1;
   int sec2=2;
   int sec3=3;
   int sec4=4;
   int sec5=5;
   int sec6=6;
%>

<td>
  <center>
  <%if (sec1 ==1){ %>
    <jsp:include page="includesPage/_sec1.jsp"></jsp:include>
  <% } %> 
  </center>
</td>

<td>
  <center>
  <%if (sec2 ==2){ %>
    <jsp:include page="includesPage/_sec2.jsp"></jsp:include>
  <% } %>
  </center>
</td>

This links to individual section pages. 这链接到各个部分页面。 Then, I also have the section pages, _sec1.jsp , _sec2.jsp , etc. and I fetch data there using product_id . 然后,我还有_sec1.jsp_sec2.jsp等页面页面,并使用product_id在此处获取数据。 For example, _sec1.jsp : 例如, _sec1.jsp

String idn="20";

DB_Conn con = new DB_Conn();
Connection c = con.getConnection();
Statement statement = c.createStatement() ;
ResultSet resultset = statement.executeQuery("select product_id,product_name,product_desc,product_prize from product where product_id="+idn);

while(resultset.next()){ 

  String product_id = resultset.getString(1);
  String product_name = resultset.getString(2);
  String product_desc = resultset.getString(3);
  String product_prize = resultset.getString(4);
  %>

  <div id="se1">   
    <a href="product.jsp?id=<%=product_id%>"><% out.println(product_name); %></a><br>
    <a href="#">Rs.<% out.println(product_prize); %></a>

<%
} %>

</div>

Basically, I have to create individual inner pages for each product ID, which obviously isn't going to be easy to deal with. 基本上,我必须为每个产品ID创建单独的内部页面,这显然不容易处理。 My question is, what is the proper way to do this? 我的问题是,执行此操作的正确方法是什么? Something using ArrayList , maybe? 使用ArrayList东西,也许吗?

I understand your problem but i will suggest that instead of writing database query related code in jsp create one DAO , query the db and then set the result in one data object , data object will contain all the attributes that you need to display in JSP . 我理解您的问题,但我建议您不要在jsp中编写与数据库查询相关的代码,而是创建一个DAO,查询db,然后将结果设置在一个数据对象中,该数据对象将包含您需要在JSP中显示的所有属性。 Set the values in this Data object while reading it from db , then put that dataObject in one arrayList and populate that arrayList in Jsp's . 在从db中读取该Data对象时,设置其值,然后将该dataObject放在一个arrayList中,并将该arrayList填充到Jsp的中。 in this way you can display all the products with the help of one jsp and there is no need to create separate jsp's . 这样,您可以在一个jsp的帮助下显示所有产品,而无需创建单独的jsp。

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

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