IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> JavaScript知识库 -> 基于jsp+javabean+servlet模式下开发网上图书管理系统 -> 正文阅读

[JavaScript知识库]基于jsp+javabean+servlet模式下开发网上图书管理系统

由于本人能力有限,对javabean类的封装以及系统功能的实现并不完善,本案例仅实现了对图书系统的简单增删改查,仅供参考。原创不易,转载请注明来源。

Login.jsp--------登录页面

<%--
  Created by IntelliJ IDEA.
  User: user
  Date: 2021/12/11
  Time: 15:47
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>login</title>
    <style>
        .div1{
            margin: 0 auto;
            border: 1px solid red;
            text-align: center;
           width: 300px;
            height: 300px;
            background-color: skyblue;
        }
    </style>
</head>

<body>
<h1 style="text-align: center">学生图书管理系统</h1>
<form action="LiServlet" method="post" class="div1">

    <table>
        <tr>
            <td>用户:</td>
            <td><input type="text" name="username"></td>
        </tr>
        <tr>
            <td>密码:</td>
            <td><input type="password" name="password"></td>
        </tr>
        <tr>
            <td><input type="submit"></td>
            <td><input type="reset"></td>
        </tr>
    </table>
</form>
</body>
</html>

index1.jsp------主页面

<%--
  Created by IntelliJ IDEA.
  User: 渡璟
  Date: 2021/12/11
  Time: 23:15
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>主页面</title>
    <style>
        .div1{
            margin: 0 auto;
            border: 1px solid red;
            height: 700px;
            width: 70%;
            background-color: skyblue;
            text-align: center;
        }
        a{
            font-size: 30px;
        }
    </style>
</head>
<body>
<div class="div1">
<h1>欢迎用户:<%=request.getAttribute("info")%></h1>
    <p>请点击以下选项来对管理系统进行相应操作</p>
<a href="tianjia.jsp">添加</a>
<br>
<a href="shanchu.jsp">删除</a>
<br>
    <a href="select.jsp">查询</a>
    <br>
    <a href="update.jsp">修改</a>
</div>
</body>
</html>

Shanchu.jsp-------删除页面

<%--
  Created by IntelliJ IDEA.
  User: 渡璟
  Date: 2021/12/11
  Time: 23:21
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>删除页面</title>
</head>
<body>
<%request.setCharacterEncoding("UTF-8");%>
<form action="DiServlet" method="post">

<span>请输入要删除的书名:</span>
<input type="text" name="d1" autofocus>
    <input type="submit">
    <input type="reset">
</form>
<%@include file="/select.jsp"%>

</body>
</html>

Diservlet---------删除页面程序

package servlets;

import classes.Book;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet(name = "DiServlet", value = "/DiServlet")
public class DiServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        String d1=request.getParameter("d1");
        Book book=new Book();
        String result=book.bookDel(d1);
        request.setAttribute("result",result);
//        request.setAttribute("name",d1);
        request.getRequestDispatcher("/index3.jsp").forward(request,response);
    }
}

Tianjia.Jsp--------添加页面

<%--
  Created by IntelliJ IDEA.
  User: 渡璟
  Date: 2021/12/11
  Time: 23:21
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>添加页面</title>
</head>
<body>
<h1>请输入要插入的信息</h1>
<%  request.setCharacterEncoding("UTF-8");%>
<form action="HiServlet" method="post">
    <table>
        <tr>
            <td>请输入图书名:</td>
            <td><input type="text" name="d1"></td>
        </tr>
        <tr>
            <td>请输入图书作者:</td>
            <td><input type="text" name="d2" ></td>
        </tr>
        <tr>
            <td>请输入图书价格:</td>
            <td><input type="text" name="d3" placeholder="单价:元"></td>
        </tr>
        <tr>
            <td><input type="submit" value="添加"></td>
            <td><input type="reset" value="取消"></td>
        </tr>
    </table>
</form>
<%@include file="/select.jsp"%>
</body>
</html>

HiServlet-------添加页面程序

package servlets;

import classes.Book;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet(name = "HiServlet", value = "/HiServlet")
public class HiServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
          doPost(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
         String d1=request.getParameter("d1");
         String d2=request.getParameter("d2");
         String d3=request.getParameter("d3");
         Book book=new Book();
         String result=book.bookAdd(d1,d2,d3);
         request.setAttribute("result",result);
         request.getRequestDispatcher("/index2.jsp").forward(request,response);

    }
}

select.jsp--------查询页面

<%@ page import="classes.Book" %>
<%@ page import="java.sql.ResultSet" %>
<%@ page import="java.sql.Connection" %>
<%@ page import="classes.DBconnect" %>
<%@ page import="java.sql.PreparedStatement" %><%--
  Created by IntelliJ IDEA.
  User: 渡璟
  Date: 2021/12/12
  Time: 22:22
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>select</title>
    <style>
        .div2{
            border: 1px solid gray;
            background-color: skyblue;
            width: 100%;
            margin: 0 auto;
        }
    </style>
</head>
<body>

<%
 request.setCharacterEncoding("UTF-8");
        System.out.println("数据库连接成功!!!");
        Connection conn = DBconnect.getConnect();
        String sql="select * from book_info";
        PreparedStatement st=conn.prepareStatement(sql);
        ResultSet rs=st.executeQuery();
%>
<h1 style="text-align: center">欢迎查看图书目录</h1>
<form>
    <table class="div2">
        <tr>
            <td>编号</td>
            <td>书名</td>
            <td>作者</td>
            <td>价格</td>
        </tr>
        <% rs.beforeFirst();
           while (rs.next())
           {
        %>
        <tr>
            <td><%=rs.getInt("id")%></td>
            <td><%=rs.getString("name")%></td>
            <td><%=rs.getString("author")%></td>
            <td><%=rs.getString("price")%> <span>元/本</span></td>
        </tr>
        <% }
        %>
    </table>
</form>
<%
    if(rs!=null){
        rs.close();
    }
    if(st!=null){
        st.close();
    }
    if(conn!=null){
        conn.close();
    }
%>
</body>
</html>

Update.jsp------更改页面

<%--
  Created by IntelliJ IDEA.
  User: 渡璟
  Date: 2021/12/13
  Time: 20:03
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>update</title>
</head>
<body>
<% request.setCharacterEncoding("UTF-8");%>
<form action="UiServlet" method="post">
    <table>
        <tr>
            <td>请输入要修改的编号:</td>
            <td><input type="text" name="d1"></td>
        </tr>
        <tr>
            <td>请输入要修改的书名:</td>
            <td><input type="text" name="d2"></td>
        </tr>
        <tr>
            <td>请输入要修改的作者:</td>
            <td><input type="text" name="d3"></td>
        </tr>
        <tr>
            <td>请输入要修改的价格:</td>
            <td><input type="text" name="d4"></td>
        </tr>
        <tr>
            <td><input type="submit"></td>
            <td><input type="reset"></td>
        </tr>
    </table>
</form>
<%@include file="/select.jsp"%>
</body>
</html>

UiServlet----更改页面程序

package servlets;

import classes.Book;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet(name = "UiServlet", value = "/UiServlet")
public class UiServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
          doPost(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
          request.setCharacterEncoding("UTF-8");
          int d1=Integer.parseInt(request.getParameter("d1"));
          String d2=request.getParameter("d2");
          String d3=request.getParameter("d3");
          String d4=request.getParameter("d4");
          Book book=new Book();
          String result=book.bookUpd(d1,d2,d3,d4);
          request.setAttribute("result",result);
          request.getRequestDispatcher("/index2.jsp").forward(request,response);
    }
}

Javabean封装的类

  1. 数据库连接类
    package classes;
    
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.Statement;
    
    public class DBconnect {
        public static Connection getConnect() throws Exception{
            String driver="com.mysql.jdbc.Driver";
            String user="root";
            String password="123456";
            String dbName="student";
            String url1="jdbc:mysql://localhost:3306/"+dbName;
            String url2="?user="+user+"&password="+password;
            String url3="&useUnicode=true&characterEncoding=UTF-8";
            String url=url1+url2+url3;
            Class.forName(driver);
            Connection conn= DriverManager.getConnection(url);
            return conn;
        }
        public static void free(ResultSet rs, Statement st,Connection conn)throws Exception{
            if(rs!=null){
                rs.close();
            }
            if(st!=null){
                st.close();
            }
            if(conn!=null){
                conn.close();
            }
            System.out.println("即将断开数据库连接!!!");
        }
    }
  2. 用户登录验证类
  3. package classes;
    
    import java.sql.Connection;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.Statement;
    
    public class User {
        private String userName;
        private String userPwd;
        private int a=0;
        public User(String userName,String userPwd){
            this.userName=userName;
            this.userPwd=userPwd;
        }
    
        public int login()  {
            try {
                System.out.println("数据库连接成功!!!");
                Connection conn = DBconnect.getConnect();
                String sql="select * from login_info where user = ? and password = ? ";
                PreparedStatement st=conn.prepareStatement(sql);
                st.setString(1,this.userName);
                st.setString(2,this.userPwd);
                ResultSet rs=st.executeQuery();
                if(rs.next())
                  this.a=1;
                DBconnect.free(rs,st,conn);
            }catch (Exception e){
                System.out.println(e);
    
            }
             return this.a;
        }
    
    
    }

?

3.增删改查类

package classes;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;

public class Book {
  private   ResultSet rs;
  private  Statement ps;
  private   Connection conn;
    public String bookAdd(String a1,String a2,String a3){
        try {
            System.out.println("数据库连接成功!!!");
            Connection conn = DBconnect.getConnect();
            String sql="insert  into book_info (name,author,price) value(?,?,?)";
            PreparedStatement st=conn.prepareStatement(sql);
            st.setString(1,a1);
            st.setString(2,a2);
            st.setString(3,a3);
            st.executeUpdate();
            DBconnect.free(null,st,conn);
            return "添加成功!!!";
        }catch (Exception e){
            System.out.println(e);
             return "添加操作失败!!!请检查连接重试";
        }
    }
    public String bookDel(String name){
        try {
            System.out.println("数据库连接成功!!!");
            Connection conn = DBconnect.getConnect();
            String sql="delete from book_info where name = ?";
            PreparedStatement st=conn.prepareStatement(sql);
            st.setString(1,name);
           int i = st.executeUpdate();
            DBconnect.free(null,st,conn);
            if(i==1)
            return "删除成功!!!";
            else
                return "删除操作失败!!!请检查连接重试";
        }catch (Exception e){
            return "删除操作失败!!!请检查连接重试";
        }

    }
    public String bookUpd(int d1,String d2,String d3,String d4){
        try {
            System.out.println("数据库连接成功!!!");
            Connection conn = DBconnect.getConnect();
            String sql="update book_info set name=?,author=?,price=? where id=?";
            PreparedStatement st=conn.prepareStatement(sql);
            st.setString(1,d2);
            st.setString(2,d3);
            st.setString(3,d4);
            st.setInt(4,d1);
            int i = st.executeUpdate();
            DBconnect.free(null,st,conn);
            if(i==1)
                return "修改成功!!!";
            else
                return "修改操作失败!!!请检查连接重试";
        }catch (Exception e){
            return "修改操作失败!!!请检查连接重试";
        }
    }
}

提示页面

<%--
  Created by IntelliJ IDEA.
  User: 渡璟
  Date: 2021/12/12
  Time: 21:53
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>?????</title>
</head>
<body>
<%=request.getAttribute("result")%>
</body>
</html>
<%--
  Created by IntelliJ IDEA.
  User: 渡璟
  Date: 2021/12/13
  Time: 17:29
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>删除详情</title>
</head>
<body>
<%--<%=request.getAttribute("name")%>--%>
<%=request.getAttribute("result")%>
</body>
</html>
<%--
  Created by IntelliJ IDEA.
  User: user
  Date: 2021/12/11
  Time: 17:08
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>input</title>
    <style>
        .div1{
            margin: 0 auto;
            border: 1px solid red;
            width: 100px;
            height: 100px;
            background-color: skyblue;
        }
    </style>
</head>
<body>
<div class="div1">
<h1>账号或密码错误!!!请重新登录</h1>


<a href="login.jsp">重新登录</a>
</div>
</body>
</html>

?

?

?

?

?

?

?

?

?

?

?

?

?

  JavaScript知识库 最新文章
ES6的相关知识点
react 函数式组件 & react其他一些总结
Vue基础超详细
前端JS也可以连点成线(Vue中运用 AntVG6)
Vue事件处理的基本使用
Vue后台项目的记录 (一)
前后端分离vue跨域,devServer配置proxy代理
TypeScript
初识vuex
vue项目安装包指令收集
上一篇文章      下一篇文章      查看所有文章
加:2021-12-14 15:50:58  更:2021-12-14 15:52:58 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/28 3:20:13-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码