题目为   编写学生注册信息界面,并且连接到数据库

 1 package com.ccc.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 com.ccc.business.ZhuceService;
12 import com.ccc.po.Zhuce;
13 
14 public class ZhuceServlet extends HttpServlet {
15 
16     @Override
17     protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
18         // TODO Auto-generated method stub
19         request.setCharacterEncoding("utf-8");//设置编码
20         String param=request.getParameter("param");
21         
22         if("add".equals(param)) {
23             String user=request.getParameter("user");
24             String password=request.getParameter("password");
25             String name=request.getParameter("name");
26             String sex=request.getParameter("sex");
27             String diqu=request.getParameter("diqu");
28             String phone=request.getParameter("phone");
29             String youxiang=request.getParameter("youxiang");
30             Zhuce zhuce=new Zhuce(user,password,name,sex,diqu,phone,youxiang);
31             ZhuceService zs=new ZhuceService();
32             zs.saveZhuce(zhuce);
33             System.out.println("连接servlet");
34             //重定向到index.jsp
35             response.sendRedirect("index.jsp");
36             
37         }
38         if("delete".equals(param)) {
39             String user = request.getParameter("user");
40             
41             ZhuceService.deleteZhuce(user);
42 
43             // 重定向到index.jspt页面
44             response.sendRedirect("index.jsp");
45         }
46     }
47     
48 }
package com.ccc.po;

public class Zhuce {
    private String user;
    private String password;
    private String name;
    private String sex;
    private String diqu;
    private String phone;
    private String youxiang;
    public Zhuce(String user,String password,String name,String sex,String diqu,String phone,String youxiang) {
        super();
        this.user=user;
        this.password=password;
        this.name=name;
        this.sex=sex;
        this.diqu=diqu;
        this.phone=phone;
        this.youxiang=youxiang;
    }
    public String getUser() {
        return user;
    }
    public void setUser(String user) {
        this.user = user;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }
    public String getDiqu() {
        return diqu;
    }
    public void setDiqu(String diqu) {
        this.diqu = diqu;
    }
    public String getPhone() {
        return phone;
    }
    public void setPhone(String phone) {
        this.phone = phone;
    }
    public String getYouxiang() {
        return youxiang;
    }
    public void setYouxiang(String youxiang) {
        this.youxiang = youxiang;
    }
    
}
 1 package com.ccc.DButil;
 2 import java.sql.Connection;
 3 import java.sql.DriverManager;
 4 import java.sql.PreparedStatement;
 5 import java.sql.SQLException;
 6 import java.sql.Statement;
 7 
 8 //数据库资源管理工具
 9 public class DButil {
10 
11     private static Connection conn;
12     private static Statement stmt;
13     private static PreparedStatement pstmt;
14     
15     static {
16         //加载驱动
17         try {
18             Class.forName("com.mysql.jdbc.Driver");
19         } catch (ClassNotFoundException e) {
20             // TODO Auto-generated catch block
21             e.printStackTrace();
22         }
23     }
24     
25     //实例化数据库连接conn
26     public static Connection getConnection() {
27         try {
28             conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/zhucebiao?useUnicode=true&characterEncoding=UTF-8", "root", "root");
29         } catch (SQLException e) {
30             // TODO Auto-generated catch block
31             e.printStackTrace();
32         }
33         return conn;
34     }
35     
36     //实例化SQL执行句柄stmt
37     public static Statement getStatement() {
38         Connection conn = getConnection();
39         try {
40             if(conn != null) {
41                 stmt = conn.createStatement();
42             }
43             
44         } catch (SQLException e) {
45             // TODO Auto-generated catch block
46             e.printStackTrace();
47         }
48         return stmt;
49     }
50     
51     //实例化SQL执行句柄pstmt
52     public static PreparedStatement getPreparedStatement(String sql) {
53         Connection conn = getConnection();
54         try {
55             if(conn != null) {
56                 pstmt = conn.prepareStatement(sql);
57             }
58         }catch(SQLException e) {
59             e.printStackTrace();
60         }
61         return pstmt;
62     }
63     
64     //关闭数据库连接资源
65     public static void closeDBResources() {
66         try {
67             if(pstmt != null && !pstmt.isClosed()) {
68                 pstmt.close();
69             }
70             if(stmt != null && !stmt.isClosed()) {//如果stmt不为空,并且还未关闭
71                 stmt.close();
72             }
73             if(conn != null && !conn.isClosed()) {
74                 conn.close();
75             }
76         }catch(SQLException e) {
77             e.printStackTrace();
78         }
79         
80     }
81     
82 }
package com.ccc.dao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.ccc.DButil.DButil;

import com.ccc.po.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;
            }
        
}
package com.ccc.business;

import java.util.List;

import com.ccc.dao.ZhuceDao;
import com.ccc.po.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);
        }
}
  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 </head>
  9 <script type="text/javascript">
 10 function checkForm(){
 11     //根据input 的id找到四个控件  节点
 12     var userNode=document.getElementById("user");
 13     var passwordNode=document.getElementById("password");
 14     var nameNode=document.getElementById("name");
 15     var phoneNode=document.getElementById("phone");
 16     var youxiangNode=document.getElementById("youxiang");
 17     
 18     var regUser=/^[a-zA-Z]([a-zA-Z]|\d){5,11}$/;//纯数字1-10个 ^起始符  $末尾符
 19     var regPassword=/^([a-zA-Z]|\d){6}$/;
 20 
 21     var regPhone=/^\d{11}$/;
 22     var regYouxiang=/^\S+@\S+\.com$/;
 23     
 24     //判断user是否为空
 25     if(userNode.value.length==0){
 26         alert("用户名不能为空");
 27         return false;
 28     }
 29     if(!regUser.test(userNode.value)){
 30         alert("用户名必须以英文字母开头和数字组成的为6-12位字符");
 31         return false;
 32     }
 33     //判断password是否为空
 34     if(passwordNode.value.length==0){//将空格替换成长度为0
 35         alert("密码不能为空!");
 36         return false;
 37     }
 38     if(!regPassword.test(passwordNode.value)){//将空格替换成长度为0
 39         alert("密码为6位英文和数字组成");
 40         return false;
 41     }
 42     //判断name是否为空
 43     if(nameNode.value.length==0){
 44         alert("姓名不能为空");
 45         return false;
 46     }
 47 
 48 
 49     //判断phone是否为空
 50     if(phoneNode.value.length==0){
 51         alert("手机号不能为空");
 52         return false;
 53     }
 54     if(!regPhone.test(phoneNode.value)){
 55         alert("手机号必须为11位数字");
 56         return false;
 57     }
 58     //判断youxiang是否为空
 59     if(youxiangNode.value.length==0){
 60         alert("邮箱不能为空");
 61         return false;
 62     }
 63     if(!regYouxiang.test(youxiangNode.value)){
 64         alert("邮箱必须为邮箱格式");
 65         return false;
 66     }
 67     return true;
 68 }        
 69 </script>
 70 <body>
 71     <h2>软件工程专业交流注册表</h2>
 72     <hr>                       
 73     <form action="ZhuceServlet?param=add"   method="post" onsubmit= "return checkForm();">
 74         <table width="400" border="0" align="center">
 75             <tr>
 76                 <td>用户名:</td>
 77                 <td><input type="text" id="user" name="user"></td>
 78             </tr>
 79             <tr>
 80                 <td>密码:</td>
 81                 <td><input type="password" id="password" name="password" ></td>
 82             </tr>
 83             <tr>
 84                 <td>姓名:</td>   <!--输入只能为数字   正则表达式 -->
 85                 <td><input type="text" id="name" name="name" ></td>
 86             </tr>
 87             <tr>
 88                 <td>sex:</td>
 89                 <td><input type="checkbox" id="sex" name="sex" value="Bike"> 90                     <input type="checkbox" id="sex" name="sex" value="Car">女 </td>
 91             </tr>
 92             <tr>
 93                 <td>地区:</td>
 94                 <td><select id="diqu" name="diqu">
 95                         <option value="Volvo">Volvo</option>
 96                         <option value="Saab">Saab</option>
 97                         <option value="Fiat" selected>Fiat</option>
 98                         <option value="Audi">Audi</option>
 99                 </select></td>
100             </tr>
101             <tr>
102                 <td>手机号:</td>
103                 <td><input type="text" id="phone" name="phone"></td>
104             </tr>
105             <tr>
106                 <td>邮箱:</td>
107                 <td><input type="text" id="youxiang" name="youxiang"></td>
108             </tr>
109             <tr>
110                 <td colspan="2">
111                     <input type="submit" value="提交">
112                     <input type="reset" value="重置">
113                 </td>
114             </tr>
115         </table>
116     </form>
117 </body>
118 </html>
posted on 2019-10-21 22:30  迎新  阅读(172)  评论(0编辑  收藏  举报