JDBC database access and JavaBean

1. Add new student number function to achieve

  1. The user clicks the “Add New Student Number” link, the page jumps to the addNum.jsp page, enters the student number and name information on this page, clicks the confirm button, and jumps to the doAddNum.jsp page;

  1. The doAddNum.jsp page obtains the entered student number information, and uses JDBC to add the information to the data table java2022;

  1. After adding, jump to the showAllStudents.jsp page.

//showAllStudents.jsp

<%@ page import="java.sql.*" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Display all student information</title>
</head>
<body>
<a href="addNum.jsp">Add student number</a>
<%
    //1. Load the driver
    Class.forName("com.mysql.jdbc.Driver");
    //2. Establish a connection with the database
    String url = "jdbc:mysql://localhost:3306/java2022";
    Connection conn = DriverManager.getConnection(url, "root", "root");
    //3. Create command object
    Statement st=conn.createStatement();
    //4. Execute the SQL statement
    String sql = "SELECT * FROM java2022";
    ResultSet rs=st. executeQuery(sql);
    //5. Get and output query results
    out.println("<table border='1'>");
    out.println("<tr><td>Student number num</td><td>Name name</td><td>Edit</td></tr>");
    while (rs. next()){
        out.println("<tr>");
        out.println("<td>" + rs.getString("num") + "</td>");
        out.println("<td>" + rs.getString("name") + "</td>");
        out.println("<td>");
        out.println("<a href='updateNum.jsp?num=" + rs.getString("num") + "'>modify</a>");
        out.println("<a href='deleteNum.jsp?num=" + rs.getString("num") + "'>delete</a>");
        out.println("</td>");
        out.println("</tr>");
    }
    out.println("</table>");
    //6. Release resources
    rs.close();st.close();conn.close();
%>
</body>
</html>

//addNum.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Add a new student number</title>
</head>
<body>
    <form action="doAddNum.jsp">
        Student ID: <input type="text" name="num">
        Name: <input type="text" name="name">
        <input type="submit" value="submit">
    </form>
</body>
</html>

//doAddNum.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <%
        String num = request. getParameter("num");
        String name = request. getParameter("name");
        Class.forName("com.mysql.cj.jdbc.Driver");
        String url = "jdbc:mysql://localhost:3306/javaee";
        Connection conn = DriverManager.getConnection(url, "root", "123456");
        String sql = "insert into java2022(num,name) values(?,?)";
        PreparedStatement pst = conn. prepareStatement(sql);
        pst. setString(1, num);
        pst. setString(2, name);
        pst. executeUpdate();
        response.sendRedirect("showAllStudents.jsp");
        ps. close();
conn. close();

    %>
</body>
</html>

2. Modify the student name according to the student number num

1. The user clicks the “Edit” link to jump to the updateName.jsp file, where the name information of the specified num is displayed and can be modified, and clicks the submit button to jump to the doUpdateName.jsp file;

2. The doUpdateName.jsp file obtains the name entered in the updateName.jsp file, and modifies the student’s name according to the student ID;

3. After modification, jump to the showAllStudents.jsp page.

//updateName.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Modify</title>
</head>
<body>
    <%
        String num = request. getParameter("num");
    %>
    <form action="doUpdateName.jsp">
        Student ID: <input type="text" name="num" value="<%=num%>" readonly>
        Name: <input type="text" name="name">
        <input type="submit" value="modify">
    </form>
</body>
</html>

//doUpdateName.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <%
        String num = request. getParameter("num");
        String name = request. getParameter("name");
        Class.forName("com.mysql.cj.jdbc.Driver");
        String url = "jdbc:mysql://localhost:3306/javaee";
        Connection conn = DriverManager.getConnection(url, "root", "123456");
        String sql = "update java2022 set name=? where num=?";
        PreparedStatement ps = conn. prepareStatement(sql);
        ps. setString(1, name);
        ps. setString(2, num);
        ps. executeUpdate();
        response.sendRedirect("showAllStudents.jsp");
        ps. close();
        conn. close();
    %>
</body>
</html>

3. Realize the function of deleting student information according to the student number num

1. The user clicks the “delete” link, and the page jumps to the deleteNum.jsp file, where the student number num is obtained, and the student information is deleted according to the student number num;

2. After deleting, jump to the showAllStudents.jsp page.

//deleteNum.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Delete student information</title>
</head>
<body>
    <%
        String num = request. getParameter("num");
        Class.forName("com.mysql.cj.jdbc.Driver");
        String url = "jdbc:mysql://localhost:3306/javaee";
        Connection conn = DriverManager.getConnection(url, "root", "123456");
        String sql = "delete from java2022 where num=?";
        PreparedStatement pst = conn. prepareStatement(sql);
        pst. setString(1, num);
        pst. executeUpdate();
        response.sendRedirect("showAllStudents.jsp");
    ps. close();
conn. close();
    
    %>
</body>
</html>

4. The creation of JavaBean can use the “code method” and “label method” to create JavaBean instance objects, set attributes and obtain attributes:

1) Create a JavaBean in the pojo package under src, the file name is Student.java, and the specific content is as follows:

  • Member variables:

num //student number, String type, private permission

name //Name, String type, private permission

  • Member method:

The getter and setter methods corresponding to each attribute are public permissions

2) Create a new javabean.jsp page, use the “code method” and “label method” to create a JavaBean instance, set and get attributes.


<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<jsp:useBean id="stu1" class="com.example.lab03.beans.Student">
    <jsp:setProperty name="stu1" property="num" value="0123"/>
    <jsp:setProperty name="stu1" property="name" value="Zhang San"/>
    <jsp:getProperty name="stu1" property="num"/>
    <jsp:getProperty name="stu1" property="name"/>
</jsp:useBean>
<%
    Student stu2 = new Student();
    stu2.setNum("0456");
    stu2.setName("Li Si");
    out. print("<br>" + stu2.getNum() + stu2.getName());
%>
</body>
</html>

The knowledge points of the article match the official knowledge files, and you can further learn related knowledge Java skill treeUsing JDBC to operate the databaseJDBC overview 108557 people are studying systematically