1. 程式人生 > 其它 >行程統計(期中考試)資料庫的增

行程統計(期中考試)資料庫的增

這次期中考試主要考資料庫的連線和將資料新增進資料庫中有需要的可以接著往下看(新手推薦做一下):

石家莊鐵道大學在校學生行程統計(20分)

考試時間:180分鐘

1、專案需求:

為了有效防止新冠疫情的傳播,急需開發一套在校學生行程統計系統,完成資訊統計,提前準備,有效保護在校學生的安全。

2.系統要求與功能設計

2.1 頁面功能要求

(1)能夠在Tomcat伺服器中正確部署,並通過瀏覽器檢視;(1分)

(2)網站頁面整體風格統一;

(3)石家莊鐵道大學在校學生行程統計頁面,頁面效果如圖所示:(15分)

頁面詳細資訊如下表所示:

資訊標題

資訊型別

填寫要求

姓名

文字框

學號

文字框

要求輸入八位數字

學生類別

單選框

單選項:本科生或者研究生

院系

下拉列表框

下來列表框內容包括(土木學院、機械學院、交通學院、資訊學院、經管學院)

聯絡電話

文字框

要求輸入11位數字

健康碼顏色

單選框

單選選項(綠碼、黃碼、紅碼)

行程統計

複選框

□10月30日去過人民醫院

□10月25日以來去過深澤縣人民醫院

□10月16日以來去過深澤縣莊澤村

□10月29日以來去過黑龍江哈爾濱市或者黑河市

□10月18日以來途徑貴州遵義市;北京豐臺、昌平

□10月17日以來到過湖南長沙;青海海東市

(以上選項可以多選)

其他涉疫資訊需要填報的

文字框

②點選“提交”按鈕,儲存成功則提示資訊“填報成功”,失敗則提示“資訊填報錯誤”,並返回當前頁面

評分標準:

①完成石家莊鐵道大學在校學生行程統計頁面(未完成0分,完成2分);

②儲存行程資訊入庫(未完成0分,完成6分);

③學號和聯絡電話判斷是否為指定位數的數字。(未完成0分,完成一個0.5,全部完成1分);

④學生類別和健康碼顏色實現單選框選擇功能(未完成0分,完成一個0.5,全部完成1分);

⑤實現院系下拉框功能(未完成0分,完成1分)。

⑥實現行程統計複選框功能(未完成0分,完成2分)

⑦實現提交後資訊提示功能;(未完成0分,完成2分)

2.2 功能要求

(1)設計出合理的資料庫和資料表,要求使用mysql、sqlserver、oracle三種資料庫中一種(1分)

(2)使用Serverlet實現資訊提交功能(1分)。

(3)使用Java Bean封裝資料庫連線操作(2分。)

 **************************************************************************************************************************

   我就簡單說一下我做題的流程:審題。然後,我先在建立了資料庫然後添加了列名、資料型別,長度。

建立完資料庫後建立四個:Bean,Dao(工具類),Servlet(資料互動),Util(連線資料庫)

bean包和Util包比較好做。下面給上兩個包的程式碼

package Bean;

public class Information {
private String name;
private String number;
private String leibie;
private String yuanxi;
private String phone;;
private String color;
private String xincheng;
private String information;
public String getName() {
	return name;
}
public void setName(String name) {
	this.name = name;
}
public String getNumber() {
	return number;
}
public void setNumber(String number) {
	this.number = number;
}
public String getLeibie() {
	return leibie;
}
public void setLeibie(String leibie) {
	this.leibie = leibie;
}
public String getYuanxi() {
	return yuanxi;
}
public void setYuanxi(String yuanxi) {
	this.yuanxi = yuanxi;
}
public String getPhone() {
	return phone;
}
public void setPhone(String phone) {
	this.phone = phone;
}
public String getColor() {
	return color;
}
public void setColor(String color) {
	this.color = color;
}
public String getXincheng() {
	return xincheng;
}
public void setXincheng(String xincheng) {
	this.xincheng = xincheng;
}
public String getInformation() {
	return information;
}
public void setInformation(String information) {
	this.information = information;
}
}

  

package Dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import Bean.Information;
import Util.Utils;

public class add {
	//建立連線物件,載體,結果集物件
//		private static Connection conn=null;
//		private static PreparedStatement ps=null;
//		private static ResultSet rs=null;

	public void add(Information A) throws ClassNotFoundException, SQLException {
		Connection connection =Utils.getConnection();
		String sql = "INSERT INTO test (name,number,leibie,yuanxi,phone,color,xincheng,information) values(?,?,?,?,?,?,?,?)";
		PreparedStatement preparedStatement = null;//載體
		try {
	         //
	         preparedStatement = connection.prepareStatement(sql);
	       // preparedStatement = connection.prepareStatement(sql);
	         preparedStatement.setString(1, A.getName());
	         preparedStatement.setString(2, A.getNumber());
	         preparedStatement.setString(3, A.getLeibie());
	         preparedStatement.setString(4, A.getYuanxi());
	         preparedStatement.setString(5, A.getPhone());
	         preparedStatement.setString(6, A.getColor());
	         preparedStatement.setString(7, A.getXincheng());
	         preparedStatement.setString(8, A.getInformation());
	         System.out.println(A.getNumber());//測試用
	         preparedStatement.executeUpdate();
	         } catch (SQLException e) {
	            // TODO Auto-generated catch block
	           e.printStackTrace();
	        }finally {

	        }
	}
}

  做完這兩個包之後我開始做介面:(介面這部分包括了幾種增加的方法文字框,下拉框,多選,單選)大家可以看下面的例子或者看之前的部落格之前發的有相關的部落格

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>

<body>
<div id="addSubjectForm" align="center">
<form action="add1" method="post">
<tr>
<h2>請輸入人口資訊</h2>
</tr>

<table align="center"> 

 <tr>
    <td>姓名:</td>
    <td> 
     <input type="text" name="name" > 
     </td> 
    </tr>
    <tr>
    <td>學號:</td>
    <td> 
     <input type="text" name="number"onblur="isCardtelnumber(this.value)" >  
     </td> 
    </tr>
<tr>
    <td>學生類別:</td>
     <td> 
 <input type="radio" name="leibie" value="本科生">本科生
 <input type="radio" name="leibie" value="研究生" checked>研究生

 </td>
    </tr>
    
   
<tr>
    <td>院系:</td>
    <td>
     <select name="yuanxi"> 
     <option value="土木學院">土木學院</option> 
     <option value="機械學院">機械學院</option> 
     <option value="交通學院">交通學院</option>
     <option value="資訊學院" selected>資訊學院</option> 
     <option value="經管學院">經管學院</option> 
     </select> 
    </td>
</tr>
    
    <tr>
    <td>聯絡電話:</td>
    <td> 
     <input type="text" name="phone" onblur="isCardtelphone(this.value)">  
     </td> 
    </tr>

<tr>
    <td>健康碼顏色:</td>
     <td> 
 <input type="radio" name="color" value="綠碼">綠碼
 <input type="radio" name="color" value="黃碼" checked>黃碼
 <input type="radio" name="color" value="紅碼">紅碼
 </td>
    </tr>    
    
    <tr>
    <td>行程統計:</td> 
		<tr><td> <input type="checkbox" name="xincheng" value="10月30日去過人民醫院">10月30日去過人民醫院</td></tr>
		<tr><td><input type="checkbox" name="xincheng" value="10月25日以來去過深澤縣人民醫院">10月25日以來去過深澤縣人民醫院</td></tr>
		<tr><td><input type="checkbox" name="xincheng" value="10月16日以來去過深澤縣莊澤村">10月16日以來去過深澤縣莊澤村</td></tr>
		<tr><td><input type="checkbox" name="xincheng" value="10月29日以來去過黑龍江哈爾濱市或者黑河市">10月29日以來去過黑龍江哈爾濱市或者黑河市</td></tr>
		<tr><td><input type="checkbox" name="xincheng" value="10月18日以來途徑貴州遵義市;北京豐臺、昌平">10月18日以來途徑貴州遵義市;北京豐臺、昌平</td></tr>
		<tr><td><input type="checkbox" name="xincheng" value="10月17日以來到過湖南長沙;青海海東市">10月17日以來到過湖南長沙;青海海東市</td></tr>
    </tr>    

<tr>

    <td>其他涉疫資訊需要填報的:</td>
    <td>
    <input type="text" name="information">
    </td>
</tr>
<td>
 <input type="submit" value="提交" />
<input type="reset" value="重置" />
<input type="button" name="Submit" onclick="javascript:history.back(-1);" value="返回上一頁">
</td>
</body>

<script type="text/javascript">
/*function check()                        //封裝一個<body>中做成點選事件的函式
{
    
    if($('input:radio[name="classname"]:checked').val()==null) {
          alert('戶別不能為空!');
          document.getElementById('classname').focus();
          return false;
         }
    if($('input:radio[name="classteacher"]:checked').val()==null) {
          alert('住房型別不能為空!');
          document.getElementById('classteacher').focus();
          return false;
         }
    if($('input:radio[name="sex"]:checked').val()==null) {
          alert('性別不能為空!');
          document.getElementById('sex').focus();
          return false;
         }
    if(document.getElementById('classplace').value=='') {
          alert('現住房面積不能為空!');
          document.getElementById('classplace').focus();
          isInterger(classplace);
          return false;
         }
    
    if(document.getElementById('calssnum').value=='') {
          alert('現住房間數不能為空!');
          document.getElementById('classnum').focus();
          return false;
         }
    
    if(document.getElementById('zhuname').value=='') {
          alert('戶主姓名不能為空!');
          document.getElementById('zhuname').focus();
          return false;
         }
    
    
    if(document.getElementById('minzu').value=='') {
          alert('民族不能為空!');
          document.getElementById('minzu').focus();
          return false;
         }
     if(document.getElementById('education').value=='') {
          alert('受教育程度不能為空!');
          document.getElementById('education').focus();
          return false;
         }
    return true;
}*/
/*function isCardNo(card) 
{ 
  // 身份證號碼為15位或者18位,15位時全為數字,18位前17位為數字,最後一位是校驗位,可能為數字或字元X 
  var reg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/; 
  if(reg.test(card) === false) 
  { 
    alert("身份證輸入不合法"); 
    document.getElementById('ID').value="";
  } 
}*/
function isCardxuehao(card) 
{ 
  // 身份證號碼為15位或者18位,15位時全為數字,18位前17位為數字,最後一位是校驗位,可能為數字或字元X 
  var reg = /(^\d{8}$)/; 
  if(reg.test(card) === false) 
  { 
    alert("學號輸入不合法"); 
    document.getElementById('number').value="";
  } 
}
function isCardtelnumber(card) 
{ 
  // 身份證號碼為15位或者18位,15位時全為數字,18位前17位為數字,最後一位是校驗位,可能為數字或字元X 
  var reg = /(^\d{11}$)/; 
  if(reg.test(card) === false) 
  { 
    alert("電話號碼輸入不合法"); 
    document.getElementById('telnumber').value="";
  } 
}

</script>
</html>

   做完介面開始做工具類和Serviet包

package Servlet;

import java.io.IOException;
import java.sql.SQLException;

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 Bean.Information;
import Dao.add;

/**
 * Servlet implementation class add
 */
public class add1 extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public add1() {
        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
		doPost(request,response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		// TODO Auto-generated method stub
				request.setCharacterEncoding("utf-8");
				//������Ӧ���ı�����
				response.setContentType("text/html;charset=utf-8");//設定字符集
				String number = request.getParameter("number");
				
				String	name = request.getParameter("name");
				String leibie = request.getParameter("leibie");
				String yuanxi = request.getParameter("yuanxi");
				String phone = request.getParameter("phone");
				String	color = request.getParameter("color");
				String[] xincheng1 = request.getParameterValues("xincheng");
				String information = request.getParameter("information");
//				System.out.println(number);
				String xincheng="";
				Information B = new Information();
				
				B.setNumber(number);
				B.setName(name);
				B.setLeibie(leibie);
				B.setYuanxi(yuanxi);
				B.setPhone(phone);
				B.setColor(color);
				for(int i=0;i<xincheng1.length;i++)
				{
					xincheng=xincheng1[i]+xincheng;
				}
				B.setXincheng(xincheng);
				B.setInformation(information);
//				response.sendRedirect("showall.jsp");\
				add A=new add();
				try {
					A.add(B);
				} catch (ClassNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				response.getWriter().append("新增成功");
				}
			}

  

package Util;


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;



/**
 * JDBC��������
 */
public class Utils{
    public static Connection getConnection() throws ClassNotFoundException, SQLException {
    
    	   Connection connection = null;//建立連線物件
    	   PreparedStatement ps = null;////建立載體
    	   ResultSet rs = null;//建立結果集物件
        Class.forName("com.mysql.cj.jdbc.Driver");
         connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/xingcheng","root", "jiang123456");
       return connection;
    }
    
    public static void close(Connection connection ) {
         try {
           if (connection != null) {
                connection.close();
             }
           
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
       }
     }
     public static void close(PreparedStatement preparedStatement ) {
         try {
            if (preparedStatement != null) {
                 preparedStatement.close();
             }            
        } catch (SQLException e) {
             // TODO Auto-generated catch block
             e.printStackTrace();
         }
     }
     public static void close(ResultSet resultSet ) {
        try {
            if (resultSet != null) {
                 resultSet.close();
             }
             
         } catch (SQLException e) {
             // TODO Auto-generated catch block
             e.printStackTrace();
         }
     }
     
 }