Hibernate 连接SQLServer 2000 插入数据,不报错,但是就是插入不了数据!
首先声明:Hibernate生成数据库表式成功的,也就是说生成数据表是没问题的(驱动包等没有任何问题)
但是就是插入不了数据
DAO类代码:
package com.hgp.dao; import java.util.List; import org.hibernate.Query; import org.hibernate.Session; import com.hgp.model.User; import com.hgp.util.HibernateUtil; public class UserDao { /** * 保存用户 * @param user User对象 */ public void saveUser(User user){ Session session = null; //Session对象 try { //获取Session session = com.hgp.util.HibernateUtil.getSession(); session.beginTransaction(); //开启事物 session.save(user); //持久化user session.getTransaction().commit(); //提交事物 } catch (Exception e) { e.printStackTrace(); //打印异常信息 session.getTransaction().rollback();//回滚事物 }finally{ HibernateUtil.closeSession(); //关闭Session } } /** * 查询所有用户信息 * @return List集合 */ public List<User> findAllUser(){ Session session = null; //Session对象 List<User> list = null; //List集合 try { //获取Session session = HibernateUtil.getSession(); session.beginTransaction(); //开启事物 String hql = "from User"; list = session.createQuery(hql) //创建Query对象 .list(); //获取结果集 session.getTransaction().commit(); //提交事物 } catch (Exception e) { e.printStackTrace(); //打印异常信息 session.getTransaction().rollback();//回滚事物 }finally{ HibernateUtil.closeSession(); //关闭Session } return list; } /** * 通过用户名和密码查询用户 * 用于登录 * @param username 用户名 * @param password 密码 * @return User对象 */ public User findUser(String username, String password){ Session session = null; //Session对象 User user = null; //用户 try { //获取Session session = HibernateUtil.getSession(); session.beginTransaction(); //开启事物 //HQL查询语句 String hql = "from User u where u.username=? and u.password=?"; Query query = session.createQuery(hql) //创建Query对象 .setParameter(0, username)//动态赋值 .setParameter(1, password);//动态赋值 user = (User)query.uniqueResult(); //返回User对象 session.getTransaction().commit(); //提交事物 } catch (Exception e) { e.printStackTrace(); //打印异常信息 session.getTransaction().rollback();//回滚事物 }finally{ HibernateUtil.closeSession(); //关闭Session } return user; } /** * 判断指定用户名的用户是否存在 * @param username 用户名 * @return */ public boolean findUserByName(String username){ Session session = null; //Session对象 boolean exist = false; try { //获取Session session = HibernateUtil.getSession(); session.beginTransaction(); //开启事物 //HQL查询语句 String hql = "from User u where u.username=?"; Query query = session.createQuery(hql) //创建Query对象 .setParameter(0, username);//动态赋值 Object user = query.uniqueResult(); //返回User对象 //如果用户存在exist为true if(user != null){ exist = true; } session.getTransaction().commit(); //提交事物 } catch (Exception e) { e.printStackTrace(); //打印异常信息 session.getTransaction().rollback();//回滚事物 }finally{ HibernateUtil.closeSession(); //关闭Session } return exist; } }
Servlet类代码:
package com.hgp.service; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.hgp.dao.UserDao; import com.hgp.model.Administrator; import com.hgp.model.Guest; import com.hgp.model.User; /** * 用户操作Servlet类 * 通过doPost()方法进行处理 * @author Li Yong Qiang */ public class UserServlet extends HttpServlet { private static final long serialVersionUID = 1380736560965055473L; @Override public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } @Override public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //请求参数 String method = request.getParameter("method"); if(method != null){ //用户注册 if("guestReg".equalsIgnoreCase(method)){ //用户名 String username = request.getParameter("username"); //密码 String password = request.getParameter("password"); //电子邮箱 String email = request.getParameter("email"); //创建UserDao UserDao dao = new UserDao(); //判断用户名是否为null或空的字符串 if(username != null && !username.isEmpty()){ //判断用户名是否存在 if(dao.findUserByName(username)){ //如果用户名已存在,进行行错误处理 request.setAttribute("error", "您注册的用户名已存在!"); System.out.println("您注册的用户名已存在!"); request.getRequestDispatcher("error.jsp").forward(request, response); }else{ //实例化一个User对象 User user = new Guest(); //对user中的属性赋值 user.setUsername(username); user.setPassword(password); user.setEmail(email); //保存user dao.saveUser(user); request.getRequestDispatcher("index.jsp").forward(request, response); } } } //用户登录 else if("userLogin".equalsIgnoreCase(method)){ String username = request.getParameter("username"); String password = request.getParameter("password"); //实例化UserDao UserDao dao = new UserDao(); //根据用户名、密码查询User User user = dao.findUser(username, password); //判断用户是否登录成功 if(user != null){ //判断user是否是管理员对象 if(user instanceof Administrator){ //将管理员对象放入到session中 request.getSession().setAttribute("admin", user); } //将用户对象放入到session中 request.getSession().setAttribute("user", user); request.getRequestDispatcher("index.jsp").forward(request, response); }else{ //登录失败 request.setAttribute("error", "用户名或密码错误 !"); request.getRequestDispatcher("error.jsp").forward(request, response); } } //退出登录 else if("exit".equalsIgnoreCase(method)){ request.getSession().removeAttribute("user"); if(request.getSession().getAttribute("admin") != null){ request.getSession().removeAttribute("admin"); } request.getRequestDispatcher("index.jsp").forward(request, response); } }else{ request.getRequestDispatcher("index.jsp").forward(request, response); } } }
以上代码:
修改方法是:
在session.save(user);后面加上
session.flush();
这样就解决问题了,原因是没有提交成功!
注意:连接SQLserver 2000 的时候,表明不要取成user ,这样的表在SQLServer 2000 里面是没法生成的(SQL2005 和2008 没有尝试过)!
相关推荐
yangkang 2020-11-09
lbyd0 2020-11-17
sushuanglei 2020-11-12
85477104 2020-11-17
KANSYOUKYOU 2020-11-16
wushengyong 2020-10-28
lizhengjava 2020-11-13
星月情缘 2020-11-13
huangxiaoyun00 2020-11-13
luyong0 2020-11-08
腾讯soso团队 2020-11-06
Apsaravod 2020-11-05
PeterChangyb 2020-11-05
gaobudong 2020-11-04
wwwjun 2020-11-02
gyunwh 2020-11-02
EchoYY 2020-10-31
dingyahui 2020-10-30