简单的课程管理系统
Posted yxsz
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了简单的课程管理系统相关的知识,希望对你有一定的参考价值。
本题本质是数据库与javaweb结合,辅以javabean和servlet。
(1)增加新课程
.java
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>添加首页</title> </head> <body> <form id="form1" action="/stumessage/AServlet" method="post" onsubmit="return form1()"> <div> 课程名称:<input type="text" id="lesson" name="lesson"/> </div> <div> 任课教师:<input type="text" id="teacher" name="teacher"/> </div> <div> 上课地点:<input type="text" id="classroom" name="classroom"/> </div> <div> <button type="submit" id="sub" value="提交">提交</button> </div> </form> <script type="text/javascript"> function form1(){ var lesson=document.getElementById("lesson"); var teacher=document.getElementById("teacher"); var classroom=document.getElementById("classroom"); if(lesson.value == ""){ alert("课程名称不能为空!"); return false; } if(teacher.value == ""){ alert("任课老师不能为空!"); return false; } if(classroom.value == ""){ alert("上课地点不能为空!"); return false; } if(teacher.value != "王建民" && teacher.value != "王辉" && teacher.value != "刘丹" && teacher.value != "刘立嘉" && teacher.value != "杨子光"){ alert("任课老师只能在王建民,王辉,刘丹,刘立嘉,杨子光中选择!!"); return false; } if(!/^基教/.test(classroom.value) && !/^一教/.test(classroom.value) && !/^二教/.test(classroom.value) && !/^三教/.test(classroom.value)){ alert("上课地点格式错误!!只能从基教,一教,二教,三教中选择!!例:基教201"); return false; } else{ alert("添加成功!"); return true; } } </script> </body> </html>
package stuMessage;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class Aservlet
*/
@WebServlet("/Aservlet")
public class Aservlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public Aservlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
System.out.println("doGet...");
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
request.setCharacterEncoding("utf-8"); //1
response.setContentType("text/html;charset=utf-8"); //2
response.setCharacterEncoding("utf-8"); //3
String lesson=request.getParameter("lesson");
String teacher=request.getParameter("teacher");
String classroom=request.getParameter("classroom");
/*
* 开始连接数据库
*/
//驱动程序名
String driver = "com.mysql.jdbc.Driver";
//URL指向要访问的数据库名mydata
String url = "jdbc:mysql://localhost:3306/demo01?serverTimezone=GMT%2B8";
//MySQL配置时的用户名
String user = "root";
//MySQL配置时的密码
String password = "wtdWTD727398q";
//遍历查询结果集
try {
//加载驱动程序
Class.forName(driver);
//1.getConnection()方法,连接MySQL数据库!!
Connection con = DriverManager.getConnection(url,user,password);
if(!con.isClosed())
System.out.println("Succeeded connecting to the Database!");
PreparedStatement psql;
//预处理添加数据,其中有两个参数--“?”
psql = con.prepareStatement("insert into stumessage (lesson,teacher,classroom) " + "values(?,?,?)");
psql.setString(1, lesson); //设置参数1,创建id为3212的数据
psql.setString(2, teacher); //设置参数2,name 为王刚
psql.setString(3, classroom);
psql.executeUpdate();
con.close();
psql.close();
}catch(SQLException e) {
//数据库连接失败异常处理
e.printStackTrace();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}finally{
System.out.println("数据库数据成功添加!!");
request.getRequestDispatcher("/NewFile.jsp").forward(request,response);
}
}
}
运行结果:
(2)删除数据
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>删除首页</title> </head> <body> <form action="/stumessage/BServlet" method="post"> 课程名称:<input type="text" id="lesson" name="lesson"/> <button type="submit" id="sub" value="提交">提交</button> </form> </body> </html>
package stuMessage; import java.io.IOException; import java.io.PrintWriter; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.Scanner; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet implementation class Bservlet */ @WebServlet("/Bservlet") public class Bservlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public Bservlet() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub PrintWriter out = response.getWriter(); request.setCharacterEncoding("utf-8"); //1 response.setContentType("text/html;charset=utf-8"); //2 response.setCharacterEncoding("utf-8"); //3 String lesson = request.getParameter("lesson"); Scanner sc = new Scanner(System.in); //驱动程序名 String driver = "com.mysql.jdbc.Driver"; //URL指向要访问的数据库名mydata String url = "jdbc:mysql://localhost:3306/demo01?serverTimezone=GMT%2B8"; //MySQL配置时的用户名 String user = "root"; //MySQL配置时的密码 String password = "wtdWTD727398q"; //遍历查询结果集 try { //加载驱动程序 Class.forName(driver); Connection con = DriverManager.getConnection(url, user, password); if(!con.isClosed()) System.out.println("Succeeded connecting to the Database!"); //2.创建statement类对象,用来执行SQL语句!! Statement statement = con.createStatement(); //要执行的SQL语句 String sql = "select * from stumessage"; //3.ResultSet类,用来存放获取的结果集!! ResultSet rs = statement.executeQuery(sql); PreparedStatement psql; psql = con.prepareStatement("delete from stumessage where lesson = ?"); while(rs.next()) { if(lesson.equals(rs.getString("lesson"))) { psql.setString(1, lesson); out.print("<html><head><body>"); out.print(rs.getString("lesson")+" "+rs.getString("teacher")+" "+rs.getString("classroom")); out.print("<button type=‘button‘ id=‘sub‘ value=‘提交‘ onclick=‘sub1()‘>删除</button>"); out.print("<script type=‘text/javascript‘>"); out.print("function sub1(){"); out.print("alert(‘删除成功!!‘)"); out.print("}"); out.print("</script>"); out.print("</body></head></html>"); psql.executeUpdate(); request.getRequestDispatcher("/NewFile.jsp").forward(request,response); break; }else if(rs.isLast()) { request.getRequestDispatcher("/fail.jsp").forward(request,response); } } rs.close(); con.close(); psql.close(); }catch(SQLException e) { //数据库连接失败异常处理 e.printStackTrace(); } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } }
以上是关于简单的课程管理系统的主要内容,如果未能解决你的问题,请参考以下文章
20155234 2017-2018-1《信息安全系统设计基础》课程总结
HTML5期末大作业:餐饮美食网站设计——咖啡(10页) HTML+CSS+JavaScript 学生DW网页设计作业成品 web课程设计网页规划与设计 咖啡网页设计 美食餐饮网页设计...(代码片段