课堂测试用javaweb写一个注册界面,并将数据保存到后台数据库(部分完成)

今天我到现在为止,也只完成了数据库的连接,还没有写前台的javascript的检查输入的代码,打算周四前完成。

代码如下:

 1 package Dao;
 2 
 3 import java.sql.Connection;
 4 import java.sql.Statement;
 5 
 6 import DBUtil.DBUtil;
 7 
 8 import Entity.User;
 9 
10 public class Dao {
11 
12     public boolean add(User user) {
13         String sql = "insert into uses1 (username,password,sex,name,stuname,email,xueyuan,xi,banji,year,address,addtext) values('"+ user.getUsername() + "','"+ user.getPassword() +"','"+ user.getSex() +"','"+user.getName() +"','"+ user.getStuname() +"','"+user.getEmail()+"','"+ user.getXueyuan() +"','"+user.getXi()+"','"+user.getBanji()+"','"+user.getYear()+"','"+user.getAddress()+"','"+user.getAddtext()+"')";
14         Connection conn = DBUtil.getConn();
15         Statement state = null;
16         boolean f = false;
17         int a = 0;
18 
19         try {
20             state = conn.createStatement();
21             a=state.executeUpdate(sql);
22         } catch (Exception e) {
23             e.printStackTrace();
24         } finally {
25             
26             DBUtil.close(state, conn);
27         }
28 
29         if (a > 0) {
30             f = true;
31         }
32         return f;
33 
34 }
35 
36 
37 }

 

 

 

 1 package DBUtil;
 2 
 3 
 4 
 5 import java.sql.Connection;
 6 import java.sql.DriverManager;
 7 import java.sql.PreparedStatement;
 8 import java.sql.ResultSet;
 9 import java.sql.SQLException;
10 import java.sql.Statement;
11 
12 /**
13  * @author Hu
14  *
15  */
16 public class DBUtil {
17     
18     public static String db_url = "jdbc:mysql://localhost:3306/user?serverTimezone=UTC";
19     public static String db_user = "root";
20     public static String db_pass = "123";
21     
22     public static Connection getConn () {
23         Connection conn = null;
24         
25         try {
26             Class.forName("com.mysql.cj.jdbc.Driver");
27             conn = DriverManager.getConnection(db_url, db_user, db_pass);
28         } catch (Exception e) {
29             e.printStackTrace();
30         }
31         
32         return conn;
33     }
34     
35     public static void close (Statement state, Connection conn) {
36         if (state != null) {
37             try {
38                 state.close();
39             } catch (SQLException e) {
40                 e.printStackTrace();
41             }
42         }
43         
44         if (conn != null) {
45             try {
46                 conn.close();
47             } catch (SQLException e) {
48                 e.printStackTrace();
49             }
50         }
51     }
52     
53     public static void close (ResultSet rs, Statement state, Connection conn) {
54         if (rs != null) {
55             try {
56                 rs.close();
57             } catch (SQLException e) {
58                 e.printStackTrace();
59             }
60         }
61         
62         if (state != null) {
63             try {
64                 state.close();
65             } catch (SQLException e) {
66                 e.printStackTrace();
67             }
68         }
69         
70         if (conn != null) {
71             try {
72                 conn.close();
73             } catch (SQLException e) {
74                 e.printStackTrace();
75             }
76         }
77     }
78 
79     public static void main(String[] args) throws SQLException {
80         Connection conn = getConn();
81         PreparedStatement pstmt = null;
82         ResultSet rs = null;
83         String sql ="select * from users";
84         pstmt = conn.prepareStatement(sql);
85         rs = pstmt.executeQuery();
86         if(rs.next()){
87             System.out.println("空");
88         }else{
89             System.out.println("不空");
90         }
91     }
92 }

 

 

 

  1 package Entity;
  2 
  3 public class User {
  4     
  5     private String username;
  6     private String password;
  7     private String sex;
  8     private String name;
  9     private String stuname;
 10     private String email;
 11     private String xueyuan;
 12     private String xi;
 13     private String banji;
 14     private String year;
 15     private String address;
 16     private String addtext;
 17     
 18     public String getUsername() {
 19         return username;
 20     }
 21 
 22     public void setUsername(String username) {
 23         this.username = username;
 24     }
 25 
 26     public String getPassword() {
 27         return password;
 28     }
 29 
 30     public void setPassword(String password) {
 31         this.password = password;
 32     }
 33 
 34     public String getSex() {
 35         return sex;
 36     }
 37 
 38     public void setSex(String sex) {
 39         this.sex = sex;
 40     }
 41 
 42     public String getName() {
 43         return name;
 44     }
 45 
 46     public void setName(String name) {
 47         this.name = name;
 48     }
 49 
 50     public String getStuname() {
 51         return stuname;
 52     }
 53 
 54     public void setStuname(String stuname) {
 55         this.stuname = stuname;
 56     }
 57 
 58     public String getEmail() {
 59         return email;
 60     }
 61 
 62     public void setEmail(String email) {
 63         this.email = email;
 64     }
 65 
 66     public String getXueyuan() {
 67         return xueyuan;
 68     }
 69 
 70     public void setXueyuan(String xueyuan) {
 71         this.xueyuan = xueyuan;
 72     }
 73 
 74     public String getXi() {
 75         return xi;
 76     }
 77 
 78     public void setXi(String xi) {
 79         this.xi = xi;
 80     }
 81 
 82     public String getBanji() {
 83         return banji;
 84     }
 85 
 86     public void setBanji(String banji) {
 87         this.banji = banji;
 88     }
 89 
 90     public String getYear() {
 91         return year;
 92     }
 93 
 94     public void setYear(String year) {
 95         this.year = year;
 96     }
 97 
 98     public String getAddress() {
 99         return address;
100     }
101 
102     public void setAddress(String address) {
103         this.address = address;
104     }
105 
106     public String getAddtext() {
107         return addtext;
108     }
109 
110     public void setAddtext(String addtext) {
111         this.addtext = addtext;
112     }
113 
114     public User() {}
115     
116 public User(String username,String password,String sex,String name,String stuname,String email,String xueyuan, String xi,String banji,String year,String address,String addtext) {
117         
118         
119         this.username=username;
120         this.password=password;
121         this.sex=sex;
122         this.name=name;
123         this.stuname=stuname;
124         this.email=email;
125         this.xueyuan=xueyuan;
126         this.xi=xi;
127         this.banji=banji;
128         this.year=year;
129         this.address=address;
130         this.addtext=addtext;
131         
132     }
133     
134 
135 }

 

 

 

 

 1 package Servlet;
 2 
 3 import java.io.IOException;
 4 
 5 import javax.servlet.ServletException;
 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 Dao.Dao;
12 
13 import Entity.User;
14 
15 
16 
17 
18 @WebServlet("/Servlet")
19 public class Servlet extends HttpServlet {
20     private static final long serialVersionUID = 1L;
21        
22     
23     public Servlet() {
24         super();
25         
26     }
27     protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
28         req.setCharacterEncoding("utf-8");
29         String method = req.getParameter("method");
30         if ("add".equals(method)) {
31             add(req, resp);
32         } 
33     }
34     
35     
36     public void add(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{
37         req.setCharacterEncoding("utf-8");
38         String username = req.getParameter("username");
39         String password = req.getParameter("password");
40         String sex = req.getParameter("sex");
41         String name = req.getParameter("name");
42         String stuname = req.getParameter("stuname");
43         String email = req.getParameter("email");
44         String xueyuan = req.getParameter("xueyuan");
45         String xi= req.getParameter("xi");
46         String banji= req.getParameter("banji");
47         String year= req.getParameter("year");
48         String address= req.getParameter("address");
49         String addtext= req.getParameter("addtext");
50         User user = new User(username,password,sex,name,stuname,email,xueyuan,xi,banji,year,address,addtext);
51         
52         Dao dao =new Dao();
53         boolean f=dao.add(user);
54         
55         if(f) {
56             req.setAttribute("message", "注册成功!");
57             req.getRequestDispatcher("users.jsp").forward(req,resp);
58         } 
59         else {
60             req.setAttribute("message", "注册失败!");
61             req.getRequestDispatcher("users.jsp").forward(req,resp);
62         }
63     }
64 }

 

 

 

 

  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>Insert title here</title>
  8 <script>
  9 
 10 </script>
 11 </head>
 12     
 13     <body>
 14     <%
 15         Object message = request.getAttribute("message");
 16         if (message != null && !"".equals(message)) {
 17     %>
 18     <script type="text/javascript">
 19               alert("<%=request.getAttribute("message")%>");
 20     </script>
 21     <%
 22         }
 23     %>
 24     
 25     
 26         <table border="0px" cellpadding="10px" cellspacing="0px"
 27                 style="width: 50%;margin:auto;background:rgb(195,195,195)"  bordercolor="red" >
 28                     <form action="Servlet?method=add" method="post"
 29             onsubmit="return check()">
 30             <caption>当前位置:添加学生信息</caption>
 31           
 32              
 33                 <tr>
 34                     <td>登录账号:</td>
 35                     <td><input type="text" name="username"></td>
 36                 </tr>
 37                 <tr>
 38                     <td>登录密码:</td>
 39                     <td><input type="password" name="password"></td>
 40                 </tr>
 41                 <tr>
 42                     <td>性别:</td>
 43                     <td>
 44                     <select name="sex">
 45                             <option value="男">男</option>
 46                             <option value="女">女</option>
 47                       </select>
 48                     </td>
 49                 </tr>
 50                 <tr>
 51                     <td>姓名:</td>
 52                     <td><input type="text" name="name"></td>
 53                 </tr>
 54                 <tr>
 55                      <td>学号:</td>
 56                     <td><input type="text" name="stuname"></td>
 57                 </tr>
 58                   <tr>
 59                     <td>电子邮箱:</td>
 60                     <td><input type="text" name="email"></td>
 61                 </tr>
 62                 <tr>
 63                     <td>所在学院:</td>
 64                     <td><input type="text" name="xueyuan"></td>
 65                 </tr>
 66                  <tr>
 67                     <td>所在系:</td>
 68                     <td><input type="text" name="xi"></td>
 69                 </tr>
 70                 <tr>
 71                     <td>所在班级:</td>
 72                     <td><input type="text" name="banji"></td>
 73                 </tr>
 74                 <tr>
 75                     <td>入学年份(届):</td>
 76                     <td>
 77                         <select name="year">
 78                             <option value="1998">1998</option>
 79                             <option value="1999">1999</option>
 80                             <option value="2000">2000</option>
 81                             <option value="2001">2001</option>
 82                             <option value="2002">2002</option>
 83                             <option value="2003">2003</option>
 84                             <option value="2004">2004</option>
 85                             <option value="2005">2005</option>
 86                             <option value="2006">2006</option>
 87                             <option value="2007">2007</option>
 88                             <option value="2008">2008</option>
 89                             <option value="2009">2009</option>
 90                             <option value="2010">2010</option>
 91                             <option value="2011">2011</option>
 92                             <option value="2012">2012</option>
 93                             <option value="2013">2013</option>
 94                             <option value="2014">2014</option>
 95                             <option value="2015">2015</option>
 96                             <option value="2016">2016</option>
 97                             <option value="2017">2017</option>
 98                             <option value="2018">2018</option>
 99                             <option value="2019">2019</option>
100                         </select>
101                     </td>
102                 </tr>
103                  <tr>
104                     <td>生源地:</td>
105                     <td><input type="text" name="address"></td>
106                 </tr>
107                  <tr>
108                     <td>
109                                     备注:
110                     </td>
111                     <td>
112                     <input type="text" name="addtext" >
113                     </td>
114                 </tr>
115                 <tr>
116                     <th colspan="2">
117                         <input type="submit" value="添加">
118                     </th>
119                 </tr>
120             </form>
121         </table>
122     </body>
123 </html>

 

 

效果截图

 

 

 

 

 

 

 

posted @ 2019-10-21 23:39 yangliuliu 阅读(...) 评论(...) 编辑 收藏