软工第二次极限测试
极限测试没做出来。
今天早上6点+下火车,7点到校,一上午的课,中午困得要死,直接睡了。
下午极限测试,大家都在写代码连数据库,而我在下软件配环境。
根据多方询问,我了解到需要下载Tomcat,MySQL以及Navicat for MySQL。
我下载了最新版本的Tomcat,最新版本的MySQL以及最新版本的Navicat for MySQL,由于javaSE不能配最新的Tomcat(我尝试了下插件,但是后面还是出了问题,这时已经下午4点了),然后我放弃了SE下载了最新版本的javaEE和最新版本的JDK。(又重新配了一波环境变量)。
但由于最新版本的MySQL的连接方式与以往不同,而最新版本的Navicat for MySQL的连接方式还是旧的,导致数据库无法连接。
这时候,各种百度,cmd,各种命令,终于调好了。
于是,在配置了一下午之后,我终于连接上了数据库。
1 package bean; 2 3 public class Login { 4 5 int id; 6 String user; 7 String pwd; 8 String name; 9 String sex; 10 String number; 11 String email; 12 String yuan; 13 String xi; 14 String ban; 15 String year; 16 String area; 17 String beizhu; 18 19 20 21 public Login(String user,String pwd,String name,String sex,String number,String email,String yuan,String xi,String ban,String year,String area,String beizhu) { 22 // TODO �Զ����ɵĹ��캯����� 23 this.user=user; 24 this.pwd=pwd; 25 this.name=name; 26 this.sex=sex; 27 this.number=number; 28 this.email=email; 29 this.yuan=yuan; 30 this.xi=xi; 31 this.ban=ban; 32 this.year=year; 33 this.area=area; 34 this.beizhu=beizhu; 35 } 36 37 public Login(int id,String user,String pwd,String name,String sex,String number,String email,String yuan,String xi,String ban,String year,String area,String beizhu) { 38 this.id=id; 39 this.user=user; 40 this.pwd=pwd; 41 this.name=name; 42 this.sex=sex; 43 this.number=number; 44 this.email=email; 45 this.yuan=yuan; 46 this.xi=xi; 47 this.ban=ban; 48 this.year=year; 49 this.area=area; 50 this.beizhu=beizhu; 51 } 52 53 public int getId() { 54 return id; 55 } 56 57 public void setId(int id) { 58 this.id = id; 59 } 60 61 public String getUser() { 62 return user; 63 } 64 65 public void setUser(String user) { 66 this.user = user; 67 } 68 69 public String getPwd() { 70 return pwd; 71 } 72 73 public void setPwd(String pwd) { 74 this.pwd = pwd; 75 } 76 77 public String getName() { 78 return name; 79 } 80 81 public void setName(String name) { 82 this.name = name; 83 } 84 85 public String getSex() { 86 return sex; 87 } 88 89 public void setSex(String sex) { 90 this.sex = sex; 91 } 92 93 public String getNumber() { 94 return number; 95 } 96 97 public void setNumber(String number) { 98 this.number = number; 99 } 100 101 public String getEmail() { 102 return email; 103 } 104 105 public void setEmail(String email) { 106 this.email = email; 107 } 108 109 public String getYuan() { 110 return yuan; 111 } 112 113 public void setYuan(String yuan) { 114 this.yuan = yuan; 115 } 116 117 public String getXi() { 118 return xi; 119 } 120 121 public void setXi(String xi) { 122 this.xi = xi; 123 } 124 125 public String getBan() { 126 return ban; 127 } 128 129 public void setBan(String ban) { 130 this.ban = ban; 131 } 132 133 public String getYear() { 134 return year; 135 } 136 137 public void setYear(String year) { 138 this.year = year; 139 } 140 141 public String getArea() { 142 return area; 143 } 144 145 public void setArea(String area) { 146 this.area = area; 147 } 148 149 public String getBeizhu() { 150 return beizhu; 151 } 152 153 public void setBeizhu(String beizhu) { 154 this.beizhu = beizhu; 155 } 156 157 }
1 package dao; 2 3 import java.sql.Connection; 4 import java.sql.SQLException; 5 import java.sql.Statement; 6 7 import bean.Login; 8 import util.BaseConnection; 9 10 public class Add { 11 //增 12 public static boolean add1(Login login) 13 { 14 boolean f = false; 15 Connection conn = BaseConnection.getConnection(); 16 //PreparedStatement ps=null; 17 18 String sql = "insert into login(user,pwd,name,sex,number,email,yuan,xi,ban,year,area,beizhu) values('" + login.getUser() + "','" + login.getPwd() + "','" + login.getName() + "','" +login.getSex() + "','" +login.getNumber() + "','" +login.getEmail() + "','" + login.getYuan() + "','" + login.getXi() + "','" + login.getBan() + "','" + login.getYear() + "','" + login.getArea() + "','" + login.getBeizhu() + "')"; 19 //String sql = "insert into course1(name,teacher,classroom) values('" + cour.getName() + "','" + cour.getTeacher() + "','" + cour.getClassroom() + "')'"; 20 Statement state = null; 21 int a = 0; 22 try { 23 state = conn.createStatement(); 24 a = state.executeUpdate(sql); 25 26 27 } catch (SQLException e) { 28 // TODO Զ ɵ catch 29 e.printStackTrace(); 30 } 31 32 if(a >0) 33 { 34 System.out.println(" 122 "); 35 f = true; 36 }else { 37 System.out.println("12 "); 38 } 39 return f; 40 } 41 public static void main(String[] args) { 42 Login login = new Login("1","2","3","4","5","6","7","8","9","10","11","12"); 43 add1(login); 44 } 45 }
1 package dao; 2 3 import java.util.regex.Matcher; 4 import java.util.regex.Pattern; 5 6 public class check { 7 8 /** 9 * 验证邮箱地址是否正确 10 * @param email 11 * @return 12 */ 13 public static boolean checkEmail(String email){ 14 boolean flag = false; 15 try{ 16 String check = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$"; 17 Pattern regex = Pattern.compile(check); 18 Matcher matcher = regex.matcher(email); 19 flag = matcher.matches(); 20 }catch(Exception e){ 21 //LOG.error("验证邮箱地址错误", e); 22 flag = false; 23 } 24 25 return flag; 26 } 27 /** 28 * 验证学号 29 * @param mobiles 30 * @return 31 */ 32 public static boolean isMobileNO(String mobiles){ 33 boolean flag = false; 34 try{ 35 Pattern p = Pattern.compile("^2018\\d{4}$"); 36 Matcher m = p.matcher(mobiles); 37 flag = m.matches(); 38 }catch(Exception e){ 39 //LOG.error("验证手机号码错误", e); 40 flag = false; 41 } 42 return flag; 43 } 44 /** 45 * 验证登录名 46 * 47 */ 48 public static boolean isUserNO(String user){ 49 boolean flag = false; 50 try{ 51 Pattern p = Pattern.compile("^[a-zA-Z][a-zA-Z0-9_]*\\d{5,11}$"); 52 Matcher m = p.matcher(user); 53 flag = m.matches(); 54 }catch(Exception e){ 55 //LOG.error("验证手机号码错误", e); 56 flag = false; 57 } 58 return flag; 59 } 60 /** 61 * 验证密码 62 * 63 */ 64 public static boolean isPwdNO(String pwd){ 65 boolean flag = false; 66 try{ 67 Pattern p = Pattern.compile("^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{8,16}$"); 68 Matcher m = p.matcher(pwd); 69 flag = m.matches(); 70 }catch(Exception e){ 71 //LOG.error("验证手机号码错误", e); 72 flag = false; 73 } 74 return flag; 75 } 76 }
1 package servlet; 2 3 import java.io.IOException; 4 import java.io.UnsupportedEncodingException; 5 6 import javax.servlet.ServletException; 7 import javax.servlet.annotation.WebServlet; 8 import javax.servlet.http.HttpServlet; 9 import javax.servlet.http.HttpServletRequest; 10 import javax.servlet.http.HttpServletResponse; 11 12 import org.apache.taglibs.standard.tag.el.fmt.RequestEncodingTag; 13 14 import bean.Login; 15 import dao.Add; 16 import dao.check; 17 18 /** 19 * Servlet implementation class Servlet 20 */ 21 @WebServlet("/Servlet") 22 public class Servlet extends HttpServlet { 23 private static final long serialVersionUID = 1L; 24 25 /** 26 * @see HttpServlet#HttpServlet() 27 */ 28 public Servlet() { 29 super(); 30 // TODO Auto-generated constructor stub 31 } 32 33 /** 34 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) 35 */ 36 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 37 // TODO Auto-generated method stub 38 //response.getWriter().append("Served at: ").append(request.getContextPath());request. 39 request.setCharacterEncoding("utf-8"); 40 String method=request.getParameter("method"); 41 if(method.equals("add")) 42 { 43 add(request,response); 44 } 45 } 46 47 private void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 48 // TODO 自动生成的方法存根 49 request.setCharacterEncoding("utf-8"); 50 String user=request.getParameter("user"); 51 String pwd=request.getParameter("pwd"); 52 String name=request.getParameter("name"); 53 String sex=request.getParameter("sex"); 54 String number=request.getParameter("number"); 55 String email=request.getParameter("email"); 56 String yuan=request.getParameter("yuan"); 57 String xi=request.getParameter("xi"); 58 String ban=request.getParameter("ban"); 59 String year=request.getParameter("year"); 60 String area=request.getParameter("area"); 61 String beizhu=request.getParameter("beizhu"); 62 63 64 65 Login a=new Login(user,pwd,name,sex,number,email,yuan,xi,ban,year,area,beizhu); 66 67 //用户 68 if (check.isUserNO(user)&&check.isPwdNO(pwd)&&check.checkEmail(email)&&check.isMobileNO(number) ){ 69 Login login = new Login(user,pwd,name,sex,number,email,yuan,xi,ban,year,area,beizhu); 70 if (Add.add1(login)) { 71 request.setAttribute("message", "注册成功"); 72 request.getRequestDispatcher("login.jsp").forward(request,response); 73 }else { 74 request.setAttribute("message", "注册失败"); 75 request.getRequestDispatcher("login.jsp").forward(request,response); 76 } 77 } 78 else if(!check.isUserNO(user)){ 79 request.setAttribute("message", "登录名的格式不正确"); 80 request.getRequestDispatcher("login.jsp").forward(request,response); 81 } 82 else if(!check.isPwdNO(pwd)){ 83 request.setAttribute("message", "密码格式不正确"); 84 request.getRequestDispatcher("login.jsp").forward(request,response); 85 } 86 else if (!check.checkEmail(email)){ 87 request.setAttribute("message", "电子邮件格式不符"); 88 request.getRequestDispatcher("login.jsp").forward(request,response); 89 } 90 else if(!check.isMobileNO(number)) { 91 request.setAttribute("message", "学号格式不符"); 92 request.getRequestDispatcher("login.jsp").forward(request,response); 93 } 94 95 } 96 97 98 99 100 /** 101 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) 102 */ 103 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 104 // TODO Auto-generated method stub 105 doGet(request, response); 106 } 107 108 }
1 package util; 2 3 import java.sql.Connection; 4 import java.sql.DriverManager; 5 import java.sql.ResultSet; 6 import java.sql.SQLException; 7 import java.sql.Statement; 8 9 public class BaseConnection { 10 11 public static Connection getConnection(){//�����������ȡmysql������ 12 Connection conn=null; 13 String driver = "com.mysql.jdbc.Driver"; 14 String url="jdbc:mysql://localhost:3306/suib?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=Hongkong"; 15 //String url = "jdbc:mysql://localhost:3306/suib?characterEncoding=utf8&useSSL=true";//�����ݿ����� 16 String user = "root"; 17 String password = "123456"; 18 try{ 19 Class.forName(driver);//���������� 20 conn=DriverManager. 21 getConnection(url,user,password);//��url���ݿ��IP��ַ��user���ݿ��û�����password���ݿ����룩 22 }catch(Exception e){ 23 e.printStackTrace(); 24 } 25 return conn; 26 } 27 //�ر����ݿ����� 28 public static void close (Statement state, Connection conn) { 29 if (state != null) { 30 try { 31 state.close(); 32 } catch (SQLException e) { 33 e.printStackTrace(); 34 } 35 } 36 37 if (conn != null) { 38 try { 39 conn.close(); 40 } catch (SQLException e) { 41 e.printStackTrace(); 42 } 43 } 44 } 45 //�ر����ݿ����� 46 public static void close (ResultSet rs, Statement state, Connection conn) { 47 if (rs != null) { 48 try { 49 rs.close(); 50 } catch (SQLException e) { 51 e.printStackTrace(); 52 } 53 } 54 55 if (state != null) { 56 try { 57 state.close(); 58 } catch (SQLException e) { 59 e.printStackTrace(); 60 } 61 } 62 63 if (conn != null) { 64 try { 65 conn.close(); 66 } catch (SQLException e) { 67 e.printStackTrace(); 68 } 69 } 70 } 71 72 public static void main(String[] args) { 73 getConnection(); 74 } 75 76 }