jsp实现商品种类的浏览
时间: 2023-08-26 13:05:36 浏览: 37
要实现商品种类的浏览,可以使用JSP中的SQL语句查询数据库中的商品种类信息,并将其展示出来。
首先,在数据库中创建商品种类表,例如:
```
CREATE TABLE product_category (
category_id INT PRIMARY KEY AUTO_INCREMENT,
category_name VARCHAR(50) NOT NULL
);
```
然后,插入一些商品种类数据,例如:
```
INSERT INTO product_category (category_name) VALUES ('手机');
INSERT INTO product_category (category_name) VALUES ('电脑');
INSERT INTO product_category (category_name) VALUES ('家电');
```
接着,在JSP页面中,使用JSP代码连接数据库并查询商品种类信息。示例代码如下:
```
<%@ page import="java.sql.*" %>
<%
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
try {
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/test";
String user = "root";
String password = "123456";
conn = DriverManager.getConnection(url, user, password);
stmt = conn.prepareStatement("SELECT * FROM product_category");
rs = stmt.executeQuery();
// 遍历查询结果并展示
while (rs.next()) {
int categoryId = rs.getInt("category_id");
String categoryName = rs.getString("category_name");
out.println("<a href='productList.jsp?categoryId=" + categoryId + "'>" + categoryName + "</a>");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
%>
```
上述代码中,首先使用JDBC连接数据库,然后执行SQL语句查询商品种类信息。对于每个查询结果,使用JSP代码生成一个超链接,链接到商品列表页面,并传递商品种类ID作为参数。
最后,在商品列表页面中,接收并解析商品种类ID参数,并根据该参数查询对应的商品信息。示例代码如下:
```
<%@ page import="java.sql.*" %>
<%
int categoryId = Integer.parseInt(request.getParameter("categoryId"));
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
try {
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/test";
String user = "root";
String password = "123456";
conn = DriverManager.getConnection(url, user, password);
stmt = conn.prepareStatement("SELECT * FROM product WHERE category_id = ?");
stmt.setInt(1, categoryId);
rs = stmt.executeQuery();
// 遍历查询结果并展示
while (rs.next()) {
int productId = rs.getInt("product_id");
String productName = rs.getString("product_name");
double productPrice = rs.getDouble("product_price");
String productImage = rs.getString("product_image");
out.println("<div>");
out.println("<a href='productDetail.jsp?productId=" + productId + "'>");
out.println("<img src='" + productImage + "' alt='" + productName + "'>");
out.println("<span>" + productName + "</span>");
out.println("<span>" + productPrice + "</span>");
out.println("</a>");
out.println("</div>");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
%>
```
上述代码中,首先获取到传递的商品种类ID参数,然后执行SQL语句查询该种类下的商品信息。对于每个查询结果,使用JSP代码生成一个商品展示框,并链接到商品详情页面,传递商品ID作为参数。
以上就是实现商品种类浏览功能的JSP代码示例。需要注意的是,为了保证用户隐私安全,可能需要对数据库连接信息进行加密处理。