• 课堂测试(六)


    石家庄铁道大学2019年秋季

      2018 课堂测试试卷(六)10分)

    课程名称: JAVA语言程序设计  任课教师 王建民        考试时间: 150 分钟  

    一、 考试要求:

     

    1登录账号:要求由612位字母、数字、下划线组成,只有字母可以开头;(1分)

    2登录密码:要求显示“• ”或“*”表示输入位数,密码要求八位以上字母、数字组成。(1分)

    3性别:要求用单选框或下拉框实现,选项只有“男”或“女”;(1分)

    4学号:要求八位数字组成,前四位为2018”开头,输入自己学号;(1分)

    5姓名:输入自己的姓名;

    5电子邮箱:要求判断正确格式xxxx@xxxx.xxxx;(1分)

    6点击“添加”按钮,将学生个人信息存储到数据库中。(3分)

    7可以演示连接上数据库。(2分)

    由于是第一次自己进行javaweb,数据库连接操作,用了一下午勉强完成基本要求。

    以下是我的程序源代码:

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Insert title here</title>
    <script type="text/javascript">
    function validate()
    {
    	var Id=document.forms["myForm"]["xuehao"].value;
    
    	if(Id<20180000|Id>20189999)
    		{
    		alert("学号必须以2018开头");
    		return false;
    		}
    }
    </script>
    <body>
    <form action="test_deal.jsp" name="myForm" onsubmit="return validate()">
    <table  border="1" align="center">
    
    <tr>
    <td>登录账号</td>
    <td><input type="text" name="zhanghao" ></td>
    </tr>
    
    <tr>
    <td>登录密码</td>
    <td><input type="password" name="pass"></td>
    </tr>
    
    <tr>
    <td>性别</td>
    <td>
    <select name="x1">
    <option value="男">男</option>
    <option value="女">女</option>
    </select>
    </td>
    </tr>
    
    <tr>
    <td>姓名</td>
    <td><input type="text" name="x2"></td>
    </tr>
    
    <tr>
    <td>学号 </td>
    <td><input type="text" name="xuehao"> </td>
    </tr>
    
    <tr>
    <td>电子邮箱</td>
    <td><input type="text" name="x3"></td>
    </tr>
    
    <tr>
    <td>所在学校 </td>
    <td><input type="text" name="xuexiao"> </td>
    </tr>
    
    <tr>
    <td> 所在系</td>
    <td> <input type="text" name="xi"></td>
    </tr>
    
    <tr>
    <td> 所在班级</td>
    <td><input type="text" name="banji"> </td>
    </tr>
    
    <tr>
    <td>入学年份(届) </td>
    <td> <input type="text" name="nian">届</td>
    </tr>
    
    <tr>
    <td>生源地 </td>
    <td> <input type="text" name="shengyuandi"></td>
    </tr>
    <tr>
    <td>备注 </td>
    <td> <input type="text" style="height:100px" name="beizhu"></td>
    </tr>
    
    <tr>
    <td> <input type="submit"  value="添加"></td>
    </tr>
    
    </table>
    </form>
    </body>
    </html>
    

      这个是我写的web界面,使用了几种标签,<td> <tr> <input>....这几种标签属性在百度中都可以查到

    <%@page import="Dao.Dao"%>
    <%@page import="java.util.regex.Pattern"%>
    <%@page import="java.util.regex.Matcher"%>
    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Insert title here</title>
    
    </head>
    </head>
    <body>
    <%
    	request.setCharacterEncoding("utf-8");
    	String username=request.getParameter("zhanghao");
    	String password=request.getParameter("pass");
    	String x1=request.getParameter("x1");
    	String x2=request.getParameter("x2");
    	String xuehao=request.getParameter("xuehao");
    	String x3=request.getParameter("x3");
    	String xuexiao=request.getParameter("xuexiao");
    	String xi=request.getParameter("xi");
    	String banji=request.getParameter("banji");
    	String nian=request.getParameter("nian");
    	String shengyuandi=request.getParameter("shengyuandi");
    	String beizhu=request.getParameter("beizhu");
    	String user="^[a-zA-Z][a-zA-Z0-9_]{5,12}$";
    	Pattern puser = Pattern.compile(user);
        Matcher muser = puser.matcher(username);
        boolean isMatch3 = muser.matches();
    	String pass="^[a-zA-Z][a-zA-Z0-9_]{7,20}$";
    	Pattern ppass = Pattern.compile(pass);
        Matcher mpass = ppass.matcher(password);
        boolean isMatch2 = mpass.matches();
    	String mail = "^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$";
    	Pattern pmail = Pattern.compile(mail);
        Matcher mmail = pmail.matcher(x3);
        boolean isMatch1 = mmail.matches();
        if(isMatch3){
        	if(isMatch2){
       		 if(isMatch1){
       			 if(Dao.add(username, password, x1, x2, xuehao, x3, xuexiao,xi,banji,nian,shengyuandi,beizhu)==1)
       			 {
       				 {
       						out.print("<script language = 'javascript'>alert('添加成功');</script>");
       						response.setHeader("refresh", "0;url=test.jsp");
       					    }
       			 }
       			 else{
       				 out.print("<script language = 'javascript'>alert('添加失败');</script>");
       				 response.setHeader("refresh", "0;url=test.jsp");
       			 }
       		 }else{
       			out.print("<script language = 'javascript'>alert('您的邮箱" +x3 + "是错误格式!!!');</script>");
    		        out.print("<script>window.history.go(-1);  </script>");
       			 
       		 }
       		 
       	    } else {
       	    	out.print("<script language = 'javascript'>alert('您的密码" +password + "是错误格式!!!应为八位以上字母数字组成!');</script>");
    		        out.print("<script>window.history.go(-1);  </script>");
       	    }
        }else{
        	out.print("<script language = 'javascript'>alert('您的用户名" +username + "是错误格式!!!应为6-12位以上字母数字下划线组成!字母开头');</script>");
            out.print("<script>window.history.go(-1);  </script>");
        }
    	
     %>
    </body>
    </html>
    

      这一篇是我为了完成要求写的jsp语句,其中使用了验证的正则表达式

    package Dao;
    
    import java.sql.Connection;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.util.Scanner;
    
    
    import util.util;
    
    public class Dao {
    	static Connection conn;
        static PreparedStatement ps = null;
        static ResultSet rs;
        static String sql = "select * from WeiHu";
        static util ut= new util();
    	static Scanner in = new Scanner(System.in);
    	static int num1=1;
    	
    	//平台维护信息录入
    	public static int add(String w1,String w2,String w3,String w4,String w5,String w6,String w7,String w8,String w9,String w10,String w11,String w12) throws SQLException
    	{
    		
    		 int b=0;
    		 conn= ut.getConn();
    		 String sql="insert into register values(?,?,?,?,?,?,?,?,?,?,?,?)";
    		 try {
    			 ps=conn.prepareStatement(sql);
    			 ps.setString(1,w1);
    			 ps.setString(2,w2);
    			 ps.setString(3,w3);
    			 ps.setString(4,w4);
    			 ps.setString(5,w5);
    			 ps.setString(6,w6);
    			 ps.setString(7,w7);
    			 ps.setString(8,w8);
    			 ps.setString(9,w9);
    			 ps.setString(10,w10);
    			 ps.setString(11,w11);
    			 ps.setString(12,w12);
    			 int a=ps.executeUpdate();
    			 if(a>0) {
    				 b++;
    				 System.out.println("添加成功");
    			 }
    			 else {
    				 System.out.println("添加失败");
    			 }
    		 }catch(Exception e) {
    			 e.printStackTrace();
    		 }
    		 try {
    			 if(ps!=null)ps.close();
    			 if(conn!=null)conn.close();
    		 }catch(Exception e2) {
    			 e2.printStackTrace();
    		 }
    			
    		 
    		 return b;
    	}	
    }
    

      这一篇定义了一个java的Dao类,是把数据导入到数据库中的方法

    package util;
    
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    
    public class util {
    	String user="sa";
    	String password="123456";
    	String url="jdbc:sqlserver://localhost:1433;DatabaseName=software";
    	public  Connection getConn(){
    		Connection conn=null;
    		try {
    			Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
    		} catch (ClassNotFoundException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    		try {
    				conn=DriverManager.getConnection(url, user, password);
    		} catch (Exception e) {
    				// TODO Auto-generated catch block
    				e.printStackTrace();
    		}
    		return conn;
    	}
    	public 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();
    			}
    		}
    	}
    }
    

      这一篇是数据库的 连接数据库。

    以下是我的操作过程:

    这是数据库中存入的信息

     收获:通过这次和大三学长一对一的学习,我的收获很大,其中学长帮我们完成了环境的搭配,以及一些入门级别的操作,学到了很多很多。如:数据库的画表,连接库,增加信息,以及web,jsp 正则表达式等等,总之,这两天收获很大。

  • 相关阅读:
    校验字符的表达式
    校验数字的表达式
    Html和xhtml有什么区别
    VUE3.0 + TS 项目实战 (2)基本写法
    VUE3.0 + TS 项目实战 (1)初始化项目
    props传递函数以及$emit触发父组件方法
    rollup
    js函数式编程
    移动端双击事件
    JS节流与防抖
  • 原文地址:https://www.cnblogs.com/zhjvvvvvv/p/11716918.html
Copyright © 2020-2023  润新知