Realization of student information to add interface (connecting to the database)

 

 

 

1 login account: the claims . 6 to 1 2 letters, numbers, underscores, only letters can begin; ( 1 min)

2 Password: required to display "•" or " * " indicates that the input digit password required eight or more letters, digits. ( 1 min)

3 Sex: required to achieve a single box or drop-down box, the options only "male" or "female"; ( 1 minute)

4 Student Number: required eight digits, the first four bits " 2 018" at the beginning, enter their student ID; (1 min)

5 Name: Enter your name;

5 E-mail: request determining correct format [email protected] ; ( . 1 min)

6 Click the "Add" button to store students' personal information to the database. ( 3 points)

7 can be connected to the demo database. ( 2 points)

 

 

1.-HTML5 interface, js content is determined to meet the requirements entered.

 

<%@ 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>
<style>
p{
text-align:center;
}
</style>
</head>
<body>
<P> The current interface: add student information </ p>
<form action="aaa" method="post">
<P> login account: <input type = "text" placeholder = "Please enter the login account" name = "text" id = "itext" /> </ p>
<P> Login Password: <input type = "password" placeholder = "Please enter the login password" name = "password" id = "ipassword" /> </ p>
<p>性别:<input type="radio" name="sex" value="男" />男   <input type="radio" name="sex" value="女"/>女</p>
<P> Name: <input type = "text" placeholder = "Please enter the name" name = "myname" onblur = "mima ()" /> </ p>
<P> Student Number: <input type = "text" placeholder = "Please enter the student number" name = "phone" id = "iphone" /> </ p>
<P> Email: <input id = "email" name = "email" type = "email" placeholder = "Please enter the email" /> </ p>
<p>所在学院:<input  type="text" name="school" id="ishaool" /></p>
<p>所在系:<input type="text" name="xi" id="ixi"/></p>
<p>所在班级:<input type="text" name="class" id="iclass"/></p>
<P> Start year: <select name = "year">
        <Option value = "- Select -" id = "select"> select </ option>
		<option value="2017" id="2017">2017 </option>
		<option value="2018" id="2018">2018</option>
		<option value="2019" id="2019">2019</option> </select>
</p>
<p>生源地:<input type="text"  name="area" id="arge" /></p>
<p>备注:<textarea name="beizhu">


         </textarea></p>

<p style="text-align:center"><input type="submit" value="添加"  onclick="return check()"></p>

<script type="text/javascript">
  function check()
  {
	  var atext=document.getElementById("itext").value;
	  if(!((atext[0]>='a'&&atext[0]<='z')||(atext[0]>='A'&&atext[0]<='Z'))||atext.length<6||atext.length>12)
			{
			   alert ( "Please enter a six to twelve English or numbers, or underscores, first place must be in English username!");
			   return false;
			}
		var apassword=document.getElementById("ipassword").value;
		if(apassword.length<8)
		{
			   alert ( "Please enter a password of letters and numbers eight or more!");
			   return false;
			}
		var aphone=document.getElementById("iphone").value;
		was bphone = / ^ [2] [0] [1] [8] [0-9] {4} $ /;
		if(!bphone.test(aphone))
		{
			   alert ( "Please enter the beginning of 2018 to eight-digit student number!");
			   return false;
			}
		var eValue = document.getElementById("eamil").value;
 		if (/ ^ ([a-zA-Z0-9 _-]) + @ ([a-zA-Z0-9 _-]) + (. [a-zA-Z0-9 _-]) + /. test ( Evaluate)) {
	 			alert ( "mailbox format is not correct!");
	 			 return false;
	 	}
 		return true;
  }
</script>
</form>
</body>
</html>

 

  2. database connection; connect to the database, data can be imported into the database

package ceshi;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.print.attribute.standard.RequestingUserName;

import jdk.nashorn.internal.ir.GetSplitState;
import jdk.nashorn.internal.runtime.linker.NashornBeansLinker;

public class DBUtil {
	// URL database and account password
			public static final String connectionURL="jdbc:mysql://localhost:3306/new_schema1?useUnicode=true&characterEncoding=GB18030&useSSL=false&serverTimezone=GMT&allowPublicKeyRetrieval=true";
			public static final String username="root";
			public static final String password = "login password";
			static Connection connection;
			static ResultSet rSet;
			static PreparedStatement sql;
			//Database Connectivity
			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 ( "Database connection failed");
					e.printStackTrace ();
				}
				return null;
			} //Database Connectivity
			
			
			

			public static boolean  addUsername(String  text,String password, String sex,String name,String  phone,String email, String xueyuan,String xi,String aclass ,String year,String area,String beizhu )//增添
			{
				try {
					connection=getConnection();
					
					sql =connection.prepareStatement("insert into new_table (text,password,sex,name,number,email,xueyuan,xi,class,year,area,beizhu) values(\'"+text+"\',\'"+password+"\',\'"+sex+"\',\'"+name+"\',\'"+phone+"\',\'"+email+"\',\'"+xueyuan+"\',\'"+xi+"\',\'"+aclass+"\',\'"+year+"\',\'"+area+"\',\'"+beizhu+"\')");
				    sql.executeUpdate();
				    System.out.println(sql);
				} catch (SQLException e) {
					// TODO Auto-generated catch 
					e.printStackTrace ();
				}
				
				return false;
				
			}
public static void main(String[] args) {
}}

 3.Svrelet operation, Servlet creates a class to implement a data connection jsp and Mysql

package serlet;

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 ceshi.DBUtil;
import com.sun.corba.se.spi.legacy.interceptor.RequestInfoExt;

/**
 * Servlet implementation class aaa
 */
@WebServlet("/aaa")
public class aaa extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
   
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		String text=request.getParameter("text");
		String password=request.getParameter("password");
		String sex=request.getParameter("sex");
		String name=request.getParameter("myname");
		String phone=request.getParameter("phone");
		String email=request.getParameter("email");
		String xueyuan=request.getParameter("school");
		String xi=request.getParameter("xi");
		String aclass=request.getParameter("class");
		String year=request.getParameter("year");
		String area=request.getParameter("area");
		String beizhu=request.getParameter("beizhu");
		DBUtil.addUsername(text,password,  sex, name, phone, email,  xueyuan,xi,aclass , year, area, beizhu );//增添
	}

	/**
	 * @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);
	}

}

 4. Testing program, the information stored in the database. 

 

 

Guess you like

Origin www.cnblogs.com/zwx655/p/11715224.html