帮我编写一个Javaweb程序,要带有注释。编写程序,使用JavaBean和数据库编程实现显示商品列表的功能: 1、创建JavaBean实体类Product类; 2、创建数据库连接类ConSQL,实现数据库的连接和关闭,可以使用数据库连接池; 3、创建数据库操作类ProductDao,实现查询1条记录; 4、编写ShowServlet,从ProductDao中获取商品列表集合List,遍历集合,使用HttpResponse显示商品信息。
时间: 2024-02-22 20:56:21 浏览: 43
好的,以下是Javaweb程序的代码:
1. Product.java
```java
package com.example.bean;
public class Product {
private int id; // 商品ID
private String name; // 商品名称
private double price; // 商品价格
private String description; // 商品描述
// 构造方法
public Product(int id, String name, double price, String description) {
this.id = id;
this.name = name;
this.price = price;
this.description = description;
}
// getter和setter方法
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
```
2. ConSQL.java
```java
package com.example.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class ConSQL {
private static String driver = "com.mysql.cj.jdbc.Driver";
private static String url = "jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8";
private static String username = "root";
private static String password = "123456";
// 获取数据库连接
public static Connection getConnection() throws SQLException, ClassNotFoundException {
Class.forName(driver);
Connection conn = DriverManager.getConnection(url, username, password);
return conn;
}
// 关闭数据库连接
public static void close(Connection conn) throws SQLException {
if(conn != null) {
conn.close();
}
}
}
```
3. ProductDao.java
```java
package com.example.dao;
import com.example.bean.Product;
import com.example.util.ConSQL;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class ProductDao {
// 根据商品ID查询商品
public Product getProductById(int id) throws SQLException, ClassNotFoundException {
Connection conn = ConSQL.getConnection();
String sql = "select * from product where id = ?";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, id);
ResultSet rs = pstmt.executeQuery();
Product product = null;
while(rs.next()) {
product = new Product(rs.getInt("id"), rs.getString("name"), rs.getDouble("price"), rs.getString("description"));
}
rs.close();
pstmt.close();
ConSQL.close(conn);
return product;
}
}
```
4. ShowServlet.java
```java
package com.example.servlet;
import com.example.bean.Product;
import com.example.dao.ProductDao;
import com.example.util.ConSQL;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class ShowServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("text/html;charset=UTF-8");
PrintWriter out = resp.getWriter();
out.println("<html>");
out.println("<head>");
out.println("<title>商品列表</title>");
out.println("</head>");
out.println("<body>");
out.println("<h2>商品列表</h2>");
out.println("<table border='1'>");
out.println("<tr>");
out.println("<th>商品ID</th>");
out.println("<th>商品名称</th>");
out.println("<th>商品价格</th>");
out.println("<th>商品描述</th>");
out.println("</tr>");
try {
ProductDao dao = new ProductDao();
List<Product> productList = new ArrayList<>();
productList.add(dao.getProductById(1));
productList.add(dao.getProductById(2));
productList.add(dao.getProductById(3));
for (Product product : productList) {
out.println("<tr>");
out.println("<td>" + product.getId() + "</td>");
out.println("<td>" + product.getName() + "</td>");
out.println("<td>" + product.getPrice() + "</td>");
out.println("<td>" + product.getDescription() + "</td>");
out.println("</tr>");
}
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
out.println("</table>");
out.println("</body>");
out.println("</html>");
}
}
```
以上就是Javaweb程序的代码,其中包含了JavaBean和数据库编程实现显示商品列表的功能,同时也包含了注释,方便您理解代码。