欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

layui表格数据与后台数据连接,javaweb

程序员文章站 2022-06-19 13:02:24
仅上传代码部分以下图示部分可视化界面admingonggao.jsp<%@ page contentType="text/html;charset=UTF-8" language="java" %><%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%><%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>....

仅上传代码部分

以下图示部分可视化界面

layui表格数据与后台数据连接,javaweb

 admingonggao.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<%@ page isELIgnored="false" %>
<%@ taglib uri="http://java.fckeditor.net" prefix="FCK"%>

<!DOCTYPE html>
<html class="x-admin-sm">
<head>
    <meta charset="UTF-8">
    <title>公告管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />
    <link rel="stylesheet" href="css/font1.css">
    <link rel="stylesheet" href="css/admin1.css">
    <script src="layui/layui.js" charset="utf-8"></script>
    <script type="text/javascript" src="js/admin1.js"></script>
    <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
    <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
</head>
<body>

<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-header">
                    <button class="layui-btn" onclick="xadmin.open('添加公告','gonggao-add.jsp',600,400)"><i class="layui-icon"></i>添加</button>
                </div>
                <div class="layui-card-body ">
                    <table class="layui-table layui-form">
                        <thead>
                            <tr>
                                <th>ID</th>
                                <th>标题</th>
                                <th>内容</th>
                                <th>时间</th>
                                <th>操作</th>
                            </tr>
                        </thead>
                        <tbody>

                            <c:forEach items="${requestScope.gonggaoList}" var="gonggao">
                            <tr>
                                <td>${gonggao.id}</td>
                                <td>${gonggao.title}</td>
                                <td>${gonggao.content}</td>
                                <td>${gonggao.shijian}</td>
                                <td class="td-manage">

                                    <a title="修改" href="admingonggaoupdateServlet?id=${gonggao.id}">
                                        <i class="layui-icon">&#xe642;</i>
                                    </a>
                                    <a title="删除"  href="admingonggaodeleteServlet?id=${gonggao.id}">
                                        <i class="layui-icon">&#xe640;</i>
                                    </a>
                                </td>
                            </tr>
                            </c:forEach>
                        </tr>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>
</body>
</html>

 gonggaodao.java

package com.dao;

import com.orm.Tgonggao;

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

public class gonggaodao {
    Connection conn = null;
    PreparedStatement pstmt = null;
    ResultSet res = null;

    private Connection getConn() {

        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/db_wljx?&useUnicode=true&characterEncoding=UTF-8", "root", "123456");
        } catch (SQLException | ClassNotFoundException e) {
            // TODO: handle exception
            e.printStackTrace();
        }
        return conn;
    }
    public List findgonggao () throws SQLException {
        List gonggaoList = new ArrayList();
        conn = getConn();
        String sql = "select * from t_gonggao ";
        pstmt = conn.prepareStatement(sql);
        try{
            res = pstmt.executeQuery();
            while (res.next()) {
                Tgonggao tgonggao = new Tgonggao();
                tgonggao.setId(res.getString("id"));
                tgonggao.setTitle(res.getString("title"));
                tgonggao.setContent(res.getString("content"));
                tgonggao.setShijian(res.getString("shijian"));
                gonggaoList.add(tgonggao);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        res.close();
        pstmt.close();
        return gonggaoList;
    }

    public Tgonggao findgonggaobyid (String id) throws SQLException {
        Tgonggao tgonggao = new Tgonggao();
        try {
            conn = getConn();
            String sql = "select * from t_gonggao where id=?";

            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, id);
            res = pstmt.executeQuery();
            while (res.next()) {
                tgonggao.setId(res.getString("id"));
                tgonggao.setTitle(res.getString("title"));
                tgonggao.setContent(res.getString("content"));
                tgonggao.setShijian(res.getString("shijian"));
            }
        } catch (SQLException e) {
            // TODO: handle exception
            e.printStackTrace();
        }

        return tgonggao;
    }

    public Tgonggao updategonggaobyid (String id,String title,String content,String shijian) throws SQLException {
        Tgonggao tgonggao = new Tgonggao();
        try {
            conn = getConn();
            String sql = "UPDATE t_gonggao  set title = ? , content = ?, shijian = ? where id=?";

            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, title);
            pstmt.setString(2, content);
            pstmt.setString(3, shijian);
            pstmt.setString(4, id);
            pstmt.executeUpdate();
        } catch (SQLException e) {
            // TODO: handle exception
            e.printStackTrace();
        }

        return tgonggao;
    }

    public void deletegonggaobyid (String id) throws SQLException {
        Tgonggao tgonggao = new Tgonggao();
        try {
            conn = getConn();
            String sql = "delete from t_gonggao where id=?";

            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, id);
            pstmt.execute();
        } catch (SQLException e) {
            // TODO: handle exception
            e.printStackTrace();
        }
    }

    public Tgonggao addgonggao (String id,String title,String content,String shijian) throws SQLException {
        Tgonggao tgonggao = new Tgonggao();
        try {
            conn = getConn();
            String sql = "insert into t_gonggao value(?,?,?,?)";

            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, id);
            pstmt.setString(2, title);
            pstmt.setString(3, content);
            pstmt.setString(4, shijian);
            pstmt.execute();
        } catch (SQLException e) {
            // TODO: handle exception
            e.printStackTrace();
        }

        return tgonggao;
    }
}

Tgonggao.java 

package com.orm;

public class Tgonggao {
    private String id;
    private String title;
    private String content;
    private String shijian;
    @Override
    public String toString() {
        return "Tgonggao{" +
                "id='" + id + '\'' +
                ", title='" + title + '\'' +
                ", content='" + content + '\'' +
                ", shijian='" + shijian + '\'' +
                '}';
    }



    public String getId() {
        return id;
    }

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

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public String getShijian() {
        return shijian;
    }

    public void setShijian(String shijian) {
        this.shijian = shijian;
    }



}

 其余servlet代码:https://download.csdn.net/download/katherinejing77/12655631

 layui表格数据与后台数据连接,javaweb

 

 

本文地址:https://blog.csdn.net/katherinejing77/article/details/107542749

相关标签: 前端 jsp