1、练习设计思想
编web应用有两种,一种纯JSP,一种是JSP+JavaBean,由于对后者了解不深,这道题先使用JSP,后者方法在以后的博客中实现。
先使用jsp制作课程添加web页面,使用request内置命令获得客户端提交的HTTP协议数据,getParameter(String name)获得URL中的第一个name参数对应的值,setAttribute(String key,object value)设置map映射中键key对应的值对象value,在jsp读取时判断课程名称,任课 教师,上课地点是否符合要求,不符合要求时,输出信息,并调转返回,否则,添加course;需要建立course类,包含get,set函数;需要建立ICourseDao的接口,用于增加课程;需要调用补充接口,sql创建语句传输对象,preparedStatement接收结果集;在Util中创建DBUtil创建连接对象connection,与数据库连接,并创建CourseException.java文件继承RuntimeException,用于处理异常信息。创建数据库时在命令行输入create database name;建立名为“name”的数据库,(也可以直接在Navicat Premium(数据库管理工具)中建立)点开数据库,输入“Ctrl q”,进入编辑框,输入命令,表建立成功。
create table t_user(id int(10) primary key auto_increment,username varchar(255),password varchar(255),nickname varchar(255),type int(2),status int(2)//仅是个范例,与本题建立数据库无关。);
2、源程序代码
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 7 <title>增加信息</title> 8 </head> 9 <body> 10 <%=request.getAttribute("error") %> 11 <form action="add.jsp" method="get"> 12 <table align="center" border="1" width="500"> 13 <tr> 14 <td>课程名称 : </td> 15 <td> 16 <input type="text" name="coursename" /> 17 </td> 18 </tr> 19 <tr> 20 <td>任课教师:</td> 21 <td> 22 <input type="text" name="teacher" /> 23 </td> 24 </tr> 25 <tr> 26 <td>上课地点:</td> 27 <td> 28 <input type="text" name="place" /> 29 </td> 30 </tr> 31 <tr align="center"> 32 <td colspan="2"> 33 <input type="submit" value="保存" /> 34 </td> 35 </tr> 36 </table> 37 </form> 38 </body> 39 </html>
1 <%@page import="com.jaovo1.msg.model.Course" %> 2 <%@page import="com.jaovo1.msg.dao.*" %> 3 <%@page import="com.jaovo1.msg.Util.DBUtil" %> 4 <%@page import="com.jaovo1.msg.Util.CourseException" %> 5 <%@ page language="java" contentType="text/html; charset=UTF-8" 6 pageEncoding="UTF-8"%> 7 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 8 <html> 9 <% 10 String coursename=request.getParameter("coursename"); 11 String teacher=request.getParameter("teacher"); 12 String place=request.getParameter("place"); 13 if(coursename==null||"".equals(coursename.trim())){ 14 request.setAttribute("error","请输入课程名称"); 15 %> 16 <jsp:forward page="addInput.jsp"></jsp:forward> 17 <% 18 } 19 if((!teacher.equals("王建民")&&!teacher.equals("刘立嘉")&&!teacher.equals("刘丹")&&!teacher.equals("王辉")&&!teacher.equals("杨子光"))&&(!place.startsWith("一教")&&!place.startsWith("二教")&&!place.startsWith("三教")&&!place.startsWith("基教"))){ 20 request.setAttribute("error", "上课地点、任课老师错误"); 21 %> 22 <jsp:forward page="addInput.jsp"></jsp:forward> 23 <% 24 } 25 if(!teacher.equals("王建民")&&!teacher.equals("刘立嘉")&&!teacher.equals("刘丹")&&!teacher.equals("王辉")&&!teacher.equals("杨子光")){ 26 request.setAttribute("error", "任课老师错误"); 27 %> 28 <jsp:forward page="addInput.jsp"></jsp:forward> 29 <% 30 } 31 if(!place.startsWith("一教")&&!place.startsWith("二教")&&!place.startsWith("三教")&&!place.startsWith("基教")){ 32 request.setAttribute("error", "上课地点错误"); 33 %> 34 <jsp:forward page="addInput.jsp"></jsp:forward> 35 <% 36 } 37 Course course=new Course(); 38 course.setCoursename(coursename); 39 course.setTeacher(teacher); 40 course.setPlace(place); 41 CourseImpl courseDao=new CourseImpl(); 42 try{ 43 courseDao.add(course); 44 %> 45 <body> 46 课程保存成功!!<br> 47 <a href="addInput.jsp">继续添加</a> 48 </body> 49 50 <% 51 }catch(CourseException e){ 52 %> 53 <h2 style="color:red ; font-size:50px">发生错误 <%=e.getMessage() %></h2> 54 <% 55 } 56 %> 57 </html>
1 package com.jaovo1.msg.model; 2 public class Course { 3 private String coursename; 4 private String teacher; 5 private String place; 6 public String getCoursename() { 7 return coursename; 8 } 9 public void setCoursename(String coursename) { 10 this.coursename = coursename; 11 } 12 public String getTeacher() { 13 return teacher; 14 } 15 public void setTeacher(String teacher) { 16 this.teacher = teacher; 17 } 18 public String getPlace() { 19 return place; 20 } 21 public void setPlace(String place) { 22 this.place = place; 23 } 24 25 }
1 package com.jaovo1.msg.dao; 2 import com.jaovo1.msg.model.Course; 3 public interface ICourseDao { 4 public void add(Course course); 5 6 }
1 package com.jaovo1.msg.dao; 2 import java.sql.Connection; 3 import java.sql.PreparedStatement; 4 import java.sql.ResultSet; 5 import java.sql.SQLException; 6 7 import com.jaovo1.msg.Util.CourseException; 8 import com.jaovo1.msg.Util.DBUtil; 9 import com.jaovo1.msg.model.Course; 10 import sun.net.www.content.text.plain; 11 public class CourseImpl implements ICourseDao{ 12 13 @Override 14 public void add(Course course) { 15 Connection connection=DBUtil.getConnection(); 16 String sql="select count(*)from t_user where coursename=?"; 17 //创建语句传输对象 18 PreparedStatement preparedStatement=null; 19 ResultSet resultSet=null; 20 try { 21 preparedStatement=(PreparedStatement) connection.prepareStatement(sql); 22 preparedStatement.setString(1, course.getCoursename()); 23 //接收结果集 24 resultSet=preparedStatement.executeQuery(); 25 while(resultSet.next()) { 26 if(resultSet.getInt(1)>0) { 27 throw new CourseException("课程名已存在"); 28 } 29 } 30 sql="insert into t_user(coursename,teacher,place) value(?,?,?)"; 31 preparedStatement = connection.prepareStatement(sql); 32 preparedStatement.setString(1, course.getCoursename()); 33 preparedStatement.setString(2, course.getTeacher()); 34 preparedStatement.setString(3, course.getPlace()); 35 preparedStatement.executeUpdate(); 36 } catch (SQLException e) { 37 // TODO Auto-generated catch block 38 e.printStackTrace(); 39 }finally { 40 DBUtil.close(resultSet); 41 DBUtil.close(connection); 42 DBUtil.close(preparedStatement); 43 } 44 45 } 46 47 }
1 package com.jaovo1.msg.Util; 2 3 public class CourseException extends RuntimeException{ 4 5 public CourseException() { 6 super(); 7 // TODO Auto-generated constructor stub 8 } 9 10 public CourseException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { 11 super(message, cause, enableSuppression, writableStackTrace); 12 // TODO Auto-generated constructor stub 13 } 14 15 public CourseException(String message, Throwable cause) { 16 super(message, cause); 17 // TODO Auto-generated constructor stub 18 } 19 20 public CourseException(String message) { 21 super(message); 22 // TODO Auto-generated constructor stub 23 } 24 25 public CourseException(Throwable cause) { 26 super(cause); 27 // TODO Auto-generated constructor stub 28 } 29 30 }
1 package com.jaovo1.msg.Util; 2 3 import java.sql.DriverManager; 4 import java.sql.PreparedStatement; 5 import java.sql.ResultSet; 6 import java.sql.SQLException; 7 8 import com.mysql.jdbc.Connection; 9 10 public class DBUtil { 11 public static Connection getConnection() { 12 //1加载驱动 13 try { 14 Class.forName("com.mysql.jdbc.Driver").newInstance(); 15 } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) { 16 // TODO Auto-generated catch block 17 e.printStackTrace(); 18 } 19 String user ="root"; 20 String password="root"; 21 String url="jdbc:mysql://localhost:3306/javo_msg"; 22 //test 23 Connection connection=null; 24 try { 25 //2创建连接对象connection 26 connection=(Connection) DriverManager.getConnection(url, user, password); 27 } catch (SQLException e) { 28 // TODO Auto-generated catch block 29 e.printStackTrace(); 30 } 31 return connection; 32 } 33 //关闭资源方法 34 public static void close(java.sql.Connection connection) { 35 try { 36 if(connection!=null) 37 { 38 connection.close(); 39 } 40 } catch (SQLException e) { 41 // TODO Auto-generated catch block 42 e.printStackTrace(); 43 } 44 } 45 public static void close(PreparedStatement preparedStatement) { 46 try 47 {if(preparedStatement!=null) 48 { 49 preparedStatement.close(); 50 } 51 }catch(SQLException e) 52 { 53 e.printStackTrace(); 54 } 55 } 56 public static void close(ResultSet resultSet) { 57 try 58 {if(resultSet!=null) 59 { 60 resultSet.close(); 61 } 62 }catch(SQLException e) 63 { 64 e.printStackTrace(); 65 } 66 } 67 68 69 }
3、运行结果截图
(输错后报错,页面将跳到重新输入的空白格里,为了显示错误结果,截图使两者同框)
4、按照PSP0级的要求记录开发过程中的项目计划日志、时间记录日志、缺陷记录日志
5、总结
目前调试程序,易出现404,500,空指针异常,jsp异常,乱码等问题,大多数情况下由于自己编写代码粗心,而且这种错误难找,因此,尽量少犯下这些错误,在今后学习中学会总结各种错误的原因。本程序存在一些细节性问题,判断语句在jsp中写,安全性降低,在后来学习中,学会将判断打包,增强安全性。学会总结,化为自己的东西,一切不要着急,慢慢来,bug慢慢改,大不了重修呗,你又不是没勇气。软工之路漫漫长道,扎实走好每一步,急于求成,反而不进。相信自己,学计算机的孩子不认输。