• JAVA web课堂测试1


    1登录账号:要求由6到12位字母、数字、下划线组成,只有字母可以开头;(1分)
    2登录密码:要求显示“• ”或“*”表示输入位数,密码要求八位以上字母、数字组成。(1分)
    3性别:要求用单选框或下拉框实现,选项只有“男”或“女”;(1分)
    4学号:要求八位数字组成,前四位为“2018”开头,输入自己学号;(1分)
    5姓名:输入自己的姓名;
    5电子邮箱:要求判断正确格式xxxx@xxxx.xxxx;(1分)
    6点击“添加”按钮,将学生个人信息存储到数据库中。(3分)
    7可以演示连接上数据库。(2分)

    源代码:

    数据库的连接与关闭

    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    
    public class shujuku 
    {
        
        //数据库URL和账号密码
                public static final String connectionURL="jdbc:mysql://localhost:3306/student1?useUnicode=true&characterEncoding=GB18030&useSSL=false&serverTimezone=GMT&allowPublicKeyRetrieval=true";
                public static final String username="root";
                public static final String password="123456";
                
                //数据库连接
                public static Connection getConnection()
                {
                    try {
                        Class.forName("com.mysql.cj.jdbc.Driver");
                        //Class.forName("com.mysql.cj.jdbc.Driver");
                        return DriverManager.getConnection(connectionURL, username, password);
                    } catch (Exception e) {
                        // TODO: handle exception
                        System.out.println("数据库连接失败");
                        e.printStackTrace();
                    }
                    return null;
                }
                
                
                public static boolean getUsername(String username)
                {
                    Connection connection=null;
                    PreparedStatement preparedStatement=null;
                    ResultSet rSet=null;
                    try {
                        connection=getConnection();
                        String sql="select * from new_table where name="+"\'"+username+"\'";
                        System.out.println(sql);
                        preparedStatement=connection.prepareStatement(sql);
                        rSet=preparedStatement.executeQuery();
                        if(rSet.next())
                        {
                            System.out.println(rSet.getString(1));
                            return true;
                        }
                        else
                            return false;
                    }
                    catch(SQLException e)
                    {
                        e.printStackTrace();
                    }
                    return false;
                }
    
                public static boolean Signin(String ID,String mima,String sex,String name,String xuehao,String Email,String college,String major,String Class,String year,String place,String beizhu)//写表只改这个函数
                {
                    Connection con=null;
                    PreparedStatement pstmt=null;
                    try {
                        con=getConnection();
                        String sql="insert into student1 (ID,mima,sex,name,xuehao,Email,college,major,Class,year,place,beizhu) values (\'"+ID+"\',\'"+mima+"\',\'"+sex+"\',\'"+name+"\',\'"+xuehao+"\',\'"+Email+"\',\'"+college+"\',\'"+major+"\',\'"+Class+"\',\'"+year+"\',\'"+place+"\',\'"+beizhu+"\')";
                        System.out.println(sql);
                        pstmt=con.prepareStatement(sql);
                        pstmt.executeUpdate();
                        return true;
                    }
                    catch(SQLException e)
                    {
                        e.printStackTrace();
                    }
                    return false;
                }
    
                public static boolean addInformation(String username,String password,String sex,String name,String id,String email,String agency,String major,String classnum,String year,String birthplace,String text)
                {
                    
                    Connection con=null;
                    PreparedStatement pstmt=null;
                    try {
                        con=getConnection();
                        String sql="insert into student1 (ID,mima,sex,name,xuehao,Email,college,major,Class,year,place,beizhu) values (\'"+
                                username+"\',\'"+password+"\',\'"+sex+"\',\'"+name+"\',\'"+id+"\',\'"+email+"\',\'"+agency+"\',\'"+major+"\',\'"+classnum+"\',\'"+year+"\',\'"+birthplace+"\',\'"+text+"\')";
                        System.out.println(sql);
                        pstmt=con.prepareStatement(sql);
                        pstmt.executeUpdate();
                        return true;
                    }
                    catch(SQLException e)
                    {
                        e.printStackTrace();
                    }
                    return false;
                    
                }
    
    }

    Selvlet

    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 com.hjf.util.DBUtil;
    
    /**
     * Servlet implementation class shuju
     */
    @WebServlet("/shujuku")
    public class shuju extends HttpServlet {
        private static final long serialVersionUID = 1L;
           
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // TODO Auto-generated method stub
        
            
    request.setCharacterEncoding("UTF-8");
            
            String username=request.getParameter("ID");
            String password=request.getParameter("mima");
            String sex=request.getParameter("sex");
            String name=request.getParameter("name");
            String id=request.getParameter("xuehao");
            String email=request.getParameter("Email");
            String agency=request.getParameter("college");
            String major=request.getParameter("major");
            String classnum=request.getParameter("Class");
            String year=request.getParameter("year");
            String birthplace=request.getParameter("place");
            String text=request.getParameter("beizhu");
            
            
            if(shujuku.addInformation(username,password,sex,name,id,email,agency,major,classnum,year,birthplace,text))
                response.sendRedirect("add.jsp?result=true");
            else
                response.sendRedirect("add.jsp?result=false");
    
        }
    
        /**
         * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
         */
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // TODO Auto-generated method stub
            doGet(request, response);
        }
    
    }

    add.jsp

    <%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>注册</title>
    <style >

    .a{
    margin-top: 20px;
    }
    .b{
    font-size: 20px;
    160px;
    color: white;
    background-color: greenyellow;
    }
    </style>
    </head>

    <body onload="getResult()">

    <div align="center">
    <h1 style="color: red;">当前位置:添加学生信息</h1>

    <br>
    <form action="shujuku" method="post" >

    <div class="a">
    登录账号:<input type="text" id="ID" name="ID"/>

    </div>
    <div class="a">
    登录密码:<input type="password" id="mima" datatype="*8-16" nullmsg="请输入8-16位密码!" name="mima" />
    </div>
    <div class="a">
    性别:<select>
    <option>--请选择--</option>
    <option value="男">男</option>
    <option value="女">女</option>
    </select>
    </div>
    <div class="a">
    姓名:<input type="text" id="name" name="name" />
    </div>
    <div class="a">
    学号:<input type="text" id="xuehao" name="xuehao" />
    </div>

    <div class="a">
    电子邮箱:<input type="text" id="Email" name="Email" />
    </div>
    <div class="a">
    所在学院:<input type="text" id="college" name="college" />
    </div>
    <div class="a">
    所在系:<input type="text" id="major" name="major" />
    </div>
    <div class="a">
    所在班级:<input type="text" id="Class" name="Class" />
    </div>

    <div class="a">
    入学年份:<select>
    <option>--请选择--</option>
    <option value="1998">1998</option>
    <option value="1999">1999</option>
    <option value="2000">2000</option>
    <option value="2001">2001</option>
    <option value="2002">2002</option>
    <option value="2003">2003</option>
    <option value="2004">2005</option>
    <option value="2006">2007</option>
    <option value="2008">2008</option>
    </select>
    </div>
    <div class="a">
    生源地:<input type="text" id="place" name="place" />
    </div>

    <div class="a">
    备注:<input type="text" id="beizhu" name="beizhu" />
    </div>

    <div class="a">
    <input type="submit" value="添加" onclick="return check()">
    </div>


    </form>
    </div>

    <script type="text/javascript">

    function GetQueryString(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
    var r = window.location.search.substr(1).match(reg); //获取url中"?"符后的字符串并正则匹配
    var context = "";
    if (r != null)
    context = r[2];
    reg = null;
    r = null;
    return context == null || context == "" || context == "undefined" ? "" : context;
    }
    function getResult()
    {
    if(GetQueryString("result")=="true")
    alert("添加成功!");
    else if(GetQueryString("result")=="false")
    alert("添加失败!");
    }




    function check() {
    var ID = document.getElementById("ID");
    var mima = document.getElementById("mima");
    var xuehao = document.getElementById("xuehao");
    var Email = document.getElementById("Email");
    var name = document.getElementById("name");
    if (!ID.value.match(/^[a-zA-Z]\w{5,11}$/)) {
    alert("用户名由六到十二英文字符和数字组成,以英文字母开头");
    ID.focus();
    return false;
    }
    else if (!mima.value.match(/^[A-Za-z0-9]\w{7,20}$/)) {
    alert(" 密码由八 位 以上英文 和数 字 组成");
    mima.focus();
    return false;
    }
    else if(name.value!="徐姣美")
    {
    alert("姓名错误");
    name.focus;
    return false;
    }
    else if(xuehao.value<"20180000"|| xuehao.value>"20189999")
    {
    alert(" 学号由2018开头的八位组成");
    xuehao.focus();
    return false;
    }
    else if (!Email.value.match(/^[a-z0-9]+([._\\-]*[a-z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$/)) {
    alert(" 邮箱格式错误");
    Email.focus();
    return false;
    }
    }

    </script>


    </body>
    </html>

    测试截图:

     

      

      

  • 相关阅读:
    id4的数据库持久化写法
    docker 加速镜像的地址收集
    mongodb 的ID转换实体要注意的地方
    net core3.0 常用封装状态码总结
    JAVA8—————StringJoiner类
    BigDecimal加减乘除
    mysql 查询奇偶数
    Java遍历Map对象的方式
    Java中List, Integer[], int[]的相互转换
    springboot 读取resources下的文件然后下载
  • 原文地址:https://www.cnblogs.com/xjmm/p/11715813.html
Copyright © 2020-2023  润新知