每日总结 5.12

今日进行了web实验。对于之前所学习的增删改查进行熟练学习。

1)     开MySQL,新建一个数据库。

2)     新建一个数据库表。

3)     在表中增加若干记录,作为初始数据。

4)     打开Eclipse软件,新建一个名为Lab03的Web项目,并设置其部署程序为Tomcat。

5)     在Lab03中添加文件,编写代码。

6)     Index.jsp文件代码

<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

    <%@ page import="java.sql.*" %>

<!DOCTYPE html>

<html>

<head>

<title>图书管理系统</title>

   <meta charset="UTF-8">

    <script th:src="@{/layuimini/js/lay-module/echarts/echarts.js}"></script>

    <script th:src="@{/layuimini/js/lay-module/echarts/wordcloud.js}"></script>

    <link rel="stylesheet" type="text/css" href="https://www.layuicdn.com/layui/css/layui.css" />

    <script src="https://www.layuicdn.com/layui/layui.js"></script>

</head>

<body>

<center><h1><center>图书管理系统</h1></center>

<center><ahref="add.jsp">添加图书</a></center>

 

<table class="layui-table"style="table-layout: fixed;word-wrap:break-word;" >

    <thead>

    <tr>

        <td style="width: 150px">名称</td>

        <td style="width: 70px">作者</td>

        <td style="width: 80px">价格</td>

        <td style="width: 50px">操作</td>

    </tr>

    </thead>

    <tbody>

    <%

Connection connection;

String url="jdbc:mysql://localhost:3306/teach?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC";

String user="root";

String pass="020907";

Class.forName("com.mysql.cj.jdbc.Driver");

connection=DriverManager.getConnection(url,user,pass);

PreparedStatement pre=null;

ResultSet res=null;

String sql="SELECT *FROM book ";

pre=connection.prepareStatement(sql);

res=pre.executeQuery();

while(res.next()) {

   int id=res.getInt("id");

%>

    <tr>

        <td style="width: 150px"><%=res.getString("name")%></td>

        <td style="width: 70px"><%=res.getString("tname")%></td>

        <td style="width: 80px"><%=res.getString("price")%>元</td>

        <td style="width: 50px"><a href='edit.jsp?id=<%=id%>'>修改</a>&nbsp;<a href='del.jsp?id=<%=id%>'>删除</a></td>

    </tr>

    <%}

   connection.close();

   pre.close();

   res.close();

    %>

    </tbody>

</table>

</body>

</html>

……

7)     Add.jsp文件代码

<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

    <%@ page import="java.sql.*" %>

<!DOCTYPE html>

<html>

<head>

<title>添加图书</title>

   <meta charset="UTF-8">

    <script th:src="@{/layuimini/js/lay-module/echarts/echarts.js}"></script>

    <script th:src="@{/layuimini/js/lay-module/echarts/wordcloud.js}"></script>

    <link rel="stylesheet" type="text/css" href="https://www.layuicdn.com/layui/css/layui.css" />

    <script src="https://www.layuicdn.com/layui/layui.js"></script>

</head>

<body>

<center><h1><center>添加图书</h1></center>

 

<table class="layui-table"style="table-layout: fixed;word-wrap:break-word;" >

    <tbody>

     <formaction="addsave.jsp"method="post">

    <tr>

        <td style="width: 50px">图书名称</td>

        <td style="width: 70px"><input type="text" name="shuname"/></td>

    </tr>

    <tr>

        <td style="width: 150px">作者</td>

        <td style="width: 70px"><input type="text" name="zuozhename"/></td>

    </tr>

    <tr>

        <td style="width: 150px">价格</td>

        <td style="width: 70px"><input type="text" name="price"/></td>

    </tr>

    <tr>

        <td style="width: 150px"> <input type="submit" value="提交">&nbsp;<input type="reset" value="重置"></td>

    </tr>

    </tbody>

</table>

</body>

</html>

8)     Addsave.jsp文件代码

<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

    <%@ page import="java.sql.*" %>

<!DOCTYPE html>

<html>

<head>

<title>图书管理系统</title>

   <meta charset="UTF-8">

</head>

<body>

    <%

    String name1=request.getParameter("shuname");

    String name2=request.getParameter("zuozhename");

    String price=request.getParameter("price");

Connection connection;

String url="jdbc:mysql://localhost:3306/teach?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC";

String user="root";

String pass="020907";

Class.forName("com.mysql.cj.jdbc.Driver");

connection=DriverManager.getConnection(url,user,pass);

ResultSet res=null;

String sql="insert into book (name,tname,price) values (?,?,?) ";

   PreparedStatement ps = connection.prepareStatement(sql);

    ps.setString(1, name1);

    ps.setString(2,name2);

    ps.setString(3, price);

    ps.executeUpdate();

     connection.close();

     ps.close();

     out.println("添加完成");

     out.println("<a href='index.jsp'>返回首页</a>");

%>

 

</body>

</html>

9)     del.jsp文件代码

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>

    <%@ page import="java.sql.*" %>

<!DOCTYPE html>

<html>

<head>

<title>图书管理系统</title>

   <meta charset="UTF-8">

</head>

<body>

    <%

Connection connection;

String url="jdbc:mysql://localhost:3306/teach?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC";

String user="root";

String pass="020907";

Class.forName("com.mysql.cj.jdbc.Driver");

connection=DriverManager.getConnection(url,user,pass);

String id=request.getParameter("id");

String sql="delete from book where id=?";

   PreparedStatement ps = connection.prepareStatement(sql);

   ps.setString(1,id);

       int i= ps.executeUpdate();

 

     if(i==1){

        out.println("<h1>删除成功</h1>");

     }else{

        out.println("<h1>删除失败</h1>");

     }

     connection.close();

     ps.close();

     out.println("<a href='index.jsp'>返回首页</a>");

%>

 

</body>

</html>

posted @   一个小虎牙  阅读(9)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
点击右上角即可分享
微信分享提示