• Java课堂测试


    //Dao.java
    package Dao;
    
    import java.sql.Connection;
    import java.sql.Statement;
    
    import DBUtil.DBUtil;
    
    import Entity.User;
    
    public class Dao {
    
        public boolean add(User user) {
            // TODO Auto-generated method stub
            String sql = "insert into user(username,password,name,sex,address,tel,email,stnumber,school,classes,xi,time,beizhu) values('"+ user.getUsername() + "','"+ user.getPassword() +"','"+ user.getName() +"','" + user.getSex() +"','"+ user.getAddress() +"','"+ user.getTel() +"','"+user.getEmail()+"','" + user.getStnumber() +"','" + user.getSchool() +"','" + user.getClasses() +"','" + user.getXi() +"','" + user.getTime() +"','" + user.getBeizhu() +"')";
            Connection conn = DBUtil.getConn();
            Statement state = null;
            boolean f = false;
            int a = 0;
    
            try {
                state = conn.createStatement();
                a=state.executeUpdate(sql);
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                
                DBUtil.close(state, conn);
            }
    
            if (a > 0) {
                f = true;
            }
            return f;
    
    }
    }
    
    //DBUtil.java
    package DBUtil;
    
    
    
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    
    /**
     * ���ݿ����ӹ���
     * @author W
     *
     */
    public class DBUtil {
        
        public static String db_url = "jdbc:mysql://localhost:3306/user";
        public static String db_user = "root";
        public static String db_pass = "0000";
        
        public static Connection getConn () {
            Connection conn = null;
            
            try {
                Class.forName("com.mysql.jdbc.Driver");
                conn = DriverManager.getConnection(db_url, db_user, db_pass);
            } catch (Exception e) {
                e.printStackTrace();
            }
            
            return conn;
        }
        
        public static void close (Statement state, Connection conn) {
            if (state != null) {
                try {
                    state.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        
        public static void close (ResultSet rs, Statement state, Connection conn) {
            if (rs != null) {
                try {
                    rs.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            
            if (state != null) {
                try {
                    state.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    
        public static void main(String[] args) throws SQLException {
            Connection conn = getConn();
            PreparedStatement pstmt = null;
            ResultSet rs = null;
            String sql ="select * from user";//select
            pstmt = conn.prepareStatement(sql);
            rs = pstmt.executeQuery();
            if(rs.next()){
                System.out.println("空");
            }else{
                System.out.println("不空");
            }
        }
    }
    
    //User.java
    package Entity;
    
    public class User {
        
        private String username;
        private String password;
        private String name;
        private String sex;
        private String address;
        private String tel;
        private String email;
        private String stnumber;
        private String school;
        private String classes;
        private String xi;
        private String time;
        private String beizhu;
        
        public String getUsername() {
            return username;
        }
        public void setUsername(String username) {
            this.username = username;
        }
        
        public String getPassword() {
            return password;
        }
        public void setPassssword(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 getAddress() {
            return address;
        }
        public void setAddress(String address) {
            this.address = address;
        }
        public String getTel() {
            return tel;
        }
        public void setTel(String tel) {
            this.tel = tel;
        }
        
        public String getEmail() {
            return email;
        }
        public void setEmail(String email) {
            this.email = email;
        }
        public String getStnumber() {
            return stnumber;
        }
        public void setStnumber(String stnumber) {
            this.stnumber=stnumber;
        }public String getSchool() {
            return school;
        }
        public void setSchool(String school) {
            this.school=school;
        }
        public String getClasses() {
            return classes;
        }
        public void setClasses(String classes) {
            this.classes=classes;
        }
        public String getXi() {
            return xi;
        }
        public void setXi(String xi) {
            this.xi=xi;
        }
        public String getTime() {
            return time;
        }
        public void setTime(String time) {
            this.time=time;
        }public String getBeizhu() {
            return beizhu;
        }
        public void setBeizhu(String beizhu) {
            this.beizhu=beizhu;
        }
        public User() {}
        
    public User(String username,String password,String name,String sex,String address,String tel,String email,String stnumber,String school,String classes,String xi,String time,String beizhu) {
            
            
            this.username=username;
            this.password=password;
            this.name=name;
            this.sex=sex;
            this.address=address;
            this.tel=tel;
            this.email=email;
            this.stnumber=stnumber;
            this.school=school;
            this.classes=classes;
            this.xi=xi;
            this.time=time;
            this.beizhu=beizhu;
        }
        
    
    }
    
    //Servlet.java
    package Servlet;
    
    import java.io.IOException;
    
    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 Dao.Dao;
    
    import Entity.User;
    
    
    
    
    @WebServlet("/Servlet")
    public class Servlet extends HttpServlet {
        private static final long serialVersionUID = 1L;
           
        
        public Servlet() {
            super();
            
        }
        protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            req.setCharacterEncoding("utf-8");
            String method = req.getParameter("method");
            if ("add".equals(method)) {
                add(req, resp);
            } 
        }
        
        
        private void add(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{
            req.setCharacterEncoding("utf-8");
            String username = req.getParameter("username");
            String password = req.getParameter("password");
            String name = req.getParameter("name");
            String sex = req.getParameter("sex");
            String address = req.getParameter("address");
            String tel = req.getParameter("tel");
            String email = req.getParameter("email");
            String stnumber = req.getParameter("stnumber");
            String school = req.getParameter("school");
            String classes = req.getParameter("classes");
            String xi = req.getParameter("xi");
            String time = req.getParameter("time");
            String beizhu = req.getParameter("beihzhu");
            User user = new User(username,password,name,sex,address,tel,email,stnumber,school,classes,xi,time,beizhu);
            
            Dao dao =new Dao();
            boolean f=dao.add(user);
            
            
            if(f) {
                req.setAttribute("message", "注册成功!");
                req.getRequestDispatcher("user.jsp").forward(req,resp);
            } else {
                req.setAttribute("message", "注册失败!");
                req.getRequestDispatcher("user.jsp").forward(req,resp);
            }
        }
    }
    
    //user.jsp
    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>Insert title here</title>
    
    </head>
        
        <body>
        <%
            Object message = request.getAttribute("message");
            if (message != null && !"".equals(message)) {
        %>
        <script type="text/javascript">
                  alert("<%=request.getAttribute("message")%>");
        </script>
        <%
            }
        %>
        
        
            <table border="1px" cellpadding="15px" cellspacing="0px"
                    style=" 30%;margin:auto;background:rgb(195,195,195)"  bordercolor="red" >
                        <form action="Servlet?method=add" method="post"onsubmit="return check(this)">
                <caption>注册用户</caption>
              
                 
                    <tr>
                        <th>用户名:</th>
                        <td><input type="text" name="username"></th>
                    </tr>
                    <tr>
                        <th>密码:</th>
                        <td><input type="password" name="password"></td>
                    </tr>
                   
                    <tr>
                        <th>姓名:</th>
                        <td><input type="text" name="name"></td>
                    </tr>
                    <tr>
                        <th>性别:</th>
                        <td>
                            <input type="radio" name="sex" checked="checked" value="男">男 &nbsp;&nbsp;
                            <input type="radio" name="sex" value="女"></td>
                    </tr>
                    <tr>
                        <th>生源地:</th>
                        <td><input type="text" name="address"></td>
                    </tr>
                     <tr>
                        <th>手机号:</th>
                        <td><input type="text" name="tel"></td>
                    </tr>
                 <tr>
                        <th>邮箱:</th>
                        <td><input type="text" name="email"></td>
                    </tr>
                    <tr>
                        <th>学号:</th>
                        <td><input type="text" name="stnumber"></td>
                    </tr>
                    <tr>
                        <th>学校:</th>
                        <td><input type="text" name="school"></td>
                    </tr>
                    <tr>
                        <th>班级:</th>
                        <td><input type="text" name="classes"></td>
                    </tr>
                    <tr>
                        <th>系:</th>
                        <td><input type="text" name="xi"></td>
                    </tr>
                    <tr>
                        <th>入学年份:</th>
                        <td>
                            <select name="time">
                                <option value="2010">2010</option>
                                <option value="2011">2011</option>
                                <option value="2012">2012</option>
                                <option value="2013">2013</option>
                                <option value="2014">2014</option>
                                <option value="2015">2015</option>
                                <option value="2016">2016</option>
                                <option value="2017">2017</option>
                                <option value="2018">2018</option>
                                <option value="2019">2019</option>
                                <option value="2020">2020</option>
                                <option value="2021">2021</option>
                            </select>
                        </td>
                    </tr>
                    <tr>
                        <th>备注:</th>
                        <td><input type="text" name="beizhu"></td>
                    </tr>
                    <tr>
                        <th colspan="2">
                            <input type="submit" value="提交">&nbsp;&nbsp;&nbsp;&nbsp;
                            <input type="reset" value="重置">
                        </th>
                    </tr>
                </form>
            </table>
        <script type="text/javascript">
    function check(form){
        if(form.username.value.length<6 || form.username.value.length>12 || new RegExp("[^a-zA-Z0-9_u4e00-u9fa5]").test(form.username.value)){
            alert("登陆账号必须由6-12位英文字符或数字或下划线组成!");
            form.username.focus();
            return false;
        }
        if(new RegExp("[^a-zA-Z]").test(form.username.value.substring(0,1))){
            alert("登陆账号必须以英文字母开头!");
            form.username.focus();
            return false;
        }
        if(form.password.value.length < 8 || new RegExp("[^0-9]").test(form.password.value)){
            alert("密码由八位以上数字或字母组成!");
            form.password.focus();
            return false;
        }
        
        //以数字字母开头,中间可以是多个数字字母或下划线;然后是“@”;然后是数字字母;然后是“.”;最后是2-4个字母结尾
        var regex = /^([a-zA-Z]|[0-9])(w|-)+@[a-zA-Z0-9]+.([a-zA-Z]{2,4})$/;
        if(!regex.test(form.email.value)){
            alert("邮箱格式错误!");
            form.email.focus();
            return false;
        }
        return true;
    }
    </script>
        </body>
    </html>
  • 相关阅读:
    python模块:XML
    python常用模块:json&pickle
    python模块:os,sys
    python常用模块:time与random
    文件查询修改功能实现
    Ubuntu--smb配置文件详解
    Ubuntu添加计划任务
    Docker私有仓库registry
    docker + calico网络,实现不同容器之间的相互访问
    Centos7 yum安装nginx
  • 原文地址:https://www.cnblogs.com/dd110343/p/11716489.html
Copyright © 2020-2023  润新知