JavaBean:用于传递数据,拥有与数据相关的逻辑处理
JSP:从Model接收数据并生成HTML
Servlet:接收HTTP请求并控制Model和View
jdbc:用于配置环境
一、【建立数据库】
在mysql中新建一个名为kecheng的数据库,并在其中新建一个kecheng数据表,包含三个字段,classname,classroom,teacher
二、【新建web项目】
1. eclipse新建一个名为kechengguanlli的Dynamic Web Project
修改class的默认输出目录为:WebContent/WEB-INF/classes,勾选自动生成web.xml
2. 将连接mysql的驱动jar包(mysql-connector-java-5.1.7-bin.jar)copy到WEB-INF下的lib目录下
3. 新建7个包
helper、链接数据库的操作,避免重复写代码,所以用一个包封装起来
SQL、数据库增删改查的统一代码,在实例中具体调用
javabean、基类,包括此项目中的成员变量
servlet、增加数据库中课程
servletdelete、删除数据库中课程
servletseclec、查询数据库中课程
servletupdate、修改数据库中课程
4. 新建类(最愉快的搬砖环节 -.-),java类放在对应包中,jsp页面放在WebContent目录下,替换web.xml中内容;
helper包下新建 DbHelper.java
package helper; import java.sql.Connection; import java.sql.DriverManager; public class DbHelper { private static String url = "jdbc:mysql://localhost:3306/kecheng"; //数据库地址 private static String userName = "root"; //数据库用户名 private static String passWord = "199812"; //数据库密码 private static Connection conn = null; private DbHelper(){ } public static Connection getConnection(){ if(null == conn){ try { Class.forName("com.mysql.jdbc.Driver"); conn = DriverManager.getConnection(url, userName, passWord); } catch (Exception e) { e.printStackTrace(); } } return conn; } public static void main(String[] args) { //测试数据库是否连通 System.out.println(getConnection()); } }
javabean包下新建kecheng.java
package javabean; public class kecheng { private String classname; private String teacher; private String classroom; private String fanhui; public kecheng() {} public String getClassname() { return classname; } public void setClassname(String classname) { this.classname = classname; } public String getTeacher() { return teacher; } public void setTeacher(String teacher) { this.teacher = teacher; } public String getClassroom() { return classroom; } public void setClassroom(String classroom) { this.classroom = classroom; } public String getFanhui() { return fanhui; } public void setFanhui(String fanhui) { this.fanhui = fanhui; } }
servlet包下新建add.java
package servlet; import java.io.IOException; 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 javabean.kecheng; import javabean.kecheng; import SQL.sqlclass; /** * Servlet implementation class add */ @WebServlet("/add") public class add extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public add() { 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 int i=0,j=1,k=0; response.getWriter().append("Served at: ").append(request.getContextPath()); String classname=request.getParameter("classname");//防止中文乱码 String teacher=request.getParameter("teacher"); String classroom=request.getParameter("classroom"); if(teacher.equals("王建民")||teacher.equals("刘立嘉")||teacher.equals("刘丹")||teacher.equals("王辉")||teacher.equals("杨子光")) i=1; String name; name=classroom.substring(0,2); if(name.equals("一教")||name.equals("二教")||name.equals("三教")||name.equals("基教")) j=1; if(true)//课程名 k=1; if(i==1&&j==1&&k==1) { kecheng student=new kecheng(); student.setClassname(classname); student.setTeacher(teacher); student.setClassroom(classroom); sqlclass sql=new sqlclass(); sql.addUser(student); response.sendRedirect("first.jsp");//页面跳转 } else response.sendRedirect("add.jsp"); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }
【 jsp页面放在WebContent目录下】
first.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <center>课程基本信息管理主界面</center> <center>——————————————————————————————————</center> <a href="add.jsp"><center><b>课程信息录入</b></center></a><br> <a href="update.jsp"><center><b>课程信息修改</b></center></a><br> <a href="delete.jsp"><center><b>课程信息删除</b></center></a><br> <a href="select.jsp"><center><b>课程信息查询</b></center></a><br> </body> </html>
add.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <form action="servlet.add" method="get"> <center>课程信息录入界面</center> <center>——————————————————————————————————————</center> <center>课程名称:<input type="text" name=classname><br></center> <center>任课教师:<input type="text" name=teacher><br></center> <center>上课地点:<input type="text" name=classroom><br></center> <center><input type="submit" value="保存"></center> </body> </html>
delete.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <center>课程信息删除</center> <center>输入以下了表中的任意一个即可</center> <center>——————————————————————————————————</center> <form action="servletdelete.delete" method="get"> <center>课程名称:<input type="text" name=classname><br></center> <center>任课教师:<input type="text" name=teacher><br></center> <center>上课地点:<input type="text" name=classroom><br></center> <center><input type="submit" value="确定删除"></center> </body> </html>
select.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@page import="java.util.*"%> <%@page import="javabean.kecheng"%> <%@page import="SQL.sqlclass"%> <%@page import="java.util.List"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <center><%="课程"+" "+"教师"+" "+"教室" %> </center> <% sqlclass a=new sqlclass(); a.getAllUser(); kecheng c=new kecheng(); List<kecheng> b= new ArrayList<kecheng>(); b=a.getAllUser(); int i=0; int j=0; j=a.getnum(); for(;i<j;i++) {c=b.get(i); %> <center><%=c.getClassname()+" "+c.getTeacher()+" "+c.getClassroom() %> </center> <% }%> </body> </html>
update.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <center>课程信息修改</center> <center>——————————————————————————————————</center> <form action="servletupdate.update" method="get"> <center>课程名称:<input type="text" name=classname><br></center> <center>任课教师:<input type="text" name=teacher><br></center> <center>上课地点:<input type="text" name=classroom><br></center> <center><input type="submit" value="保存"></center> </body> </html>
【替换web.xml中内容】
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" id="WebApp_ID" version="4.0"> <display-name>kechengguanli</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <servlet> <servlet-name>servlet</servlet-name> <servlet-class>servlet.add</servlet-class> </servlet> <servlet-mapping> <servlet-name>servlet</servlet-name> <url-pattern>/servlet.add</url-pattern> </servlet-mapping> <servlet> <servlet-name>servletdelete</servlet-name> <servlet-class>servletdelete.delete</servlet-class> </servlet> <servlet-mapping> <servlet-name>servletdelete</servlet-name> <url-pattern>/servletdelete.delete</url-pattern> </servlet-mapping> <servlet> <servlet-name>servletupdate</servlet-name> <servlet-class>servletupdate.update</servlet-class> </servlet> <servlet-mapping> <servlet-name>servletupdate</servlet-name> <url-pattern>/servletupdate.update</url-pattern> </servlet-mapping> </web-app>