jsp期末大作业

按照老师的要求,掌握数据库的连接,增删查改

大作业展示


大作业展示链接:https://www.bilibili.com/video/BV1f5411s7iy
源码链接:https://download.csdn.net/download/Mr_yuekitty/12473112

思路:
建个表格
实现增删查改
完事

显示界面

<%@ page contentType="text/html; charset=UTF-8" import="java.sql.*" %>

<html>
<head>
    
    <title>成绩</title>
</head>
<body>
<center>
    <h1>学生成绩表</h1>

    <hr width="80%">
    <a href="insert_page.jsp" style="font-size: 20px">添加信息</a>
    <table border="1" width="80%" align="center">
        <tr align="center">
            <th style="width: 20%"><b>学号</b></th>
            <th style="width: 20%"><b>姓名</b></th>
            <th style="width: 20%"><b>科目</b></th>
            <th style="width: 20%"><b>成绩</b></th>
            <th style="width: 20%"><b>删除</b></th>
            <th style="width: 20%"><b>更新</b></th>
        </tr>
        <form action="update.jsp">
            <%
                for (int i = 1; i <= 10; i++) {
                    if (!rs.next()) {
                        break;
                    }
                    int a = rs.getInt(1);
                    String b = rs.getString(2);
                    String c = rs.getString(3);
                    int d = rs.getInt(4);
            %>
            <tr align="center">
                <td style="width: 20%"><input name="id" value="<%=a%>"></td>
                <td style="width: 20%"><input type="text" name="name" value="<%=b%>"></td>
                <td style="width: 20%"><input type="text" name="course" value="<%=c%>"></td>
                <td style="width: 20%"><input type="text" name="score" value="<%=d%>"></td>
                <td style="width: 20%"><input type="button" value="删除" οnclick="del(<%=a%>)"></td>
                <td style="width: 20%"><input type="submit" value="更新"></td>
            </tr>
        </form>
        <%
            }
        %>

    </table>

</center>
<script>
    function del(x) {
        window.location.href = "delete.jsp?id=" + x;
    }
</script>
</body>
</html>



数据库的连接

<%
	String JDBC_DRIVER = "com.mysql.jdbc.Driver";
    String DB_URL = "jdbc:mysql://localhost:3306/test";
    String USER = "root";
    String PASS = "password";
%>

增删改查

增加的页面

<%@ page contentType="text/html; charset=UTF-8" import="java.sql.*" %>

<%
    String id = request.getParameter("id");
    String name = request.getParameter("name");
    String course = request.getParameter("course");
    String score = request.getParameter("score");


    String JDBC_DRIVER = "com.mysql.jdbc.Driver";
    String DB_URL = "jdbc:mysql://localhost:3306/test";
    String USER = "root";
    String PASS = "password";


    try {
        Class.forName(JDBC_DRIVER);
        Connection conn = DriverManager.getConnection(DB_URL, USER, PASS);
        String sql = "INSERT INTO test(id,name,course,score) value(?,?,?,?)";
        PreparedStatement psmt = conn.prepareStatement(sql);

        psmt.setInt(1, Integer.parseInt(id));
        psmt.setString(2, name);
        psmt.setString(3, course);
        psmt.setInt(4, Integer.parseInt(score));

        int rs = psmt.executeUpdate();
        conn.close();
        response.sendRedirect("index.jsp");
    } catch (ClassNotFoundException | SQLException e) {
        e.printStackTrace();
    }
%>

增加的实现

<%@ page contentType="text/html; charset=UTF-8" import="java.sql.*" %>
<html>
<head>
    <%
        String JDBC_DRIVER = "com.mysql.jdbc.Driver";
        String DB_URL = "jdbc:mysql://localhost:3306/test";
        String USER = "root";
        String PASS = "password";

        String sql = "SELECT max(id) FROM test";

        try {
            Class.forName(JDBC_DRIVER);
            Connection conn = DriverManager.getConnection(DB_URL, USER, PASS);
            PreparedStatement psmt = conn.prepareStatement(sql);

            ResultSet rs = psmt.executeQuery();
            rs.next();
            int id = rs.getInt(1) + 1;
            conn.close();

    %>
    <title>插入页面</title>
</head>
<body>
<center>
    <a>insert message</a>
    <hr>
    <form action="insert.jsp">
        <a>学号:<input name="id" type="text" value="<%=id%>"></a>
        <a>姓名:<input type="text" name="name"></a>
        <a>科目:<select name="course">
            <option>语文</option>
            <option>数学</option>
            <option>英语</option>
        </select>
        </a>
        <a>成绩:<input type="text" name="score"></a>
        <input type="submit" value="submit">
    </form>
</center>
<%
    } catch (ClassNotFoundException | SQLException e) {
        e.printStackTrace();
    }
%>
</body>
</html>

<%@ page contentType="text/html; charset=UTF-8" import="java.sql.*" %>

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

    String JDBC_DRIVER = "com.mysql.jdbc.Driver";
    String DB_URL = "jdbc:mysql://localhost:3306/test";
    String USER = "root";
    String PASS = "password";

    try {
        Class.forName(JDBC_DRIVER);
        Connection conn = DriverManager.getConnection(DB_URL, USER, PASS);
        String sql="DELETE FROM test WHERE id=?";

        PreparedStatement psmt=conn.prepareStatement(sql);
        psmt.setInt(1,Integer.parseInt(id));

        psmt.executeUpdate();
        conn.close();
        response.sendRedirect("index.jsp");
    } catch (ClassNotFoundException | SQLException e) {
        e.printStackTrace();
    }
%>

<%@ page contentType="text/html; charset=UTF-8" import="java.sql.*" %>

<%
    String id = request.getParameter("id");
    String name = request.getParameter("name");
    String course = request.getParameter("course");
    String score = request.getParameter("score");

    String JDBC_DRIVER = "com.mysql.jdbc.Driver";
    String DB_URL = "jdbc:mysql://localhost:3306/test";
    String USER = "root";
    String PASS = "password";

    String sql="UPDATE test SET name=? ,course=?,score=?,id=?";
    try {
        Class.forName(JDBC_DRIVER);
        Connection conn = DriverManager.getConnection(DB_URL, USER, PASS);
        PreparedStatement psmt=conn.prepareStatement(sql);

        psmt.setString(1,name);
        psmt.setString(2,course);
        psmt.setInt(3,Integer.parseInt(score));
        psmt.setInt(4,Integer.parseInt(id));

        psmt.executeUpdate();
        conn.close();
        response.sendRedirect("index.jsp");
    } catch (ClassNotFoundException | SQLException e) {
        e.printStackTrace();
    }
%>

  • 12
    点赞
  • 153
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值