软件工程概论 课堂练习 添加课程信息
设计思路:
想要实现这个功能,首先要连接数据库,在jsp页面中输入内容,通过调用函数,获取输入的内容,然后通过数据库的执行语句将其存进数据库的表中。
还要判断输入的内容是否为空、是否是要求的内容。
源代码:
package bean; public class Msg { String classname; String place; String teacher; public String getClassname() { return classname; } public void setClassname(String classname) { this.classname = classname; } public String getPlace() { return place; } public void setPlace(String place) { this.place = place; } public String getTeacher() { return teacher; } public void setTeacher(String teacher) { this.teacher = teacher; } }
实现功能:
package dao; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import util.ClassException; import bean.Msg; import util.DButil; public class ClassMsg { public void add(Msg msg) { Connection connection = DButil.getConnection(); PreparedStatement preparedStatement = null; try { String sql = "insert into class_msg (classname,teacher,place) value (?,?,?)"; preparedStatement = connection.prepareStatement(sql); preparedStatement.setString(1, msg.getClassname()); preparedStatement.setString(2,msg.getTeacher()); preparedStatement.setString(3,msg.getPlace()); preparedStatement.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); }finally { DButil.close(connection); } } }
异常:
package util; public class ClassException extends RuntimeException{ public ClassException() { super(); // TODO Auto-generated constructor stub } public ClassException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { super(message, cause, enableSuppression, writableStackTrace); // TODO Auto-generated constructor stub } public ClassException(String message, Throwable cause) { super(message, cause); // TODO Auto-generated constructor stub } public ClassException(String message) { super(message); // TODO Auto-generated constructor stub } public ClassException(Throwable cause) { super(cause); // TODO Auto-generated constructor stub } }
链接数据库:
package util; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; public class DButil { public static Connection getConnection() { try { Class.forName("com.mysql.jdbc.Driver").newInstance(); } catch (InstantiationException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } catch (IllegalAccessException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } catch (ClassNotFoundException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } String user="****"; String password="****"; String url = "jdbc:mysql://localhost:3306/user_msg"; Connection connection=null; try { connection=DriverManager.getConnection(url,user,password); } catch (SQLException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } return connection; } public static void close(Connection connection) { try { connection.close(); } catch (SQLException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } } public static void close(PreparedStatement preparedStatement) { try { preparedStatement.close(); } catch (SQLException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } } public static void close(ResultSet resultSet) { try { resultSet.close(); } catch (SQLException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } } }
判断输入是否为空并返回为空的结果:
package util; import java.util.HashMap; import java.util.Map; import javax.servlet.http.HttpServletRequest; public class ValidateUtil { public static boolean validateNull(HttpServletRequest request,String[] fileds) { boolean validate = true; //map对象用来装载不同的错误信息 Map<String,String> errorMsg = new HashMap(); for(String filed :fileds) { String value = request.getParameter(filed); if (value == null || "".equals(value.trim())) { validate = false; errorMsg.put(filed, "这一行不能为空!"); } if (!validate) { request.setAttribute("errormsg", errorMsg); } } return validate; } public static String showError(HttpServletRequest request , String filed) { Map<String, String> errorMsg = (Map<String,String>)request.getAttribute("errormsg"); if (errorMsg == null) { return ""; } String msg = errorMsg.get(filed); if (msg == null) { return ""; } return msg; } }
页面:
<%@page import="java.util.Map"%> <%@page import="util.ValidateUtil"%> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>添加课程</title> </head> <body style="background-image:url('../image/back.jpg');"> <%Map<String,String> errorMsg = (Map<String,String>)request.getAttribute("errormsg"); %> <form action="add2.jsp" method="get"> <br> <center style="color:black ; font-family:楷体;font-size:15px" > 课程名称:<input type="text" name="classname" /><%= ValidateUtil.showError(request, "classname") %></center> <br> <center style="color:black ; font-family:楷体;font-size:15px" > 任课教师:<input type="text" name="teacher" /><%= ValidateUtil.showError(request, "teacher") %></center> <br> <center style="color:black ; font-family:楷体;font-size:15px" > 上课地点:<input type="text" name="place" /><%= ValidateUtil.showError(request, "place") %></center> <br> <center><input type="submit" value="提交" /> </center> </form> </body> </html>
页面:
<%@page import="util.ValidateUtil"%> <%@page import="util.ClassException"%> <%@page import="bean.Msg"%> <%@page import="dao.ClassMsg"%> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>添加课程</title> </head> <body style="background-image:url('../image/back.jpg');"> <% //接收客户端传递过来的参数 String classname = request.getParameter("classname"); String teacher = request.getParameter("teacher"); String place = request.getParameter("place"); boolean validate = ValidateUtil.validateNull(request, new String[]{"classname","teacher","place"}); if(!validate){ %> <jsp:forward page="add.jsp"></jsp:forward> <% } if(!place.startsWith("基教")&&!place.startsWith("一教")&&!place.startsWith("二教")&&!place.startsWith("三教")) { %> <script language='javaScript' > alert('没有这个教室\n请重新输入!');window.history.back(-1);</script> <% } else if(!teacher.equals("王建民")&&!teacher.equals("刘立嘉")&&!teacher.equals("刘丹")&&!teacher.equals("王辉")&&!teacher.equals("杨子光")) { %> <script language='javaScript' > alert('没有这个老师!\n请重新输入');window.history.back(-1);</script> <% } else { Msg msg=new Msg(); msg.setClassname(classname); msg.setTeacher(teacher); msg.setPlace(place); ClassMsg cm=new ClassMsg(); cm.add(msg); %> <center>保存成功!!<br></center> <center><a href="add.jsp">继续添加</a></center> <% } %> </body> </html>
结果:
开始页面:
若不输入内容:
如果输入错误地点:
点击确定后 可以重新输入
如果输入错误的老师名称:
如果输入正确:
输入前的数据库表
输入后的表