el表达式的使用教程

接热心网友:内部静态类的要求,今天它来了,时尚高端,无人能敌,数据获取,快于荒谬。
首先我们进行项目数据库的创建,
在这里插入图片描述然后根据数据库写出实体类

package entity;

public class Entity {
    private Integer id;
    private String name;
    private String dept;

    public Entity() {
    }

    public Entity(Integer id, String name, String dept) {
        this.id = id;
        this.name = name;
        this.dept = dept;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDept() {
        return dept;
    }

    public void setDept(String dept) {
        this.dept = dept;
    }
}

配置数据库连接:用德鲁伊进行连接
首先写出配置文件druid.properties:

# 加载数据库驱动
driverClassName=com.mysql.jdbc.Driver
# 连接数据库的url,db1表示数据库名,useSSL=false表示不使用SSL规范
url=jdbc:mysql://127.0.0.1:3306/sunhill?useSSL=false&characterEncoding=UTF-8&serverTimezone=UTC
# 用户登录数据库的账号和密码
username=root
password=123
# 初始化连接数量
initialSize=5
# 最大连接数量
maxActive=10
# 最大等待时间
maxWait=3000

创建数据库封装连接工具类,增加连接安全性。

package JdbcUtils;

import com.alibaba.druid.pool.DruidDataSourceFactory;

import javax.sql.DataSource;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

public class DataSourceUtils {
    /**
     * DataSource 数据库连接池对象
     */
    private static DataSource dataSource;

    /**
     * 私有化构造方法
     */
    private DataSourceUtils() {
    }

    /**
     * 使用静态代码块加载配置文件
     */
    static {
        try {
            // 读取配置文件
            Properties properties = new Properties();
            InputStream resourceAsStream = DataSourceUtils.class.getClassLoader().getResourceAsStream("druid.properties");
            properties.load(resourceAsStream);
            // 获取数据库连接池
            dataSource = DruidDataSourceFactory.createDataSource(properties);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 封装获取连接方法
     *
     * @return
     * @throws SQLException
     */
    public static Connection getConnection() {
        try {
            return dataSource.getConnection();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 封装关闭连接方法
     */
    public static void close(Connection connection, Statement statement, ResultSet resultSet) {
        if (connection != null) {
            try {
                connection.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if (statement != null) {
            try {
                statement.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if (resultSet != null) {
            try {
                resultSet.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }
}

然后进行Dao层的书写:

package Dao;

import JdbcUtils.DataSourceUtils;
import entity.Entity;


import java.sql.*;
import java.util.ArrayList;
import java.util.List;

import static JdbcUtils.DataSourceUtils.*;
import static com.alibaba.druid.util.JdbcUtils.close;

public class Dao {
    Connection connection = null;
    ResultSet resultset = null;
    PreparedStatement preparedStatement = null;
    public List<Entity> TaogetAll() throws SQLException {

        try {
            connection = getConnection();
            //获取编译者执行者对象,防止sql语句注入的问题
            preparedStatement = connection.prepareStatement("select * from Taotianci");
            //执行我们的sql语句
            resultset = preparedStatement.executeQuery();
            ArrayList<Entity> entities = new ArrayList<>();
            while (resultset.next()) {
                //获取数据库中的信息
                Integer id = resultset.getInt("id");
                String name = resultset.getString("name");
                String dept = resultset.getString("dept");
                Entity entity = new Entity(id, name, dept);
                entities.add(entity);
                System.out.println(entities);

            }
            return entities;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        } finally {
            // 释放资源
         resultset.close();
         preparedStatement.close();
         connection.close();
        }
    }
}

然后进行Service层:

package Service;

import Dao.Dao;
import entity.Entity;

import java.sql.SQLException;
import java.util.List;

public class Service {
  public   static Dao dao=new Dao();

    public  List<Entity> TaogetAll() throws SQLException {
        return dao.TaogetAll();
    }
}

然后进行Servlet层的书写

package Servlet;

import Service.Service;
import entity.Entity;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;
@WebServlet("/tao")
public class Show extends HttpServlet {
    private Service Service = new Service();
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<Entity> Entitys= null;

        try {
            Entitys = Service.TaogetAll();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        req.setAttribute("people", Entitys);
        req.getRequestDispatcher("tao.jsp").forward(req,resp);
    }


    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<Entity> Entitys= null;

        try {
            Entitys = Service.TaogetAll();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        req.setAttribute("people", Entitys);
        req.getRequestDispatcher("tao.jsp").forward(req,resp);
    }
}

然后进入重点tao.jsp界面

<%--
  Created by IntelliJ IDEA.
  User: qinhaidong
  Date: 2022/10/22
  Time: 12:27
  To change this template use File | Settings | File Templates.
--%>
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core_rt" %>
<%@ page isELIgnored="false"  %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<head>
    <title>展示界面</title>
</head>
<body>
<table>
    <tr>
        <th align="center">编号</th>
        <th align="center">姓名</th>
        <th align="center">系别</th>
    </tr>
    <c:forEach items="${people}" var="p">
        <tr>
            <th align="center">${p.id}</th>
            <th align="center">${p.name}</th>
            <th align="center">${p.dept}</th>
        </tr>
    </c:forEach>
</table>
</body>
</html>


注意看里面我们先写出使用c标签的指令<%@ taglib prefix=“c” uri=“http://java.sun.com/jstl/core_rt” %>,同时我们也要导入相应的 taglib 指令jar包。
在这里插入图片描述
然后我们在里面进行 <c:forEach items="${people}" var="p"> <tr> <th align="center">${p.id}</th> <th align="center">${p.name}</th> <th align="center">${p.dept}</th> </tr> </c:forEach>
这个的书写,记住item里面的 p e o p l e 是 S e r v l e t 里面的一定要进行对应:然后就可以进行这个声明 v a r = ′ p ′ 然后下面的东西就可以进行 e l 表达式获取数据库里面的属性 ‘ < t h a l i g n = " c e n t e r " > {people}是Servlet里面的一定要进行对应: 然后就可以进行这个声明var='p' 然后下面的东西就可以进行el表达式获取数据库里面的属性` <th align="center"> peopleServlet里面的一定要进行对应:然后就可以进行这个声明var=p然后下面的东西就可以进行el表达式获取数据库里面的属性<thalign="center">{p.id}
p . n a m e < / t h > < t h a l i g n = " c e n t e r " > {p.name}</th> <th align="center"> p.name</th><thalign="center">{p.dept}`
看获取成功图
在这里插入图片描述

       大家感觉怎摸样呢,el表达式是不是很简单啊!
       小伙伴们快去和ttc去试一试把!
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值