我在数据库的表里面自己插入了几个数据。然后登陆它显示没有
因为无法插入数据所以有些地方我还没完善
index.jsp
<%@ page language="java" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE HTML>
<html>
<head>
<title>首页</title>
<script type="text/javascript">
function doLogout(){
//访问LogoutServlet注销当前登录的用户
window.location.href="${pageContext.request.contextPath}/servlet/LogoutServlet";
}
</script>
</head>
<body>
<h1>123</h1>
<hr/>
<c:if test="${user==null}">
<a href="${pageContext.request.contextPath}/servlet/RegisterUIServlet" target="_blank">注册</a>
<a href="/FirstTry/servlet/LoginUIServlet">登陆</a>
</c:if>
<c:if test="${user!=null}">
欢迎您:${user.userName}
<input type="button" value="退出登陆" onclick="doLogout()">
</c:if>
<hr/>
</body>
</html>
Register.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>用户注册</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<script type="text/javascript">
function valid(form)
{if(form.username.value.length==0)
{alert("请输入用户名!");
return false;
}
if(form.password.value.length==0)
{alert("请输入密码!");
return false;
}
if(form.confirmPwd.value.length==0)
{alert("请输入确认密码!");
return false;
}
if(form.password.value!=form.confirmPwd.value)
{
alert("两次输入的密码不一致");
}
if(form.email.value.length==0)
{alert("请输入邮箱");
return false;
}
return true;
}
</script>
<form action="${pageContext.request.contextPath}/RegisterServlet" method="post" onsubmit="return valid(this)">
<table width="60%" border="1">
<tr>
<td>用户名</td>
<td>
<input type="text" name="username" >
</td>
<td><font color="red">*必须填写</font>
</td>
</tr>
<tr>
<td>密码</td>
<td>
<input type="password" name="password" >
</td>
<td><font color="red">*必须填写</font>
</td>
</tr>
<tr>
<td>确认密码</td>
<td>
<input type="password" name="confirmPwd" >
</td>
</tr>
<tr>
<tr>
<td>性别</td>
<td>
<input type="radio" name="sex" value="male">男
<input type="radio" name="sex" value="female">女
</td>
</tr>
<tr>
<td>邮箱</td>
<td>
<input type="text" name="email" >
</td>
<td><font color="red">*必须填写</font>
</td>
</tr>
<%--
<tr>
<td>生日</td>
<td>
<input type="text" name="birthday" >
</td>
</tr>
--%>
<tr>
<td>
<input type="reset" value="清空">
</td>
<td>
<input type="submit" value="注册">
</td>
</tr>
</table>
</form>
</body>
</html>
RegisterServlet.java
package me.hyf.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.DateFormat;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import me.hyf.domain.User;
import me.hyf.service.IUserService;
import me.hyf.service.impl.UserServiceImpl;
public class RegisterServlet extends HttpServlet {
/**
* Constructor of the object.
*/
public RegisterServlet() {
super();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
String username = request.getParameter("username");
String password = request.getParameter("password");
String sex = request.getParameter("sex");
String email = request.getParameter("email");
IUserService service = new UserServiceImpl();
User user1=service.loginUser(username, password);
if(user1!=null)
{
System.out.print();
request.getRequestDispatcher("../Login.jsp").forward(request, response);
PrintWriter out = response.getWriter();
String str ="<script>"+
"alert('用户名已经存在‘);"+
"window.location.href='../Register.jsp'"+
"</script>";
out.print(str);
System.out.println(str);
}
else{
User user=new User();
user.setUserName(username);
user.setUserPwd(password);
user.setUserSex(sex);
user.setEmail(email);
service.registerUser(user);
request.getRequestDispatcher("/index.jsp").forward(request, response);
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
public void init() throws ServletException {
// Put your code here
}
}
UserServiceImpl.java
package me.hyf.service.impl;
import me.hyf.dao.IUserDao;
import me.hyf.dao.impl.UserDaoImpl;
import me.hyf.domain.User;
import me.hyf.service.IUserService;
public class UserServiceImpl implements IUserService{
/**
* 注册
* @param user
* @throws UserExistException
*/
IUserDao userdao=new UserDaoImpl();
public void registerUser(User user)
{
userdao.addUser(user);
}
/**
* 登录
* @param userName
* @param userPwd
* @return
*/
public User loginUser(String userName, String userPwd)
{
User user =userdao.logoin(userName,userPwd);
return user;
}
}
------解决思路----------------------
最关键的地方没有贴出来
数据插不进数据库,报什么错误了吗?
断点下,看看userdao.addUser(user) 跟数据库交互时的数据情况
public void registerUser(User user) {
userdao.addUser(user);
}
------解决思路----------------------
你报错是什么啊?
------解决思路----------------------
调试下,看看哪里返回有问题
------解决思路----------------------
你用的是什么数据库,把数据库的代码贴出来看看
------解决思路----------------------
正如楼上所说,你最关键的数据插入到数据库中的那段代码没有贴出来。我觉得你可能是代码在插入数据的时候未做事务处理,即提交事务。
------解决思路----------------------
String sql ="insert into user(username, userpwd, usersex, email) values(?,?,?,?) ";
直接默认的不写user后面的列名时,默认是全部插入的,但你后面的values 却缺id列,应该会报错的
------解决思路----------------------
果然我猜测的没错,你没有做事务处理。你应先在编译预处理SQL语句前设置为手动提交事务(conn.setAutoCommit(false);)。在执行SQL语句插入数据之后,判断如插入成功则提交事务(conn.commit();),若失败则回滚事务(conn.rollback();)。
------解决思路----------------------
最后发现是id的问题,我把id设置成了主键,并且让它自动增加,然后insert into user里面就没有写它就不行,该怎么办啊
调试下,看看哪里返回有问题
主键没设置应该直接就报错了。可以设置一个序列,让序列递增即可。