学生servlet import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.util.List; @WebServlet("/sl") public class studentListServlet extends HttpServlet { private StudentDao studentDao = new StudentDao(); @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { try { List<Student> list = studentDao.selectAll(); req.setAttribute("list", list); req.getRequestDispatcher("studentList.jsp").forward(req, resp); } catch (Exception e) { e.printStackTrace(); } } }s数据库管理层
import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.Statement; import java.util.ArrayList; import java.util.List; //dao Data Access Object 数据访问对象 public class StudentDao { private Connection conn = DBUtil.getConn(); public List<Student> selectAll() throws Exception { String sql = "select * from student"; Statement st = conn.createStatement(); ResultSet rs = st.executeQuery(sql); List<Student> list = new ArrayList<>(); while (rs.next()) { Student s = rsToStudent(rs); list.add(s); } rs.close(); return list; } public void insert(Student st) throws Exception { String sql = "insert into student(sname,ssex,sage,sdept) values(?,?,?,?)"; PreparedStatement ps = conn.prepareStatement(sql); ps.setString(1, st.getSname()); ps.setString(2, st.getSsex()); ps.setInt(3, st.getSage()); ps.setString(4, st.getSdept()); ps.executeUpdate(); } public Student SelectByNo(int sno) throws Exception { return null; } public void update(Student st) throws Exception { } public void delete(String sno) throws Exception { } private Student rsToStudent(ResultSet rs) throws Exception { Student s = new Student(); s.setSno(rs.getInt("sno")); s.setSname(rs.getString("sname")); s.setSsex(rs.getString("ssex")); s.setSage(rs.getInt("sage")); s.setSdept(rs.getString("sdept")); return s; } } 数据库连接
import java.sql.Connection; import java.sql.DriverManager; public class DBUtil { private static Connection conn = null; static { try { Class.forName("com.mysql.cj.jdbc.Driver"); String url = "jdbc:mysql://localhost:3306/studb?serverTimezone=UTC&characterEncoding=UTF-8&useSSL=false"; String username = "root"; String password = "root"; conn = DriverManager.getConnection(url, username, password); }catch (Exception e) { e.printStackTrace(); } } public static Connection getConn() { return conn; } }
public class Student { private int sno; private String sname; private String ssex; private int sage; private String sdept; public int getSno() { return sno; } public void setSno(int sno) { this.sno = sno; } public String getSname() { return sname; } public void setSname(String sname) { this.sname = sname; } public String getSsex() { return ssex; } public void setSsex(String ssex) { this.ssex = ssex; } public int getSage() { return sage; } public void setSage(int sage) { this.sage = sage; } public String getSdept() { return sdept; } public void setSdept(String sdept) { this.sdept = sdept; } }
学生列表 <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>学生列表</title> </head> <body> <h1 align="center">学生列表</h1> <h3 align="center"><a href="addStudent.jsp">添加学生</a></h3> <table align="center" border="1" width="800px"> <tr> <td>学号</td> <td>姓名</td> <td>性别</td> <td>年龄</td> <td>专业</td> <td>操作</td> </tr> <% List<Student> list = (List<Student>) request.getAttribute("list"); for (Student s : list) { out.println("<tr>"); out.println("<td>" + s.getSno() + "</td>"); out.println("<td>" + s.getSname() + "</td>"); out.println("<td>" + s.getSsex() + "</td>"); out.println("<td>" + s.getSage() + "</td>"); out.println("<td>" + s.getSdept() + "</td>"); out.println("<td>"); out.println("<a href='us?sno=" + s.getSno() + "'>修改</a>"); out.println("<a href='ds?sno=" + s.getSno() + "'>删除</a>"); out.println("</td>"); out.println("</tr>"); } %> </table> </body> </html>
添加学生
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>添加学生</title> </head> <body> <h1>添加学生</h1> <form action="as" method="post"> 姓名:<input type="text" name="sname"><br> 性别:<input type="text" name="ssex"><br> 年龄:<input type="text" name="sage"><br> 专业:<input type="text" name="sdept"><br> <input type="submit" value="添加"> </form> </body> </html>
修改学生
<html> <head> <title>修改学生</title> </head> <body> <h1>修改学生</h1> <% Student s = (Student) request.getAttribute("st"); %> <form action="us" method="post"> <%-- type="hidden"是一个隐藏输入框,作用是用来传值,不会在页面上显示--%> <input type="hidden" name="sno" value="<%=s.getSno()%>"> 姓名:<input type="text" name="sname" value="<%=s.getSname()%>"><br> 性别:<input type="text" name="ssex" value="<%=s.getSsex()%>"><br> 年龄:<input type="text" name="sage" value="<%=s.getSage()%>"><br> 专业:<input type="text" name="sdept" value="<%=s.getSdept()%>"><br> <input type="submit" value="修改" > </form> </body> </html>标签:java,String,管理系统,println,增删,import,out,public,Javaweb From: https://blog.csdn.net/2301_79612384/article/details/140770261