java7连接数据库 网页 添加学生信息测试
石家庄铁道大学2019年秋季 ??2018?级课堂测试试卷(六)(10分) 课程名称:?JAVA语言程序设计??任课教师: 王建民 ???????考试时间:?150 分钟 ? 一、?考试要求: ? 1登录账号:要求由6到12位字母、数字、下划线组成,只有字母可以开头;(1分) 2登录密码:要求显示“? ”或“*”表示输入位数,密码要求八位以上字母、数字组成。(1分) 3性别:要求用单选框或下拉框实现,选项只有“男”或“女”;(1分) 4学号:要求八位数字组成,前四位为“2018”开头,输入自己学号;(1分) 5姓名:输入自己的姓名; 5电子邮箱:要求判断正确格式[email?protected];(1分) 6点击“添加”按钮,将学生个人信息存储到数据库中。(3分) 7可以演示连接上数据库。(2分) ?index.jsp: ? <%@?page?language="java"?contentType="text/html; charset=UTF-8" ????pageEncoding="UTF-8"%> <!DOCTYPE?html> <html> <head> <meta?charset="UTF-8"> <title>注册</title> ? <!-- CSS样式 --> <style?type="text/css"> tr{height:30px;} th{width:70px;text-align:left;} </style> ? <!-- 结束 --> </head> <body?> ? <h2?style="text-align:center">用户注册</h2> <form?action="./AddServlet"?method="post"?onsubmit="return check()"> <table?style="align:center;margin:auto;" > <tr?> <th?>登录账号:</th> <td?><input??type="text"?name="username"id="username"?placeholder="请输入登录账号"></td> </tr> <tr> <th?>登录密码:</th> <td><input??type="password"?name="password"id="password"placeholder="请输入登录密码"></td> </tr> <tr?> <th?>性别:</th> <td><input?type="radio"?name="sex"id="sex"?value="男"?checked="checked">男<input?type="radio"?name="sex"id="sex"?value="女">女</td> </tr> <tr?> <th?>姓名:</th> <td><input?type="text"?name="name"id="name"placeholder="请输入姓名"></td> </tr> <tr?> <th?>学号:</th> <td><input?type="text"?name="stunumber"id="stunumber"placeholder="请输入学号"></td> </tr> <tr?> <th?>电子邮件:</th> <td><input?type="text"?name="mail"id="mail"placeholder="请输入电子邮件"></td> </tr> <tr?> <th?>所在学校:</th> <td><input?type="text"?name="school"id="school"placeholder="请输入所在学校"></td> </tr> <tr?> <th?>所在系:</th> <td><input?type="text"?name="xi"id="xi"placeholder="请输入所在系"></td> </tr> <tr?> <th?>所在班级:</th> <td><input?type="text"?name="class1"id="class1"placeholder="请输入所在班级"></td> </tr> <tr?> <th?>入学年份:</th> <td><select?name="stuyear"id="stuyear"> <option?value="2019届">2019届</option> <option?value="2018届">2018届</option> <option?value="2017届">2017届</option> <option?value="2016届">2016届</option> </select></td> </tr> <tr?> <th?>生源地:</th> <td><input?type="text"?name="place"id="place"placeholder="请输入生源地"></td> </tr> <tr?> <th?>备注:</th> <td><input?type="text"?name="beizhu"id="beizhu"placeholder="无"></td> </tr> <tr?> <td?colspan="2"align="center"><input?type="submit"?value="注册"?> ????? <input?type="reset"?value="重置"?></td> </tr> </table> </form> ? <!-- 输入字段验证部分 --> <script?type="text/javascript"> function?check(){ var?username=document.getElementById("username"); var?password=document.getElementById("password"); var?stunumber=document.getElementById("stunumber"); var?name=document.getElementById("name"); var?mail=document.getElementById("mail"); var?sReg = /[_a-zA-Zd-.][email?protected][_a-zA-Zd-]+(.[_a-zA-Zd-]+)+$/; //正则表达式 //判断登陆账号位数 if((username.value).length<6||(username.value).length>12){ alert(‘请输入6到12位英文字母、数字和下划线,以英文字母开头‘); username.focus(); return?false; } //判断登陆账号是否包含汉字 if(/.*[u4e00-u9fa5]+.*$/.test(username.value)){ alert(‘用户名不能包含汉字‘); username.focus(); return?false; } //判断登陆账号是否以英文字母开头 if(!isNaN(username.value[0])){ alert(‘请以英文字母开头‘); username.focus(); return?false; } //判断登录密码位数 if((password.value).length<8){ alert(‘密码应为8位以上英文或数字‘); password.focus(); return?false; } //验证学号是否合法 if((stunumber.value).length!=8) { alert(‘学号应为8位以2018为开头的数字‘); stunumber.focus(); return?false; } else?if(stunumber.value[3]!=8) { alert(‘学号应为8位以2018为开头的数字‘); stunumber.focus(); return?false; } //判断姓名是否为空 if(name.value==‘‘){ alert(‘姓名不能为空‘); name.focus(); return?false; } //判断电子邮件格式是否正确 if(! sReg.test(mail.value)){ alert(‘电子邮件格式错误‘); mail.focus(); return?false; } } </script> <!-- 验证结束 --> ? </body> </html> ? ? fail.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> <h1?style="text-align:center;">添加失败</h1> <h1?style="text-align:center;"><a?href="index.jsp">返回继续添加信息</a></h1> </body> </html> ? success.jps: <%@ page language="java" contentType="text/html; charset=UTF-8" ? Name.java: package?com.login1.name; ? public?class?Name { private?int?id; private?String username; private?String password; private?String sex; private?String name; private?String stunumber; private?String mail; private?String school; private?String xi; private?String class1; private?String stuyear; private?String place; private?String beizhu; ? public?int?getId() { return?id; } public?void?setId(int?id) { this.id?= id; } public?String getUsername() { return?username; } public?void?setUsername(String username) { this.username?= username; } public?String getPassword() { return?password; } public?void?setPassword(String password) { this.password?= password; } public?String getSex() { return?sex; } public?void?setSex(String sex) { this.sex?= sex; } public?String getName() { return?name; } public?void?setName(String name) { this.name?= name; } public?String getStunumber() { return?stunumber; } public?void?setStunumber(String stunumber) { this.stunumber?= stunumber; } public?String getMail() { return?mail; } public?void?setMail(String mail) { this.mail?= mail; } public?String getSchool() { return?school; } public?void?setSchool(String school) { this.school?= school; } public?String getXi() { return?xi; } public?void?setXi(String xi) { this.xi?= xi; } public?String getClass1() { return?class1; } public?void?setClass1(String class1) { this.class1?= class1; } public?String getStuyear() { return?stuyear; } public?void?setStuyear(String stuyear) { this.stuyear?= stuyear; } public?String getPlace() { return?place; } public?void?setPlace(String place) { this.place?= place; } public?String getBeizhu() { return?beizhu; } public?void?setBeizhu(String beizhu) { this.beizhu?= beizhu; } ? public?Name(int?id,String username,String password,String sex,String name,String stunumber,String mail, String school,String xi,String class1,String stuyear,String place,String beizhu) { super(); this.id?= id; this.username?= username; this.password?= password; this.sex?= sex; this.name?= name; this.stunumber?= stunumber; this.mail?= mail; this.school?= school; this.xi?= xi; this.class1?= class1; this.stuyear?= stuyear; this.place?= place; this.beizhu?= beizhu; } public?Name(String username,String school, String xi,String beizhu) { super(); this.username?= username; this.password?= password; this.sex?= sex; this.name?= name; this.stunumber?= stunumber; this.mail?= mail; this.school?= school; this.xi?= xi; this.class1?= class1; this.stuyear?= stuyear; this.place?= place; this.beizhu?= beizhu; } } ? ?DBUtil.java: ? package?com.login1.util; ? import?java.sql.Connection; import?java.sql.DriverManager; import?java.sql.ResultSet; import?java.sql.SQLException; import?java.sql.Statement; ? public?class?DBUtil { ????public?static?String db_url="jdbc:mysql://127.0.0.1:3306/javaweb?useUnicode=ture&" ????????????+ "characterEncoding=utf8&serverTimezone=GMT&useSSL=false"; ????public?static?String db_user="root"; ????public?static?String db_password="123456"; ????public?static?Connection getConn() ????{ ????????Connection conn=null; ????????try ????????{ ????????????Class.forName("com.mysql.cj.jdbc.Driver"); ????????????conn=DriverManager.getConnection(db_url,db_user,db_password); ????????????System.out.println("数据库连接成功"); ????????}catch(Exception e) ????????{ ????????????e.printStackTrace(); ????????} ????????return?conn; ????} ????public?static?void?close (Statement state,Connection conn) ????{ ????????if(state!=null) ????????{ ????????????try ????????????{ ????????????????state.close(); ????????????????//System.out.println("state关闭"); ????????????}catch(SQLException e) ????????????{ ????????????????e.printStackTrace(); ????????????} ????????} ????????if(conn!=null) ????????{ ????????????try ????????????{ ????????????????conn.close(); ????????????????//System.out.println("conn关闭"); ????????????}catch(SQLException e) ????????????{ ????????????????e.printStackTrace(); ????????????} ????????} ????} ????public?static?void?close (ResultSet rs,Statement state,Connection conn) ????{ ????????if(rs!=null) ????????{ ????????????try ????????????{ ????????????????rs.close(); ????????????????//System.out.println("rs关闭"); ????????????}catch(SQLException e) ????????????{ ????????????????e.printStackTrace(); ????????????} ????????} ????????if(state!=null) ????????{ ????????????try ????????????{ ????????????????state.close(); ????????????????//System.out.println("state关闭"); ????????????}catch(SQLException e) ????????????{ ????????????????e.printStackTrace(); ????????????} ????????} ????????if(conn!=null) ????????{ ????????????try ????????????{ ????????????????conn.close(); ????????????????//System.out.println("conn关闭"); ????????????}catch(SQLException e) ????????????{ ????????????????e.printStackTrace(); ????????????} ????????} ????} ????public?static?void?main(String[] args) { ???? Connection conn?= DBUtil.getConn(); } ? } ? Dao.java: package?com.login1.dao; ? import?java.sql.Connection; import?java.sql.SQLException; import?java.sql.Statement; ? import?com.login1.name.Name; import?com.login1.util.DBUtil; public?class?Dao { //将数据添加到数据库 public?boolean?add(Name login1) ????{ ????????Connection conn?= DBUtil.getConn(); ????????Statement state?= null; ???????? ????????//添加的SQL语句,将信息录入到login1表中 ????????String sql="insert into login1(username,password,sex,name,stunumber,mail,school,xi,class1,stuyear,place,beizhu) values(‘"+login1.getUsername()+"‘,‘"+login1.getPassword()+"‘,‘"+login1.getSex()+"‘,‘"+login1.getName()+ ????????????????"‘,‘"+login1.getStunumber()+"‘,‘"+login1.getMail()+"‘,‘"+login1.getSchool()+"‘,‘"+login1.getXi()+"‘,‘"+login1.getClass1()+"‘,‘"+login1.getStuyear()+"‘,‘"+login1.getPlace()+"‘,‘"+login1.getBeizhu()+"‘)"; ????????int?a=0; ????????try ????????{ ????????????state?= conn.createStatement(); ????????????a=state.executeUpdate(sql); ????????}catch?(SQLException e) ????????{ ????????????e.printStackTrace(); ????????????return?false; ????????}finally ????????{ ????????????DBUtil.close(state,conn); ????????} ????????if(a>0)return?true; ????????else?return?false; ????} ? } ? AddServlet.java: package?com.login.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?com.login1.dao.Dao; import?com.login1.name.Name; ? /** ?* Servlet?implementation class AddServlet ?*/ @WebServlet("/AddServlet") public?class?AddServlet extends?HttpServlet { private?static?final?long?serialVersionUID?= 1L; ????Dao dao?=new?Dao(); ? ????/** ?????* @see?HttpServlet#HttpServlet() ?????*/ ????public?AddServlet() { ????????super(); ????????// TODO?Auto-generated constructor stub ????} ? /** ?* @see?HttpServlet#doGet(HttpServletRequest request,HttpServletResponse response) ?*/ protected?void?doGet(HttpServletRequest request,HttpServletResponse response) throws?ServletException,IOException { request.setCharacterEncoding("utf-8"); String username=request.getParameter("username"); String password=request.getParameter("password"); String sex=request.getParameter("sex"); String name=request.getParameter("name"); String stunumber=request.getParameter("stunumber"); String mail=request.getParameter("mail"); String school=request.getParameter("school"); String xi=request.getParameter("xi"); String class1=request.getParameter("class1"); String stuyear=request.getParameter("stuyear"); String place=request.getParameter("place"); String beizhu=request.getParameter("beizhu"); ? Name login1?=new?Name(username,beizhu); ? if(dao.add(login1)) { ????????????request.getRequestDispatcher("success.jsp").forward(request,response); } else? { ????????????request.getRequestDispatcher("fail.jsp").forward(request,response); ????????} } ? /** ?* @see?HttpServlet#doPost(HttpServletRequest request,HttpServletResponse response) ?*/ protected?void?doPost(HttpServletRequest request,IOException { // TODO?Auto-generated method stub doGet(request,response); } ? } ? (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |