學生信息的添加 Java web簡單項目初試(修改)

錯誤緣由:javascript

1.Java web 的Servlet類沒有配置好,而且缺乏一個 Dao類(Date Access Object一般用於操做數據庫的).html

2.代碼的某些名稱錯誤,致使數據庫數據存儲錯誤。前端

添加的Dao類以下:java

 1 package com.Dao;  2 
 3 import java.sql.Connection;  4 import java.sql.PreparedStatement;  5 import java.sql.ResultSet;  6 import java.sql.SQLException;  7 import java.sql.Statement;  8 import java.util.ArrayList;  9 import java.util.List; 10 import com.DBUtil.UserDBUtil; 11 
12 import jdk.nashorn.internal.ir.RuntimeNode.Request; 13 
14 public class Dao { 15     public boolean add(String username,String password,String name,String studentID,String sex,String phone,String email,String xueyuan,String xi,String classes,String year,String shengyuandi,String beizhu) { 16         String sql = "insert into use02  values ('" + username + "','" + password +"','" + name +"','" +studentID +"','" + sex +"','" +phone +"','" + email +"','" +xueyuan +"','"+xi +"','"+classes +"','"+year+"','"+shengyuandi +"','"+beizhu +"')"; 17          // 建立數據庫連接
18         Connection conn = UserDBUtil.getConnection(); 19         Statement state = null; 20         boolean f = false; 21         int a = 0; 22         try { 23             state = conn.createStatement(); 24             a = state.executeUpdate(sql); 25         } catch (Exception e) { 26  e.printStackTrace(); 27         } finally { 28              // 關閉z 鏈接 29             //DB.close(state, conn);
30  } 31 
32         if (a > 0) { 33             f = true; 34  } 35         return f; 36  } 37 }

 

修改後的Servlet代碼以下:mysql

 1 import java.io.IOException;  2 
 3 
 4 import javax.servlet.ServletException;  5 import javax.servlet.ServletRequest;  6 import javax.servlet.annotation.WebServlet;  7 import javax.servlet.http.HttpServlet;  8 import javax.servlet.http.HttpServletRequest;  9 import javax.servlet.http.HttpServletResponse; 10 
11 import com.Dao.*; 12 import com.DBUtil.*; 13 
14 /**
15  * Servlet implementation class UserServlet 16  */
17 @WebServlet("/AddServlet") 18 public class AddServlet extends HttpServlet { 19     private static final long serialVersionUID = 1L; 20        
21     /**
22  * @see HttpServlet#HttpServlet() 23      */
24     public AddServlet() { 25         super(); 26         // TODO Auto-generated constructor stub
27  } 28     Dao dao = new Dao(); 29     
30     protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 31         req.setCharacterEncoding("utf-8"); 32         String method = req.getParameter("method"); 33         if ("add".equals(method)) { 34             
35  add(req, resp); 36             System.out.println("!!!"); 37  } 38  } 39     private void add(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException { 40         // TODO Auto-generated method stub
41         String username = req.getParameter("username"); 42          String password = req.getParameter("password"); 43         String name = req.getParameter("name"); 44         String studentID = req.getParameter("studentID"); 45         String sex = req.getParameter("sex"); 46         String phone = req.getParameter("phone"); 47         String email = req.getParameter("email"); 48         String xueyuan = req.getParameter("xueyuan"); 49         String xi = req.getParameter("xi"); 50         String classes = req.getParameter("classes"); 51         String year = req.getParameter("year"); 52         String shengyuandi = req.getParameter("shengyuandi"); 53         String beizhu = req.getParameter("beizhu"); 54         if(dao.add(username,password,name,studentID,sex,phone,email,xueyuan,xi,classes,year,shengyuandi,beizhu)) { 55             req.setAttribute("message", "註冊成功!"); 56             req.getRequestDispatcher("index1.jsp").forward(req, resp); 57             return; 58         }else { 59             req.setAttribute("message", "帳號重複,請從新輸入!"); 60             req.getRequestDispatcher("index1.jsp").forward(req, resp); 61             return; 62  } 63         //req.getRequestDispatcher("index1.jsp").forward(req, resp);
64  } 65 
66 
67 }

bean類jquery

 1 public class User {  2     private String username;  3     private String password;  4     private String name;  5     private String studentID;  6     private String sex;  7     private String phone;  8     private String email;  9     private String xueyuan;  10     private String xi;  11     private String classes;  12     private String year;  13     private String shengyuandi;  14     private String beizhu;  15     public String getUsername() {  16         return username;  17  }  18     public void setUsername(String username) {  19         this.username = username;  20  }  21     public String getPassword() {  22         return password;  23  }  24     public void setPassword(String password) {  25         this.password = password;  26  }  27     public String getName() {  28         return name;  29  }  30     public void setName(String name) {  31         this.name = name;  32  }  33     public String getStudentID() {  34         return studentID;  35  }  36     public void setStudentID(String studentID) {  37         this.studentID = studentID;  38  }  39     public String getSex() {  40         return sex;  41  }  42     public void setSex(String sex) {  43         this.sex = sex;  44  }  45     public String getPhone() {  46         return phone;  47  }  48     public void setPhone(String phone) {  49         this.phone = phone;  50  }  51     public String getEmail() {  52         return email;  53  }  54     public void setEmail(String email) {  55         this.email = email;  56  }  57     public String getXueyuan() {  58         return xueyuan;  59  }  60     public void setXueyuan(String xueyuan) {  61         this.xueyuan = xueyuan;  62  }  63     public String getXi() {  64         return xi;  65  }  66     public void setXi(String xi) {  67         this.xi = xi;  68  }  69     public String getClasses() {  70         return classes;  71  }  72     public void setClasses(String classes) {  73         this.classes = classes;  74  }  75     public String getYear() {  76         return year;  77  }  78     public void setYear(String year) {  79         this.year = year;  80  }  81     public String getShengyuandi() {  82         return shengyuandi;  83  }  84     public void setShengyuandi(String shengyuandi) {  85         this.shengyuandi = shengyuandi;  86  }  87     public String getBeizhu() {  88         return beizhu;  89  }  90     public void setBeizhu(String beizhu) {  91         this.beizhu = beizhu;  92  }  93     public User(String username, String password, String name, String studentID, String sex, String phone, String email,  94  String xueyuan, String xi, String classes, String year, String shengyuandi, String beizhu) {  95         super();  96         this.username = username;  97         this.password = password;  98         this.name = name;  99         this.studentID = studentID; 100         this.sex = sex; 101         this.phone = phone; 102         this.email = email; 103         this.xueyuan = xueyuan; 104         this.xi = xi; 105         this.classes = classes; 106         this.year = year; 107         this.shengyuandi = shengyuandi; 108         this.beizhu = beizhu; 109  } 110     
111     
112 
113 }

數據庫鏈接程序:web

 1 import java.sql.Connection;  2 
 3 import java.sql.DriverManager;  4 //import java.sql.ResultSet;
 5 import java.sql.SQLException;  6 //import java.sql.Statement;
 7 
 8 public class UserDBUtil {  9     static Connection con; 10     public static Connection getConnection() { 11         try { 12             Class.forName("com.mysql.cj.jdbc.Driver"); 13             System.out.println("數據庫驅動加載成功"); 14         } catch (ClassNotFoundException e) { 15             // TODO Auto-generated catch block
16  e.printStackTrace(); 17  } 18         
19         try { 20             con=DriverManager.getConnection("jdbc:mysql://localhost:3306/user?useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B88&serverTimezone=GMT%2B8&useSSL=false","root","101032"); 21             System.out.println("數據庫連接成功!"); 22         } catch (SQLException e) { 23             // TODO Auto-generated catch block
24  e.printStackTrace(); 25  } 26         
27         return con; 28  } 29     
30      public static void main(String[] args) { 31          UserDBUtil c=new UserDBUtil(); 32  c.getConnection(); 33         
34     }

jsp 頁面源代碼實現:正則表達式

 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <!DOCTYPE html>
 4 <html>
 5 <head>
 6 <meta charset="UTF-8">
 7 <title>用戶註冊</title>
 8 </head>
 9 <body>
 10 
 11 <script type="text/javascript" src="./js/jquery-3.3.1.min.js"></script>
 12 
 13 <div class="container">
 14     <section>
 15         <form action="AddServlet?method=add" method="post" onsubmit="return check(this)">        
 16             <span class="username">登錄帳號</span>
 17                 <input type="text" name="username" placeholder="  username" id="username">
 18             <br/>
 19             
 20             <span class="password">登錄密碼</span>
 21             <input type="password" name="password" placeholder="  password" id="password">
 22             <br/>
 23             
 24             <span class="name">姓名</span>
 25                 <input type="text" name="name" placeholder="  name" id="name">
 26             <br/>
 27             
 28             <span class="studentID">學號</span>
 29             <input type="text" name="studentID" placeholder="  studentID" id="studentID" >
 30             <br/>
 31             
 32             <span class="sex">性別</span>
 33                 <input type="radio" name="sex" id="sexnan" value="M"> 34                 &nbsp;  35                 <input type="radio" name="sex" id="sexnv" value="F"> 36             <br/>
 37             
 38 <!--             <span class="diqu">地區</span> -->
 39 <!--             <select name="diqu"> -->
 40 <!--                   <option value ="北京市">北京市</option> -->
 41 <!--                   <option value ="天津市">天津市</option> -->
 42 <!--                   <option value="河北省">河北省</option> -->
 43 <!--             </select> -->
 44 <!--             <br/> -->
 45             
 46             <span class="phone">手機號</span>
 47                 <input type="text" name="phone" placeholder="  phone" id="phone">
 48             <br/>
 49             
 50             <span class="email">郵箱</span>
 51                 <input type="text" name="email" placeholder="  email" id="email">
 52             <br/>
 53             
 54             <span class="xueyuan">所在學院:</span>
 55             <input type="text" name="xueyuan" placeholder="  xueyuan" id="xueyuan">
 56             <br/>
 57             
 58             <span class="xi">所在系:</span>
 59             <input type="text" name="xi" placeholder="  suozaixi" id="xi">
 60             <br/>
 61             
 62             <span class="classes">所在班級:</span>
 63             <input type="text" name="classes" placeholder="  classes" id="classes">
 64             <br/>
 65             
 66             
 67             <span class="year">入學年份(屆):</span>
 68             <select name="year">
 69             <option value ="2014">2014</option>
 70                   <option value ="2015">2015</option>
 71                   <option value ="2016">2016</option>
 72                   <option value="2017">2017</option>
 73                   <option value="2018">2018</option>
 74                   <option value ="2019">2019</option>
 75                   </select>
 76             <br/>
 77             
 78             <span class="shengyuandi">生源地:</span>
 79             <input type="text" name="shengyuandi" placeholder="  shengyuandi" id="shengyuandi">
 80             <br/>
 81             
 82             <span class="beizhu">備註:</span>
 83             <input type="text" name="beizhu" placeholder="  beizhu" id="beizhu">
 84             <br/>
 85             
 86             <input type="submit" value="保存" class="butn">
 87 <!--             <input type="reset" value="重置" class="butn"> -->
 88             <br/>
 89         </form>
 90     </section>
 91 </div>
 92 
 93 <script>
 94     //正則表達式
 95 function check(form){  96 
 97     if(form.username.value.length<6 || form.username.value.length>12 || new RegExp("[^0-9a-zA-Z]").test(form.username.value)){  98         alert("登錄帳號必須由6-12位英文字符或數字組成!");  99  form.user.focus(); 100         return false; 101  } 102     if(new RegExp("[^a-zA-Z]").test(form.username.value.substring(0,1))){ 103         alert("登錄帳號必須以英文字母開頭!"); 104  form.user.focus(); 105         return false; 106  } 107     if(form.password.value.length <= 8 || new RegExp("[^0-9a-zA-Z]").test(form.password.value)){ 108         alert("密碼必須由八位以上字母、數字組成!"); 109  form.phone.focus(); 110         return false; 111  } 112     if(form.studentID.value.length != 8 || new RegExp("[^0-9]").test(form.studentID.value)){ 113         alert("請輸入八位學號!"); 114  form.phone.focus(); 115         return false; 116  } 117 
118     if(form.phone.value.length != 11 || new RegExp("[^0-9]").test(form.phone.value)){ 119         alert("手機號請輸入11位數字!"); 120  form.phone.focus(); 121         return false; 122  } 123 
124     //以數字字母開頭,中間能夠是多個數字字母或下劃線;而後是「@」;而後是數字字母;而後是「.」;最後是2-4個字母結尾
125     var regex = /^([a-zA-Z]|[0-9])(\w|\-)+@[a-zA-Z0-9]+\.([a-zA-Z]{2,4})$/; 126     if(!regex.test(form.email.value)){ 127         alert("郵箱格式錯誤!"); 128  form.email.focus(); 129         return false; 130  } 131     return true; 132 } 133 </script>
134 
135 </body>
136 </html>

數據庫存儲界面:sql

 

對於Java web數據庫鏈接的理解:數據庫

Java類:

1.servlet 類 處理從前端接受的信息,即jsp 與數據庫鏈接的接口

 2.DBUtil 類  加載數據庫驅動, 創建對數據庫的鏈接。

3.Dao 類  實現對數據庫的增刪改查

4.Bean 類 存儲信息的基礎類

5.在lib目錄下導入須要的jar的包

 

 6. jsp 實現界面。

 

本人目前處於初學階段,代碼大部分都是對模板進行修改,沒有徹底理解,仍舊有許多須要學習的地方。

 數據庫的JDBC橋接:

面向對象的應用程序接口,制定了統一的訪問各類關係型數據庫的標準接口。

JDBC任務:

1.與數據庫創建鏈接

2.向數據庫發生sql語句

3.處理從數據庫返回的結果

如下是以前的錯誤代碼:

https://www.cnblogs.com/cxy0210/p/11715924.html

 

 

以上代碼若有錯誤,敬請指正,感激涕零。

相關文章
相關標籤/搜索