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.*" %>



    
    成绩


学生成绩表


添加信息 <% 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); %> <% } %>
学号 姓名 科目 成绩 删除 更新

数据库的连接

<%
	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.*" %>


    <%
        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();

    %>
    插入页面


insert message
学号: 姓名: 科目: 成绩:
<% } catch (ClassNotFoundException | SQLException e) { e.printStackTrace(); } %>

<%@ 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();
    }
%>

你可能感兴趣的:(web_class)