1登陸帳號:要求由6到12位字母、數字、下劃線組成,只有字母能夠開頭;(1分)javascript
2登陸密碼:要求顯示「• 」或「*」表示輸入位數,密碼要求八位以上字母、數字組成。(1分)html
3性別:要求用單選框或下拉框實現,選項只有「男」或「女」;(1分)java
4學號:要求八位數字組成,前四位爲「2018」開頭,輸入本身學號;(1分)mysql
5姓名:輸入本身的姓名;sql
5電子郵箱:要求正確判斷格式xxxx@xxxx.xxxx;(1分)數據庫
6點擊「添加」按鈕,將學生我的信息存儲到數據庫中。(3分)jsp
7能夠演示鏈接上數據庫。(2分)ide
首先在jsp頁面進行表單數據的校驗,若是校驗不成功則提示錯誤,而後另用戶從新輸入數據,若是校驗成功,則將表單中的數據發送到servlet層,servlet層接收到數據後開始進行數據的存放,而後調用Dao層的add函數來進行數據庫的寫入,這時Dao層調用DBUtil來註冊驅動和獲取數據庫的鏈接,而後進行數據的寫入,若是寫入成功則返回true,寫入失敗則返回false,servlet層接收到Dao層的返回值後進行不一樣的處理,若是寫入成功則向註冊頁面發送註冊成功的消息,反之則發生註冊失敗的消息。函數
註冊界面工具
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>註冊界面</title> <script type="text/javascript"> /*表單校驗*/ function check() { f=0; var re = /^[\w\u4e00-\u9fa5]{6,8}$/; //判斷字符串是否爲數字和字母組合 var myPattern = new RegExp("^[a-zA-Z]"); // 以英文字母開頭 var username = document.getElementById("username").value; //alert(username.length); if(!(username.length>5&&username.length<13)){ alert("用戶名長度錯誤!");return false; } else if(!(re.test(username))){ alert("用戶名組成內容錯誤!");return false; }else if(!(myPattern.exec(username))){ alert("用戶名開頭必須是字母!");return false; } var password = document.getElementById("password").value; if(password.length<8){f++;alert("密碼長度錯誤!");} return false; var xuehao = document.getElementById("xuehao").value; if(xuehao.length!=8){ alert("學號長度錯誤!");return false; } if(xuehao[0]=='2'&&xuehao[1]=='0'&&xuehao[2]=='1'&&xuehao[3]=='8'){f++;} else{ alert("學號格式錯誤!");return false; } var mail = document.getElementById("mail").value; if(/^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+/.test(mail)){f++;} else{ alert("郵箱格式錯誤!");return false; } if(f>1){ alert("添加成功"); return true; } else{ return false; } } </script> </head> <body background="1.jpg"> <form action="UserServlet?method=add" method="post" onsubmit="return check()"><br/><br/><br/><br/><br/><br/><br/> <table align="center"> <tr> <td>登入帳號:</td> <td><input type="text" id="username" name="username" value="" ></td> </tr> <tr> <td>登入密碼:</td> <td> <input type="password" id="password" name="password" value="" ></td> </tr> <tr> <td>性 別:</td> <td> <select id="sex" name="sex" > <option>--請選擇--</option> <option value="男">男</option> <option value="女">女</option> </select> </td> </tr> <tr> <td>姓 名:</td> <td> <input type="text" name="name" value="" ></td> </tr> <tr> <td>學 號:</td> <td> <input type="text" name="xuehao" id="xuehao" value="" ></td> </tr> <tr> <td>電子郵件:</td> <td> <input type="text" id="mail" name="mail" value="" ></td> </tr> <tr> <td>所在學院:</td> <td> <input type="text" id="xueyuan" name="xueyuan" value="" ></td> </tr> <tr> <td>所在系:</td> <td> <input type="text" id="xi" name="xi" value="" ></td> </tr> <tr> <td>所在班級:</td> <td> <input type="text" id="ban" name="ban" value="" ></td> </tr> <tr> <td>入學年份(屆):</td> <td> <select id="year" name="year"> <option>--請選擇--</option> <option value="2018">2018</option> <option value="2017">2019</option> <option value="2016">2018</option> <option value="2015">2019</option> <option value="2014">2018</option> </select> </td> </tr> <tr> <td>生源地:</td> <td> <select name="address"> <option value ="河北省">河北省</option> <option value ="北京市">北京市</option> <option value ="天津市">天津市</option> </select> </td> </tr> <tr> <td>備註:</td> <td> <input type="text" id="beizhu" name="beizhu" value="" ></td> </tr> <tr> <td> <button type="submit" >添 加</button></td> </tr> </table> </form> </body> </html>
DBUtil.java
package zhuce; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.SQLException; import java.sql.Statement; //數據庫資源管理工具 public class DBUtil { private static Connection conn; private static Statement stmt; private static PreparedStatement pstmt; static { // 加載驅動 try { Class.forName("com.mysql.jdbc.Driver"); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } // 實例化數據庫鏈接conn public static Connection getConnection() { try { conn = DriverManager.getConnection( "jdbc:mysql://localhost:3306/user?user=true&characterEncoding=UTF-8", "root", "123456"); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return conn; } // 實例化SQL執行句柄stmt public static Statement getStatement() { Connection conn = getConnection(); try { if (conn != null) { stmt = conn.createStatement(); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return stmt; } // 實例化SQL執行句柄pstmt public static PreparedStatement getPreparedStatement(String sql) { Connection conn = getConnection(); try { if (conn != null) { pstmt = conn.prepareStatement(sql); } } catch (SQLException e) { e.printStackTrace(); } return pstmt; } // 關閉數據庫鏈接資源 public static void closeDBResources() { try { if (pstmt != null && !pstmt.isClosed()) { pstmt.close(); } if (stmt != null && !stmt.isClosed()) {// 若是stmt不爲空,而且還未關閉 stmt.close(); } if (conn != null && !conn.isClosed()) { conn.close(); } } catch (SQLException e) { e.printStackTrace(); } } }
ZhuceDao.java
package zhuce; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import zhuce.DBUtil; import zhuce.Zhuce; public class ZhuceDao { // 保存信息 public void saveZhuce(Zhuce zhuce) { String sql = "insert into zhucebiaodan(user,password,name,sex,diqu,phone,youxiang)values(?,?,?,?,?,?,?)"; PreparedStatement pstmt = DBUtil.getPreparedStatement(sql); // 設置參數 try { pstmt.setString(1, zhuce.getUser()); pstmt.setString(2, zhuce.getPassword()); pstmt.setString(3, zhuce.getName()); pstmt.setString(4, zhuce.getSex()); pstmt.setString(5, zhuce.getDiqu()); pstmt.setString(6, zhuce.getPhone()); pstmt.setString(7, zhuce.getYouxiang()); pstmt.executeUpdate();// 更新數據 } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } // 刪除學生 public void deleteZhuce(String user) { String sql = "delete from zhucebiaodan where user = ? "; PreparedStatement pstmt = DBUtil.getPreparedStatement(sql); try { pstmt.setString(1, user); pstmt.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); } finally { DBUtil.closeDBResources(); } } // 查詢全部信息 public static List<Zhuce> findAllZhuce() { List<Zhuce> zhucelist = new ArrayList<Zhuce>(); String sql = "select user,password,name,sex,diqu,phone,youxiang from zhucebiaodan "; PreparedStatement pstmt = DBUtil.getPreparedStatement(sql); // 設置參數 try { ResultSet rs = pstmt.executeQuery();// 執行獲得結果集 while (rs.next()) {// 每循環一次就找到一個學生 String user = rs.getString("user"); String password = rs.getString("password"); String name = rs.getString("name"); String sex = rs.getString("sex"); String diqu = rs.getString("diqu"); String phone = rs.getString("phone"); String youxiang = rs.getString("youxiang"); Zhuce zhuce = new Zhuce(user, password, name, sex, diqu, phone, youxiang); zhucelist.add(zhuce);// 將找到的學生對象加到集合裏面 } rs.close();// 關閉查詢資源 } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { // 關閉資源 DBUtil.closeDBResources(); } return zhucelist; } }
ZhuceServlet.java
package zhuce; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import zhuce.ZhuceService; import zhuce.Zhuce; public class ZhuceServlet extends HttpServlet { /** * */ private static final long serialVersionUID = 1L; @Override protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub request.setCharacterEncoding("utf-8");//設置編碼 String param=request.getParameter("param"); if("add".equals(param)) { String user=request.getParameter("user"); String password=request.getParameter("password"); String name=request.getParameter("name"); String sex=request.getParameter("sex"); String diqu=request.getParameter("diqu"); String phone=request.getParameter("phone"); String youxiang=request.getParameter("youxiang"); Zhuce zhuce=new Zhuce(user,password,name,sex,diqu,phone,youxiang); ZhuceService zs=new ZhuceService(); zs.saveZhuce(zhuce); System.out.println("鏈接servlet"); //重定向到index.jsp response.sendRedirect("index.jsp"); } if("delete".equals(param)) { String user = request.getParameter("user"); ZhuceService.deleteZhuce(user); // 重定向到index.jspt頁面 response.sendRedirect("index.jsp"); } } }
ZhuceService.java
package zhuce; import java.util.List; import zhuce.ZhuceDao; import zhuce.Zhuce; public class ZhuceService { private static ZhuceDao zhucedao=new ZhuceDao(); //添加信息 public void saveZhuce(Zhuce zhuce) { zhucedao.saveZhuce(zhuce); } //查詢全部信息 public List<Zhuce> findAllZhuce(){ return ZhuceDao.findAllZhuce(); } //刪除信息 public static void deleteZhuce(String user) { zhucedao.deleteZhuce(user); } }
一、zhuce.jsp
這是網頁的主頁面的代碼,在畫出頁面後加入了表單的校驗,當數據校驗成功後會將表單數據發送到servlet層。
二、ZhuceServlet.java
這個文件接收來自jsp頁面的數據,而後調用Dao層的函數來進行數據庫的寫入。
三、ZhuceDao.java
這個文件裏存放的是用來寫入數據庫的代碼,在該文件裏調用了DBUtil類來獲取和數據庫的鏈接。
四、DBUtil.java
這個文件裏存放的類是用來註冊驅動、獲取和數據庫的鏈接以及關閉相應的鏈接的方法,用來供TextLogin調用。