JAVABean(实体类)

1、特定的写法

  • 必须要有一个无参构造
  • 属性必须私有化
  • 必须有对应的get/set方法
public class People {

    private int id;
    private String name;
    private int age;
    private String address;

    public People(int id, String name, int age, String address) {
        this.id = id;
        this.name = name;
        this.age = age;
        this.address = address;
    }

    public People() {
    }

    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 int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    @Override
    public String toString() {
        return "People{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                ", address='" + address + '\'' +
                '}';
    }
}

2、作用

 一般用来和数据库的字段做映射

ORM:对象关系映射

  • 表-->类
  • 字段(列)-->属性
  • 记录(行)-->对象

3、增加数据,得到数据

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<jsp:useBean id="people" class="com.xiaocong.jsp.People" scope="page"/>
<jsp:setProperty name="people" property="id" value="1"/>
<jsp:setProperty name="people" property="name" value="肖"/>
<jsp:setProperty name="people" property="age" value="18"/>
<jsp:setProperty name="people" property="address" value="吉安"/>
<%--<%-相当于:-%>
<%--    People people1 = new People();--%>
<%--    people1.setId(1);--%>
<%--    people1.setName("肖");--%>
<%--    people1.setAge(18);--%>
<%--    people1.setAddress("吉安");--%>
<%--%>--%>

ID:<jsp:getProperty name="people" property="id"/>
姓名:<jsp:getProperty name="people" property="name"/>
年龄:<jsp:getProperty name="people" property="age"/>
地址:<jsp:getProperty name="people" property="address"/>
<%--<%-相当于:-%>
<%--    people.getId();--%>
<%--    people.getName();--%>
<%--    people.getAge();--%>
<%--    people.getAddress();--%>
<%--%>--%>

</body>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
好的,以下是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和数据库编程实现显示商品列表的功能,同时也包含了注释,方便您理解代码。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

XC_2502476612

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值