課堂練習-增長信息

 

 

 

一、練習設計思想html

編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)
//僅是個範例,與本題創建數據庫無關。
);

二、源程序代碼java

 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>
addInput.jsp
 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>
add.jsp
 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 }
Course.java
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 }
ICourseDao.java
 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 }
CourseImpl.java
 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 }
CourseException.java
 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 }
DBUtil.java

 

三、運行結果截圖mysql

(輸錯後報錯,頁面將跳到從新輸入的空白格里,爲了顯示錯誤結果,截圖使二者同框)web

 

 

 

四、按照PSP0級的要求記錄開發過程當中的項目計劃日誌、時間記錄日誌、缺陷記錄日誌sql

 

五、總結數據庫

目前調試程序,易出現404,500,空指針異常,jsp異常,亂碼等問題,大多數狀況下因爲本身編寫代碼粗心,並且這種錯誤難找,所以,儘可能少犯下這些錯誤,在從此學習中學會總結各類錯誤的緣由。本程序存在一些細節性問題,判斷語句在jsp中寫,安全性下降,在後來學習中,學會將判斷打包,加強安全性。學會總結,化爲本身的東西,一切不要着急,慢慢來,bug慢慢改,大不了重修唄,你又不是沒勇氣。軟工之路漫漫長道,紮實走好每一步,急於求成,反而不進。相信本身,學計算機的孩子不認輸。安全

相關文章
相關標籤/搜索