标签:web JAVA String request getParameter sql return 课堂 name
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; width: 160px; color: white; background-color: greenyellow; } </style> </head> <body onl oad="getResult()"> <table align="center"> <div align="center"> <h1 style="color: red;">当前位置:添加学生信息</h1> <form action="shujuku" method="post" > <tr> <td>登入账号:</td> <td><input type="text" id="ID" name="ID" value="" ></td> </tr> <tr> <td>登入密码:</td> <td> <input type="password" id="mima" name="mima" value="" ></td> </tr> <tr> <td>性 别:</td> <td> <select id="sex" name="sex" > <option>--请选择--</option> <option value="男">男</option> <option value="女">女</option> </select> </td> </tr> <tr> <td>姓 名:</td> <td> <input type="text" name="name" value="" ></td> </tr> <tr> <td>学 号:</td> <td> <input type="text" name="xuehao" id="xuehao" value="" ></td> </tr> <tr> <td>电子邮件:</td> <td> <input type="text" id="Email" name="Email" value="" ></td> </tr> <tr> <td>所在学院:</td> <td> <input type="text" id="college" name="college" value="" ></td> </tr> <tr> <td>所在系:</td> <td> <input type="text" id="major" name="major" value="" ></td> </tr> <tr> <td>所在班级:</td> <td> <input type="text" id="Class" name="Class" value="" ></td> </tr> <tr> <td>入学年份(届):</td> <td> <select id="year" name="year"> <option>--请选择--</option> <option value="2018">2018</option> <option value="2017">2019</option> <option value="2016">2018</option> <option value="2015">2019</option> <option value="2014">2018</option> </select> </td> </tr> <tr> <td>生源地:</td> <td> <input type="text" id="place" name="place" value="" ></td> </tr> <tr> <td>备注:</td> <td> <input type="text" id="beizhu" name="beizhu" value="" ></td> </tr> <tr> <td> <button type="submit" >添 加</button></td> </tr> </table> </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>
标签:web,JAVA,String,request,getParameter,sql,return,课堂,name 来源: https://www.cnblogs.com/xjmm/p/11715699.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。