今天将本周一的代码进行了bug修改和完善,下面是源代码
四张数据库的内容与命名:
主页面:
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>选课管理系统</title> </head> <body> <table bgcolor="yellow" border="5" cellpadding="10" cellspacing="10" style="margin: 10px auto; color: darkorange; border-collapse: collapse" align="center" width="500"> <tr> <th colspan=2>登录</th> </tr> <form action="tdenglu.jsp" method="post" target="_self"> <tr> <td>老师</td> <td> <input type="submit" value="跳转"> </td> </tr> </form> <form action="sdenglu.jsp" method="post" target="_self"> <tr> <td>学生</td> <td> <input type="submit" value="跳转"> </td> </tr> </form> <form action="gdenglu.jsp" method="post" target="_self"> <tr> <td>管理员</td> <td> <input type="submit" value="跳转"> </td> </tr> </form> </table> </body> </html>
老师登录界面:
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>教师登录</title> </head> <body> <form action="teacher.jsp" method="post" target="_self"> <table bgcolor="yellow" border="5" cellpadding="10" cellspacing="10" style="margin: 10px auto; color: darkorange; border-collapse: collapse" align="center" width="500"> <tr> <th colspan=2>登录</th> </tr> <tr> <td>工号</td> <td><input type="text" name="tid"></td> </tr> <tr> <td>密码</td> <td><input type="password" name="tpass"></td> </tr> <tr> <td colspan="2"> <input type="submit"style=" background-color: aqua" value="登录"/> </td> </tr> </table> </form> </body> </html>
老师操作界面:
<%@ page import="java.sql.*" %><%-- Created by IntelliJ IDEA. User: 惠普 Date: 2023/11/13 Time: 14:05 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>教师功能页</title> </head> <body> <% String id=request.getParameter("tid"); String passw=request.getParameter("tpass"); String tname=""; try{ Class.forName("com.mysql.cj.jdbc.Driver"); // 2.使用我自己的数据库 test 获取链接 String url = "jdbc:mysql://localhost:3306/test"; String username = "root"; String password = "123456"; Connection connection = DriverManager.getConnection(url, username, password); //获取执行sql的对象statement Statement statement = connection.createStatement(); String sql2="select * from teacher where 教师工号 = '"+id+"'"; ResultSet res=statement.executeQuery(sql2); while(res.next()) { if(!passw.equals(res.getString("密码"))){ response.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY); String newLocn = "tdenglu.jsp"; response.setHeader("Location",newLocn); break; } tname=res.getString("教师姓名"); pageContext.setAttribute("p1",tname); System.out.println(tname); } connection.close(); }catch(Exception e){ System.out.println(e); } %> <table bgcolor="yellow" border="5" cellpadding="10" cellspacing="10" style="margin: 10px auto; color: darkorange; border-collapse: collapse" align="center" width="500"> <tr> <th colspan=2>操作</th> </tr> <form action="addclass.jsp" method="post" target="_self"> <tr> <input type="hidden" name="tname" value="${p1}"> <td>添加课程</td> <td> <input type="submit" value="跳转"> </td> </tr> </form> <form action="teacherupdate.jsp" method="post" target="_self"> <tr> <input type="hidden" value="<%=id%>" name="id"> <td>修改个人信息</td> <td> <input type="submit" value="跳转"> </td> </tr> </form> <form action="lookstudent.jsp" method="post" target="_self"> <tr> <input type="hidden" name="tname" value="<%=tname%>"> <td>浏览选课学生信息</td> <td> <input type="submit" value="跳转"> </td> </tr> </form> </table> <form action="index.jsp" method="post" target="_self"> <table align="center" > <tr> <td> <input type="submit"style=" background-color: aqua" value="返回"/> </td> </tr> </table> </form> </body> </html>
老师操作1,添加课程:
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>添加课程信息</title> </head> <body> <% String name=request.getParameter("tname"); pageContext.setAttribute("p1",name); %> <form action="acfinish.jsp" method="post" target="_self"> <table bgcolor="yellow" border="5" cellpadding="10" cellspacing="10" style="margin: 10px auto; color: darkorange; border-collapse: collapse" align="center" width="500"> <input type="hidden" name="teacher" value="${p1}"> <tr> <th colspan=2>添加课程信息</th> </tr> <tr> <td>课程编号</td> <td><input type="text" name="cid"></td> </tr> <tr> <td>课程名称</td> <td><input type="text" name="cname"></td> </tr> <tr> <td>选课人数</td> <td><input type="text" name="cnum"></td> </tr> <tr> <td colspan="2"> <input type="submit"style=" background-color: aqua" value="添加"/> </td> </tr> </table> </form> </body> </html>
添加成功:
<%@ page import="java.sql.*" %> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>添加成功</title> </head> <body> <% String teacher=request.getParameter("teacher"); String id=request.getParameter("cid"); String name=request.getParameter("cname"); String num=request.getParameter("cnum"); try{ Class.forName("com.mysql.cj.jdbc.Driver"); // 2.使用我自己的数据库 test 获取链接 String url = "jdbc:mysql://localhost:3306/test"; String username = "root"; String password = "123456"; Connection connection = DriverManager.getConnection(url, username, password); //获取执行sql的对象statement Statement statement = connection.createStatement(); String sql="insert into class(任课教师,课程编号,课程名称,选课人数) value(?,?,?,?)"; PreparedStatement ps1=connection.prepareStatement(sql); ps1.setString(1,teacher); ps1.setString(2,id); ps1.setString(3,name); ps1.setString(4,num); ps1.executeUpdate(); ps1.close(); connection.close(); }catch(Exception e){ System.out.println(e); } %> <form action="teacher.jsp" method="post" target="_self"> <table align="center" > <tr> <td> <input type="submit"style=" background-color: aqua" value="返回"/> </td> </tr> </table> </form> </body> </html>
老师操作2,修改个人信息:
<%@ page import="java.sql.DriverManager" %> <%@ page import="java.sql.Statement" %> <%@ page import="java.sql.Connection" %> <%@ page import="java.sql.ResultSet" %><%-- Created by IntelliJ IDEA. User: 惠普 Date: 2023/11/13 Time: 14:57 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>修改个人信息</title> </head> <body> <% String id=request.getParameter("id"); String tname=""; String tgender=""; String txueyuan=""; String zhicheng=""; try{ Class.forName("com.mysql.cj.jdbc.Driver"); // 2.使用我自己的数据库 test 获取链接 String url = "jdbc:mysql://localhost:3306/test"; String username = "root"; String password = "123456"; Connection connection = DriverManager.getConnection(url, username, password); //获取执行sql的对象statement Statement statement = connection.createStatement(); String sql2="select * from teacher where 教师工号 = '"+id+"'"; ResultSet res=statement.executeQuery(sql2); while(res.next()) { tname=res.getString("教师姓名"); tgender=res.getString("教师性别"); txueyuan=res.getString("教师所在学院"); zhicheng=res.getString("职称"); } connection.close(); }catch(Exception e){ System.out.println(e); } %> <form action="tufinish.jsp" method="post" target="_self"> <table bgcolor="yellow" border="5" cellpadding="10" cellspacing="10" style="margin: 10px auto; color: darkorange; border-collapse: collapse" align="center" width="500"> <tr> <input type="hidden" value="<%=id%>" name="id"> <td>教师姓名</td> <td><input type="text" name="tname" value="<%=tname%>"></td> </tr> <tr> <td>教师性别</td> <td><input type="text" name="tgender" value="<%=tgender%>"></td> </tr> <tr> <td>教师所在学院</td> <td><input type="text" name="txueyuan" value="<%=txueyuan%>"></td> </tr> <tr> <td>职称</td> <td><input type="text" name="zhicheng" value="<%=zhicheng%>"></td> </tr> <tr> <td colspan=2> <input type="submit" value="修改"> </td> </tr> </table> </form> </body> </html>
修改成功:
<%@ page import="java.sql.Connection" %> <%@ page import="java.sql.DriverManager" %> <%@ page import="java.sql.Statement" %> <%@ page import="java.sql.PreparedStatement" %><%-- Created by IntelliJ IDEA. User: 惠普 Date: 2023/11/13 Time: 15:09 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>修改成功</title> </head> <body> <% String xueyuan=request.getParameter("txueyuan"); String id=request.getParameter("id"); String name=request.getParameter("tname"); String gender=request.getParameter("tgender"); String zhicheng=request.getParameter("zhicheng"); try{ Class.forName("com.mysql.cj.jdbc.Driver"); // 2.使用我自己的数据库 test 获取链接 String url = "jdbc:mysql://localhost:3306/test"; String username = "root"; String password = "123456"; Connection connection = DriverManager.getConnection(url, username, password); //获取执行sql的对象statement Statement statement = connection.createStatement(); String sql="update plan set 教师姓名 = '"+name+"' ,教师性别 = '"+gender+"' ,教师所在学院 = '"+xueyuan+"' ,职称 = '"+zhicheng+"' where 教师工号 = '"+id+"'"; PreparedStatement ps1=connection.prepareStatement(sql);
ps1.executeUpdate(); ps1.close(); connection.close(); }catch(Exception e){ System.out.println(e); } %> <form action="teacher.jsp" method="post" target="_self"> <table align="center" > <tr> <td> <input type="submit"style=" background-color: aqua" value="返回"/> </td> </tr> </table> </form> </body> </html>
老师操作3,浏览选课学生信息:
<%@ page import="java.sql.Connection" %> <%@ page import="java.sql.DriverManager" %> <%@ page import="java.sql.Statement" %> <%@ page import="java.sql.ResultSet" %> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>浏览选课学生信息</title> </head> <body> <form action="lookstudent2.jsp" method="post" target="_self"> <table bgcolor="yellow" border="5" cellpadding="10" cellspacing="10" style="margin: 10px auto; color: darkorange; border-collapse: collapse" align="center" width="500"> <tr> <td>请选择要查看的课程</td> <% String tname=request.getParameter("tname"); try{ Class.forName("com.mysql.cj.jdbc.Driver"); // 2.使用我自己的数据库 test 获取链接 String url = "jdbc:mysql://localhost:3306/test"; String username = "root"; String password = "123456"; Connection connection = DriverManager.getConnection(url, username, password); //获取执行sql的对象statement Statement statement = connection.createStatement(); String sql2="select * from class where 任课教师 = '"+tname+"'"; ResultSet res=statement.executeQuery(sql2); while(res.next()) { String cname=res.getString("课程名称"); pageContext.setAttribute("p1",cname); %> <td><input type="radio" name="cname" value="<%=cname%>">${p1} </td> <% } connection.close(); }catch(Exception e){ System.out.println(e); } %> </tr> <tr> <td colspan="2"> <input type="submit"style=" background-color: aqua" value="查询"/> </td> </tr> </table> </form> </body> </html>
浏览界面:
<%@ page import="java.sql.Connection" %> <%@ page import="java.sql.DriverManager" %> <%@ page import="java.sql.Statement" %> <%@ page import="java.sql.ResultSet" %> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>浏览选课学生信息</title> </head> <body> <form action="teacher.jsp" method="post" target="_self"> <table bgcolor="yellow" border="5" cellpadding="10" cellspacing="10" style="margin: 10px auto; color: darkorange; border-collapse: collapse" align="center" width="500"> <tr> <td>学号</td> <td>姓名</td> <td>性别</td> <td>班级</td> <td>所属专业组成</td> </tr> <% String cname=request.getParameter("cname"); //1.注册驱动 Class.forName("com.mysql.cj.jdbc.Driver"); // 2.使用我自己的数据库 test 获取链接 String url = "jdbc:mysql://localhost:3306/test"; String username = "root"; String password = "123456"; Connection connection = DriverManager.getConnection(url, username, password); //获取执行sql的对象statement Statement statement = connection.createStatement(); String sql="select * from student where 所属专业组成 like '%"+cname+"%'"; ResultSet res=statement.executeQuery(sql); while(res.next()){ String s1=res.getString("学号"); String s2=res.getString("姓名"); String s3=res.getString("性别"); String s4=res.getString("班级"); String s5=res.getString("所属专业组成"); pageContext.setAttribute("p1",s1); pageContext.setAttribute("p2",s2); pageContext.setAttribute("p3",s3); pageContext.setAttribute("p4",s4); pageContext.setAttribute("p5",s5); %> <tr> <td><input type="hidden" name="name" value="${p1}">${p1}</td> <td><input type="hidden" name="talk" value="${p2}">${p2}</td> <td><input type="hidden" name="way" value="${p3}">${p3}</td> <td><input type="hidden" name="otime" value="${p4}">${p4}</td> <td><input type="hidden" name="ctime" value="${p5}">${p5}</td> </tr> <% } %> <tr> <td colspan="5"> <input type="submit"style=" background-color: aqua" value="返回"/> </td> </tr> </table> </form> </body> </html>
至此,教师部分完成,学生部分:
学生登录界面:
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>学生登录</title> </head> <body> <form action="student.jsp" method="post" target="_self"> <table bgcolor="yellow" border="5" cellpadding="10" cellspacing="10" style="margin: 10px auto; color: darkorange; border-collapse: collapse" align="center" width="500"> <tr> <th colspan=2>登录</th> </tr> <tr> <td>学号</td> <td><input type="text" name="sid"></td> </tr> <tr> <td>密码</td> <td><input type="password" name="spass"></td> </tr> <tr> <td colspan="2"> <input type="submit"style=" background-color: aqua" value="登录"/> </td> </tr> </table> </form> </body> </html>
学生操作界面:
<%-- Created by IntelliJ IDEA. User: 惠普 Date: 2023/11/13 Time: 14:05 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ page import="java.sql.*" %> <html> <head> <title>学生功能页</title> </head> <body> <% String id=request.getParameter("sid"); String passw=request.getParameter("spass"); String sname=""; try{ Class.forName("com.mysql.cj.jdbc.Driver"); // 2.使用我自己的数据库 test 获取链接 String url = "jdbc:mysql://localhost:3306/test"; String username = "root"; String password = "123456"; Connection connection = DriverManager.getConnection(url, username, password); //获取执行sql的对象statement Statement statement = connection.createStatement(); String sql2="select * from student where 学号 = '"+id+"'"; ResultSet res=statement.executeQuery(sql2); while(res.next()) { if(!passw.equals(res.getString("密码"))){ response.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY); String newLocn = "sdenglu.jsp"; response.setHeader("Location",newLocn); break; } sname=res.getString("姓名"); } connection.close(); }catch(Exception e){ System.out.println(e); } %> <table bgcolor="yellow" border="5" cellpadding="10" cellspacing="10" style="margin: 10px auto; color: darkorange; border-collapse: collapse" align="center" width="500"> <tr> <th colspan=2>操作</th> </tr> <form action="studentupdate.jsp" method="post" target="_self"> <tr> <input type="hidden" value="<%=id%>" name="id"> <td>修改个人信息</td> <td> <input type="submit" value="跳转"> </td> </tr> </form> <form action="lookclass.jsp" method="post" target="_self"> <tr> <input type="hidden" value="<%=id%>" name="id"> <td>浏览课程信息</td> <td> <input type="submit" value="跳转"> </td> </tr> </form> <form action="choclass.jsp" method="post" target="_self"> <tr> <input type="hidden" value="<%=id%>" name="id"> <td>选课</td> <td> <input type="submit" value="跳转"> </td> </tr> </form> </table> <form action="index.jsp" method="post" target="_self"> <table align="center" > <tr> <td> <input type="submit"style=" background-color: aqua" value="返回"/> </td> </tr> </table> </form> </body> </html>
学生操作1,修改个人信息:
<%@ page import="java.sql.DriverManager" %> <%@ page import="java.sql.Statement" %> <%@ page import="java.sql.Connection" %> <%@ page import="java.sql.ResultSet" %><%-- Created by IntelliJ IDEA. User: 惠普 Date: 2023/11/13 Time: 14:57 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>修改学生个人信息</title> </head> <body> <% String id=request.getParameter("id"); String sname=""; String sgender=""; String sbanji=""; String zhuanye=""; try{ Class.forName("com.mysql.cj.jdbc.Driver"); // 2.使用我自己的数据库 test 获取链接 String url = "jdbc:mysql://localhost:3306/test"; String username = "root"; String password = "123456"; Connection connection = DriverManager.getConnection(url, username, password); //获取执行sql的对象statement Statement statement = connection.createStatement(); String sql2="select * from student where 学号 = '"+id+"'"; ResultSet res=statement.executeQuery(sql2); while(res.next()) { sname=res.getString("姓名"); sgender=res.getString("性别"); sbanji=res.getString("班级"); zhuanye=res.getString("所属专业组成"); } connection.close(); }catch(Exception e){ System.out.println(e); } %> <form action="sufinish.jsp" method="post" target="_self"> <table bgcolor="yellow" border="5" cellpadding="10" cellspacing="10" style="margin: 10px auto; color: darkorange; border-collapse: collapse" align="center" width="500"> <tr> <input type="hidden" value="<%=id%>" name="id"> <td>姓名</td> <td><input type="text" name="sname" value="<%=sname%>"></td> </tr> <tr> <td>性别</td> <td><input type="text" name="sgender" value="<%=sgender%>"></td> </tr> <tr> <td>班级</td> <td><input type="text" name="sbanji" value="<%=sbanji%>"></td> </tr> <tr> <td>所属专业组成</td> <td><input type="text" name="zhuanye" value="<%=zhuanye%>"></td> </tr> <tr> <td colspan=2> <input type="submit" value="修改"> </td> </tr> </table> </form> </body> </html>
修改成功:
<%@ page import="java.sql.Connection" %> <%@ page import="java.sql.DriverManager" %> <%@ page import="java.sql.Statement" %> <%@ page import="java.sql.PreparedStatement" %> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>修改成功</title> </head> <body> <% String banji=request.getParameter("sbanji"); String id=request.getParameter("id"); String name=request.getParameter("sname"); String gender=request.getParameter("sgender"); String zhuanye=request.getParameter("zhuanye"); try{ Class.forName("com.mysql.cj.jdbc.Driver"); // 2.使用我自己的数据库 test 获取链接 String url = "jdbc:mysql://localhost:3306/test"; String username = "root"; String password = "123456"; Connection connection = DriverManager.getConnection(url, username, password); //获取执行sql的对象statement Statement statement = connection.createStatement(); String sql="update student set 姓名 = '"+name+"' ,性别 = '"+gender+"' ,班级 = '"+banji+"' ,所属专业组成 = '"+zhuanye+"' where 学号 = '"+id+"'"; PreparedStatement ps1=connection.prepareStatement(sql); ps1.executeUpdate(); ps1.close(); connection.close(); }catch(Exception e){ System.out.println(e); } %> <form action="student.jsp" method="post" target="_self"> <table align="center" > <tr> <td> <input type="submit"style=" background-color: aqua" value="返回"/> </td> </tr> </table> </form> </body> </html>
学生操作2,浏览课程信息:
<%@ page import="java.sql.Connection" %> <%@ page import="java.sql.DriverManager" %> <%@ page import="java.sql.Statement" %> <%@ page import="java.sql.ResultSet" %> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>浏览课程信息</title> </head> <body> <form action="student.jsp" method="post" target="_self"> <table bgcolor="yellow" border="5" cellpadding="10" cellspacing="10" style="margin: 10px auto; color: darkorange; border-collapse: collapse" align="center" width="500"> <tr> <td>任课教师</td> <td>课程编号</td> <td>课程名称</td> <td>选课人数</td> </tr> <tr> <% String id=request.getParameter("id"); try{ Class.forName("com.mysql.cj.jdbc.Driver"); // 2.使用我自己的数据库 test 获取链接 String url = "jdbc:mysql://localhost:3306/test"; String username = "root"; String password = "123456"; Connection connection = DriverManager.getConnection(url, username, password); //获取执行sql的对象statement Statement statement = connection.createStatement(); Statement stat = connection.createStatement(); String sql2="select * from 选课信息 where 学号 = '"+id+"'"; ResultSet res=statement.executeQuery(sql2); while(res.next()) { String sclass=res.getString("课程编号"); String sql="select * from class where 课程编号 = '"+sclass+"'"; ResultSet res2=stat.executeQuery(sql); while(res2.next()){ String classname=res2.getString("课程名称"); String tname=res2.getString("任课教师"); int num=res2.getInt("选课人数"); pageContext.setAttribute("p1",classname); %> <input type="hidden" name="tname" value="<%=tname%>"> <td><a href="find.jsp"><%=tname%></a></td> <td><%=sclass%></td> <td>${p1}</td> <td><%=num%>></td> <% } } connection.close(); }catch(Exception e){ System.out.println(e); } %> </tr> <tr> <td> <input type="submit"style=" background-color: aqua" value="返回"/> </td> </tr> </table> </form> </body> </html>
点击老师名字查看老师信息界面(超链接跳转):
<%-- Created by IntelliJ IDEA. User: 惠普 Date: 2023/11/14 Time: 21:58 To change this template use File | Settings | File Templates. --%> <%@ page import="java.sql.Connection" %> <%@ page import="java.sql.DriverManager" %> <%@ page import="java.sql.Statement" %> <%@ page import="java.sql.ResultSet" %> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>老师信息</title> </head> <body> <% String tname=request.getParameter("tname"); String tid = ""; String tgen = ""; String txueyuan = ""; String tzhicheng = ""; try { Class.forName("com.mysql.cj.jdbc.Driver"); // 2.使用我自己的数据库 test 获取链接 String url = "jdbc:mysql://localhost:3306/test"; String username = "root"; String password = "123456"; Connection connection = DriverManager.getConnection(url, username, password); //获取执行sql的对象statement Statement statement = connection.createStatement(); String sql = "select * from teacher where 教师姓名 = '" + tname + "'"; ResultSet res = statement.executeQuery(sql); while (res.next()) { tid = res.getString("教师工号"); tgen = res.getString("教师性别"); txueyuan = res.getString("教师所在学院"); tzhicheng = res.getString("职称"); } }catch (Exception e){ System.out.println(e); } %> <form action="student.jsp" method="post" target="_self"> <table bgcolor="yellow" border="5" cellpadding="10" cellspacing="10" style="margin: 10px auto; color: darkorange; border-collapse: collapse" align="center" width="500"> <tr> <td>教师工号</td> <td><%=tid%></td> </tr> <tr> <td>教师姓名</td> <td><%=tname%></td> </tr> <tr> <td>教师性别</td> <td><%=tgen%></td> </tr> <tr> <td>教师所在学院</td> <td><%=txueyuan%></td> </tr> <tr> <td>职称</td> <td><%=tzhicheng%></td> </tr> <tr> <td colspan=2><input type="submit" value="返回"></td> </tr> </table> </form> </body> </html>
学生操作3,选课:
<%@ page import="java.sql.*" %> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>选课</title> </head> <body> <form action="classin.jsp" method="post" target="_self"> <table bgcolor="yellow" border="5" cellpadding="10" cellspacing="10" style="margin: 10px auto; color: darkorange; border-collapse: collapse" align="center" width="500"> <% String id=request.getParameter("id"); //1.注册驱动 Class.forName("com.mysql.cj.jdbc.Driver"); // 2.使用我自己的数据库 test 获取链接 String url = "jdbc:mysql://localhost:3306/test"; String username = "root"; String password = "123456"; Connection connection = DriverManager.getConnection(url, username, password); //获取执行sql的对象statement Statement statement = connection.createStatement(); String sql="select * from class"; ResultSet res=statement.executeQuery(sql); while(res.next()){ int num=res.getInt("选课人数"); String name=res.getString("课程名称"); String cid=res.getString("课程编号"); if(num<100){ pageContext.setAttribute("p1",name); pageContext.setAttribute("p2",id); %> <tr> <input type="hidden" value="${p2}" name="id"> <input type="hidden" value="<%=cid%>" name="cid"> <td><input type="submit" value="${p1}" name="cname"></td> </tr> <% } } %> </table> </form> </body> </html>
选择课程:
<%@ page import="java.sql.*" %> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>课程信息</title> </head> <body> <form action="ccfinish.jsp" method="post" target="_self"> <table bgcolor="yellow" border="5" cellpadding="10" cellspacing="10" style="margin: 10px auto; color: darkorange; border-collapse: collapse" align="center" width="500"> <tr> <th colspan=2>课程信息</th> </tr> <% String cname=request.getParameter("cname"); String id=request.getParameter("id"); int cnum=0; try { Class.forName("com.mysql.cj.jdbc.Driver"); // 2.使用我自己的数据库 test 获取链接 String url = "jdbc:mysql://localhost:3306/test"; String username = "root"; String password = "123456"; Connection connection = DriverManager.getConnection(url, username, password); //获取执行sql的对象statement Statement statement = connection.createStatement(); Statement stat = connection.createStatement(); String sql = "select * from class where 课程名称 = '" + cname + "'"; ResultSet res = statement.executeQuery(sql); while (res.next()) { String cteacher = res.getString("任课教师"); String cid = res.getString("课程编号"); cnum = res.getInt("选课人数"); System.out.println(cnum); pageContext.setAttribute("p1", cteacher); pageContext.setAttribute("p2", cid); String sql3 = "select * from teacher where 教师姓名 = '" + cteacher + "'"; ResultSet res2 = stat.executeQuery(sql3); while(res2.next()) { String tid = res2.getString("教师工号"); String n=""+cnum; %> <input type="hidden" value="<%=cid%>" name="cid"> <input type="hidden" value="<%=n%>" name="cnum"> <input type="hidden" value="<%=tid%>" name="tid"> <% } } }catch (Exception e){ System.out.println(e); } %> <tr> <input type="hidden" value="<%=id%>" name="id"> <input type="hidden" value="<%=cname%>" name="cname"> <td> 任课教师 </td> <td>${p1}</td> </tr> <tr> <td> 课程编号 </td> <td>${p2}</td> </tr> <tr> <td> 选课人数 </td> <td><%=cnum%></td> </tr> <tr> <td colspan=2><input type="submit" value="选课"></td> </tr> </table> </form> </body> </html>
选课完成:
<%@ page import="java.sql.*" %> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>选课成功</title> </head> <body> <% String id=request.getParameter("id"); String cid=request.getParameter("cid"); String tid=request.getParameter("tid"); int num=Integer.valueOf(request.getParameter("cnum")); num+=1; System.out.println(num); try { Class.forName("com.mysql.cj.jdbc.Driver"); // 2.使用我自己的数据库 test 获取链接 String url = "jdbc:mysql://localhost:3306/test"; String username = "root"; String password = "123456"; Connection connection = DriverManager.getConnection(url, username, password); //获取执行sql的对象statement Statement statement = connection.createStatement(); String sql = "update class set 选课人数 = " + num + " where 课程编号 = '" + cid + "'"; PreparedStatement ps1 = connection.prepareStatement(sql); ps1.executeUpdate(); String sql2 = "insert into 选课信息(课程编号,教师编号,学号) value(?,?,?)"; PreparedStatement ps2 = connection.prepareStatement(sql2); ps2.setString(1, cid); ps2.setString(2, tid); ps2.setString(3, id); ps2.executeUpdate(); }catch (Exception e){ System.out.println(e); } %> <form action="student.jsp" method="post" target="_self"> <table align="center" > <tr> <td> <input type="submit"style=" background-color: aqua" value="返回"/> </td> </tr> </table> </form> </body> </html>
至此,学生部分完成,管理员部分:
管理员登录:
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>管理员登录</title> </head> <body> <form action="guanli.jsp" method="post" target="_self"> <table bgcolor="yellow" border="5" cellpadding="10" cellspacing="10" style="margin: 10px auto; color: darkorange; border-collapse: collapse" align="center" width="500"> <tr> <th colspan=2>登录</th> </tr> <tr> <td>帐号</td> <td><input type="text" name="tid"></td> </tr> <tr> <td>密码</td> <td><input type="password" name="tpass"></td> </tr> <tr> <td colspan="2"> <input type="submit"style=" background-color: aqua" value="登录"/> </td> </tr> </table> </form> </body> </html>
管理员操作界面:
<%-- Created by IntelliJ IDEA. User: 惠普 Date: 2023/11/13 Time: 14:04 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>管理员功能页</title> </head> <body> <table bgcolor="yellow" border="5" cellpadding="10" cellspacing="10" style="margin: 10px auto; color: darkorange; border-collapse: collapse" align="center" width="500"> <tr> <th colspan=2>操作</th> </tr> <form action="addteacher.jsp" method="post" target="_self"> <tr> <td>添加教师信息</td> <td> <input type="submit" value="跳转"> </td> </tr> </form> <form action="addstudent.jsp" method="post" target="_self"> <tr> <td>添加学生信息</td> <td> <input type="submit" value="跳转"> </td> </tr> </form> <form action="index.jsp" method="post" target="_self"> <tr> <td colspan=2> <input type="submit" value="返回"> </td> </tr> </form> </table> </body> </html>
管理员操作1,添加教师信息:
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>添加教师信息</title> </head> <body> <form action="finishat.jsp" method="post" target="_self"> <table bgcolor="yellow" border="5" cellpadding="10" cellspacing="10" style="margin: 10px auto; color: darkorange; border-collapse: collapse" align="center" width="500"> <tr> <th colspan=2>添加教师信息</th> </tr> <tr> <td>教师工号</td> <td><input type="text" name="sid"></td> </tr> <tr> <td>教师姓名</td> <td><input type="text" name="sname"></td> </tr> <tr> <td>教师性别</td> <td><input type="text" name="sgen"></td> </tr> <tr> <td>教师所在学院</td> <td><input type="text" name="sbanji"></td> </tr> <tr> <td>职称</td> <td><input type="text" name="szhuanye"></td> </tr> <tr> <td>密码</td> <td><input type="text" name="spass"></td> </tr> <tr> <td colspan="2"> <input type="submit"style=" background-color: aqua" value="添加"/> </td> </tr> </table> </form> </body> </html>
添加教师信息完成:
<%@ page import="java.sql.*" %> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>添加成功</title> </head> <body> <% String gen=request.getParameter("sgen"); String id=request.getParameter("sid"); String name=request.getParameter("sname"); String banji=request.getParameter("sbanji"); String zhuanye=request.getParameter("szhuanye"); String pass=request.getParameter("spass"); try{ Class.forName("com.mysql.cj.jdbc.Driver"); // 2.使用我自己的数据库 test 获取链接 String url = "jdbc:mysql://localhost:3306/test"; String username = "root"; String password = "123456"; Connection connection = DriverManager.getConnection(url, username, password); //获取执行sql的对象statement Statement statement = connection.createStatement(); String sql="insert into teacher(教师工号,教师姓名,教师性别,教师所在学院,职称,密码) value(?,?,?,?,?,?)"; PreparedStatement ps1=connection.prepareStatement(sql); ps1.setString(1,id); ps1.setString(2,name); ps1.setString(3,gen); ps1.setString(4,banji); ps1.setString(5,zhuanye); ps1.setString(6,pass); ps1.executeUpdate(); ps1.close(); connection.close(); }catch(Exception e){ System.out.println(e); } %> <form action="guanli.jsp" method="post" target="_self"> <table align="center" > <tr> <td> <input type="submit"style=" background-color: aqua" value="返回"/> </td> </tr> </table> </form> </body> </html>
管理员操作2,添加学生信息:
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>添加学生信息</title> </head> <body> <form action="finishas.jsp" method="post" target="_self"> <table bgcolor="yellow" border="5" cellpadding="10" cellspacing="10" style="margin: 10px auto; color: darkorange; border-collapse: collapse" align="center" width="500"> <tr> <th colspan=2>添加学生信息</th> </tr> <tr> <td>学号</td> <td><input type="text" name="sid"></td> </tr> <tr> <td>姓名</td> <td><input type="text" name="sname"></td> </tr> <tr> <td>性别</td> <td><input type="text" name="sgen"></td> </tr> <tr> <td>班级</td> <td><input type="text" name="sbanji"></td> </tr> <tr> <td>所属专业组成</td> <td><input type="text" name="szhuanye"></td> </tr> <tr> <td>密码</td> <td><input type="text" name="spass"></td> </tr> <tr> <td colspan="2"> <input type="submit"style=" background-color: aqua" value="添加"/> </td> </tr> </table> </form> </body> </html>
添加完成:
<%@ page import="java.sql.*" %> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>添加成功</title> </head> <body> <% String gen=request.getParameter("sgen"); String id=request.getParameter("sid"); String name=request.getParameter("sname"); String banji=request.getParameter("sbanji"); String zhuanye=request.getParameter("szhuanye"); String pass=request.getParameter("spass"); try{ Class.forName("com.mysql.cj.jdbc.Driver"); // 2.使用我自己的数据库 test 获取链接 String url = "jdbc:mysql://localhost:3306/test"; String username = "root"; String password = "123456"; Connection connection = DriverManager.getConnection(url, username, password); //获取执行sql的对象statement Statement statement = connection.createStatement(); String sql="insert into student(学号,姓名,性别,班级,所属专业组成,密码) value(?,?,?,?,?,?)"; PreparedStatement ps1=connection.prepareStatement(sql); ps1.setString(1,id); ps1.setString(2,name); ps1.setString(3,gen); ps1.setString(4,banji); ps1.setString(5,zhuanye); ps1.setString(6,pass); ps1.executeUpdate(); ps1.close(); connection.close(); }catch(Exception e){ System.out.println(e); } %> <form action="guanli.jsp" method="post" target="_self"> <table align="center" > <tr> <td> <input type="submit"style=" background-color: aqua" value="返回"/> </td> </tr> </table> </form> </body> </html>
标签:11,教师,name,选课,信息,学生,添加,源代码 From: https://www.cnblogs.com/jiajiayu/p/17832769.html