注册的问题
package com.imooc.jdbc.servlet; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.Date; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.imooc.jdbc.bean.User; import com.imooc.jdbc.service.RegistService; public class RegistServlet extends HttpServlet{ private RegistService registService; @Override public void init() throws ServletException { // TODO Auto-generated method stub super.init(); registService = new RegistService(); } @Override protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String username = request.getParameter("username"); String password = request.getParameter("password"); String real_name = request.getParameter("real_name"); String birthday = request.getParameter("birthday"); String phone = request.getParameter("phone"); String address = request.getParameter("address"); boolean result = false; try { SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd"); Date dateBirthday = simpleDateFormat.parse(birthday); User user = new User(username,password,real_name,dateBirthday,phone,address); result = registService.registUser(user); if (result) { System.out.println("注册成功"); request.getRequestDispatcher("/login.do").forward(request, response); }else { System.out.println("注册失败"); request.getRequestDispatcher("/regist.do").forward(request, response); } } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } } }
package com.imooc.jdbc.service; import com.imooc.jdbc.bean.User; import com.imooc.jdbc.dao.RegistDAO; public class RegistService { private RegistDAO registDAO; public RegistService() { registDAO = new RegistDAO(); } public Boolean registUser(User user) { return registDAO.registUser(user); } }
package com.imooc.jdbc.dao; import java.sql.Connection; import java.sql.Date; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import com.imooc.jdbc.bean.User; import com.imooc.jdbc.common.ConnectionUtil; public class RegistDAO { /** * 用户注册 */ public boolean registUser(User user) { Connection conn = ConnectionUtil.getConnection(); String sql = "select username from user "; PreparedStatement stmt = null; ResultSet rs = null; try { stmt = conn.prepareStatement(sql); rs = stmt.executeQuery(); // 判断数据库是否已经有该用户了 while(rs.next()) { if(rs.getString("username").equals(user.getName())) { return false; } } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { ConnectionUtil.release(rs, stmt, conn); } return notRegist(user); } // 将注册的用户存入到数据库中 public boolean notRegist(User user) { Connection conn = ConnectionUtil.getConnection(); PreparedStatement stmt = null; String sql = "update user set username = ?,password = ?,real_name=?,birthday = ?,phone = ?,address = ?"; try { // 若没有该用户,则将该用户存入到数据库中 stmt = conn.prepareStatement(sql); stmt.setString(1, user.getName()); stmt.setString(2, user.getPassword()); stmt.setString(3, user.getRealName()); stmt.setDate(4, new Date(user.getBirthday().getTime())); stmt.setString(5, user.getPhone()); stmt.setString(6, user.getAddress()); stmt.executeUpdate(); } catch (Exception e) { // TODO: handle exception }finally { ConnectionUtil.release(null, stmt, conn); } return true; } }
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path; %> <html> <head> <title>登录</title> <link rel="stylesheet" href="<%=request.getContextPath()%>/css/reg.css"> </head> <body> <div class="reg"> <div class="header"> <h1> <a href="<%=request.getContextPath()%>/login.do">登录</a> <a href="<%=request.getContextPath()%>/regPrompt.do">注册</a> </h1> <button></button> </div> <form action="<%=request.getContextPath()%>/regist.do" method="post"> <div class="name"> 用户名:<input type="text" id="name" name="username" placeholder="请输入所要注册的用户名"> <p></p> </div> <div class="pwd"> 密码:<input type="password" id="pwd" name="password" placeholder="6-16位密码,区分大小写,不能用空格"> <p></p> </div> <div class="name"> 姓名:<input type="text" id="real_name" name="real_name" placeholder="请输入所要注册的姓名"> <p></p> </div> <div class="name"> 生日日期:<input type="text" id="birthday" name="birthday" placeholder="请输入出生日期"> <p></p> </div> <div class="name"> 手机号码:<input type="text" id="phone" name="phone" placeholder="请输入电话号码"> <p></p> </div> <div class="name"> 地址:<input type="text" id="address" name="address" placeholder="请输入地址"> <p></p> </div> <div class="btn-red"> <input onclick="return checkVerificationCode();" type="submit" value="注册" id="reg-btn"> </div> </form> </div> </body> </html>
老师,这是我注册的代码,就是控制台出的是user为空,然后我去数据库中查的话也是没有查到,也就说用户没有存入到数据库。老师帮忙看看这段代码有什么逻辑错误?
31
收起
正在回答 回答被采纳积分+1
6回答
芝芝兰兰
2019-12-10 11:24:26
同学你好。
1、用户没有存入到数据库:
这边尝试同学的代码,是可以正常插入的:
同学说user为空,是说username,password 等属性没有获取到吗?同学的页面中,给提交按钮绑定了onclick事件onclick="return checkVerificationCode();",但没有JS代码实现,有可能会造成提交不成功。同学可以先确定一下是否提交成功了。
2、注册页面的白色的背景
将reg元素的高度改大一些就可以了。注意重启后清除浏览器缓存再访问。在浏览器中使用Ctrl+Shift+Delete清除缓存
如果解答了同学的疑问,望采纳~
祝学习愉快~
Java数据库开发与实战应用2018版
- 参与学习 人
- 提交作业 277 份
- 解答问题 4297 个
Java数据库开发的必备技能,从流行的MySQL数据库开始,到Java原生的数据库管理接口JDBC的使用,再到常用的数据持久化框架MyBatis,让你向Java工程师的目标又迈进了一步!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星