记录一下自己写的注册功能:用的编译器 eclipse 数据库 Mysql 服务器 tomcat
服务器搭建配置这里就直接过了(可以参考):https://www.cnblogs.com/2979100039-qq-con/p/12493329.html
一,建库建表
二、创建动态web项目:
下载完成后复制到 lib文件夹下
下面上代码:
jsp页面代码:
Insert title heremargin:0px 460px 0px 387px;
}
.rest:hover{
color:black;
width: 230px;
height: 26px;
background:#0066ff;
border: none;
border-radius:8px;
}
.rest {
color:black;
width: 230px;
height: 26px;
background: #cccccc;
border: none;
border-radius:8px;
}
用户名:
密 码 :
性别:男女
爱好:羽毛球篮球足球
var flag= true;
var admin= document.getElementById("username").value;
var password= document.getElementById("password").value;if (admin==""){
alert("账号不能为空,请输入账号!");
flag= false;return false;
}else if (password==""){
alert("密码不能为空 ,请输入密码!");
flag= false;return false;
}for(i=0;i
{if(from.sex[0].checked||from.sex[1].checked)
{return true;
}else{ alert("性别未选,请选择");return false; }if(flag == true){return true; }
}
servlet代码:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throwsServletException, IOException {
doPost(request, response);
response.getWriter().append("Served at: ").append(request.getContextPath());
}protected void doPost(HttpServletRequest request, HttpServletResponse response) throwsServletException, IOException {
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
String name= request.getParameter("username");
String pwd= request.getParameter("password");
String sex= request.getParameter("sex");
String[] happy= request.getParameterValues("habby");
StringBuffer buf= newStringBuffer();for(String string : happy) {
buf.append(string);
}
String string=buf.toString();boolean boo =UserDao.register(name,pwd,sex,string);if(boo) {
response.getWriter().print ("
response.getWriter().print ("alert('恭喜您 注册成功!')");
response.getWriter().print ("");
}/*response.getWriter().print("");*/}
User(实体类代码)
private intid;privateString name;privateString pwd;privateString sex;privateString hobby;public intgetId() {returnid;
}public void setId(intid) {this.id =id;
}publicString getName() {returnname;
}public voidsetName(String name) {this.name =name;
}publicString getPwd() {returnpwd;
}public voidsetPwd(String pwd) {this.pwd =pwd;
}publicString getSex() {returnsex;
}public voidsetSex(String sex) {this.sex =sex;
}publicString getHobby() {returnhobby;
}public voidsetHobby(String hobby) {this.hobby =hobby;
}public User(intid, String name, String pwd, String sex, String hobby) {super();this.id =id;this.name =name;this.pwd =pwd;this.sex =sex;this.hobby =hobby;
}
JDBC代码就是连接数据库的
private static final String DRIVER="com.mysql.cj.jdbc.Driver";private static final String URL="jdbc:mysql://localhost:3306/user?serverTimezone=UTC";private static final String NAME="root";private static final String PWD="root";static{try{
Class.forName(DRIVER);
}catch(ClassNotFoundException e) {
e.printStackTrace();
}
}private staticConnection getconnection() {try{returnDriverManager.getConnection(URL,NAME,PWD);
}catch(SQLException e) {
e.printStackTrace();
}return null;
}private staticPreparedStatement getpreparedStatement(String sql,Object...objects) {try{
PreparedStatement prepareStatement=getconnection().prepareStatement(sql);for (int i = 0; i < objects.length; i++) {
prepareStatement.setObject(i+1, objects[i]);
}returnprepareStatement;
}catch(SQLException e) {
e.printStackTrace();
}return null;
}public static booleanexecuteUpade(String sql,Object...objects) {try{int updae =getpreparedStatement(sql, objects).executeUpdate();if(updae>0) return true;
}catch(SQLException e) {
e.printStackTrace();
}return false;
}
UserDao代码
public static booleanregister(String name, String pwd, String sex, String string) {return JDBCUtil.executeUpade("INSERT INTO `tb_user` (`name`, `pwd`, `sex`, `hobby`) VALUES (?,?,?,?)",name,pwd,sex,string);
}
代码结束
看起来其实代码量很多,但是是自己花了半天一点点写出来的,如果有不足之处希望可以告知,谢谢!
如有不懂的地方可以留言询问,或者联系qq 2979100039 我会尽力帮忙 over