spring+hibernate 插入数据时控制台打印出sql语句,但是数据库中并没有数据解决方法

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/yubin_yubin/article/details/8916381

原因可能是spring对于proxool连接配置不支持自动提交,

必须在hibernate.cfg.xml中添加一个自动提交的配置:

<property name="connection.autocommit">true </property> 
这样就能够写入数据库了

展开阅读全文

java中sql语句执行有错误,但是印出语句后在sql管理中没有问题

08-01

java中sql语句执行有错误,但是打印出语句后在sql管理中没有问题rnjava中代码如下:rnString sqlview="if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[员工考勤卡号-按最新启 用日期]') and OBJECTPROPERTY(id, N'IsView') = 1)"+rn "drop view [dbo].[员工考勤卡号-按最新启用日期]" +rn "\n"+"GO"+"\n"+rn "SET QUOTED_IDENTIFIER ON"+ rn "\n"+"GO"+"\n"+rn "SET ANSI_NULLS ON"+ rn "\n"+"GO"+"\n"+rn "CREATE VIEW dbo.[员工考勤卡号-按最新启用日期]"+"\n"+rn " AS "+"\n"+rn "SELECT DISTINCT SMT_PersonnelID, SMT_CardSerial, SMT_DateTime, SMT_State"+"\n"+rn "FROM dbo.Smart_Card ta"+"\n"+rn "WHERE (SMT_DateTime ="+"\n"+rn "(SELECT MAX(tb.SMT_DateTime) AS Expr1"+"\n"+rn "FROM dbo.Smart_Card AS tb"+"\n"+rn "WHERE (tb.SMT_PersonnelID = ta.SMT_PersonnelID)))"+rn "\n"+"GO"+"\n"+rn "SET QUOTED_IDENTIFIER OFF"+ rn "\n"+"GO"+"\n"+rn "SET ANSI_NULLS ON"+ rn "\n"+"GO"+"\n"+rn ;rn打印出的语句为:if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[员工考勤卡号-按最新启用日期]') and OBJECTPROPERTY(id, N'IsView') = 1)drop view [dbo].[员工考勤卡号-按最新启用日期]rnGOrnSET QUOTED_IDENTIFIER ONrnGOrnSET ANSI_NULLS ONrnGOrnCREATE VIEW dbo.[员工考勤卡号-按最新启用日期]rn AS rnSELECT DISTINCT SMT_PersonnelID, SMT_CardSerial, SMT_DateTime, SMT_StaternFROM dbo.Smart_Card tarnWHERE (SMT_DateTime =rn(SELECT MAX(tb.SMT_DateTime) AS Expr1rnFROM dbo.Smart_Card AS tbrnWHERE (tb.SMT_PersonnelID = ta.SMT_PersonnelID)))rnGOrnSET QUOTED_IDENTIFIER OFFrnGOrnSET ANSI_NULLS ONrnGOrnrn这个打印出来的语句直接复制到sqlserver management中是可以执行的 没有问题rn但是eclipse中直接报错:com.microsoft.sqlserver.jdbc.SQLServerException: 'GO' 附近有语法错误。rnrn请大家分析下。。。。。。。。。 论坛

hibernate能打印出insert语句但是数据库中没有更新数据啊!

07-14

hibernate能打印出insert语句,但是数据库中没有更新数据啊!能分析一下具体什么原因么?不行的话我在贴代码!!谢谢各位了~!r r 补充一下:我是在一个JSP页面里 两次调用HIBERNATE.UTIL的SAVE方法来实现数据更新的。前一条语句可以更新数据库,但是后一条可以打印出insert语句但是数据库没有更新。我是,请大家关照! 问题补充 来高人啊! 问题补充: 兄弟们,请看代码r SupplierDao.javar //////////////////////////r package com.supply.dao;r r import java.util.List;r import org.hibernate.HibernateException;r import com.supply.dao.hibernate.HibernateUtil;r import org.hibernate.Session;r import com.supply.domain.Supplier;r r public class SupplierDaor r r public boolean saveSupplier(Supplier supplier)r r tryr r Session savesu=HibernateUtil.currentSession();//取得SESSIONr HibernateUtil.beginTransaction(); //开始事务r savesu.saveOrUpdate(supplier); //保存r savesu.flush();r HibernateUtil.commitTrasaction(); //提交事务r HibernateUtil.closeSession(); //关闭SESSIONr return true;r r catch(HibernateException e)r r System.out.print(e); r r return false;r r r r //////////////////////////////////////////r SuhistoryDao.javar ////////////////////////////////////r package com.supply.dao;r r import java.util.List;r import org.hibernate.HibernateException;r import com.supply.dao.hibernate.HibernateUtil;r import org.hibernate.Session;r import com.supply.domain.Supplier;r import com.supply.domain.Suhistory;;r r public class SuhistoryDao r r public boolean addhistory(Suhistory suhistory)r r tryr r Session addsu=HibernateUtil.currentSession();//取得SESSIONr HibernateUtil.beginTransaction(); //开始事务r addsu.saveOrUpdate(suhistory); //保存r addsu.flush();r HibernateUtil.commitTrasaction(); //提交事务r HibernateUtil.closeSession(); //关闭SESSIONr return true;r r catch(HibernateException e)r r e.printStackTrace();r System.out.print(e);r return false;r r r r r r ////////////////////////r HibernateUtil.javar ///////////////////////r package com.supply.dao.hibernate;r r import org.hibernate.HibernateException;r import org.hibernate.Session;r import org.hibernate.SessionFactory;r import org.hibernate.Transaction;r import org.hibernate.cfg.Configuration;r r public class HibernateUtilr r private static final SessionFactory sessionFactory;r r staticr r tryr r sessionFactory=new Configuration().configure().buildSessionFactory();r r r catch(Throwable ex)r r ex.printStackTrace();r System.out.println("Initial SessionFactory creation failed");r throw new ExceptionInInitializerError(ex);r r r public static final ThreadLocal tLocalsess=new ThreadLocal();r r public static final ThreadLocal tLocaltx=new ThreadLocal();r r public static Session currentSession()r r Session session=(Session)tLocalsess.get();r tryr r //如果第一次使用,则创建一个新的SESSIONr if(session==null||!session.isOpen())r r //如果不是第一次使用,则打开一个SESSIONr session=sessionFactory.openSession();r tLocalsess.set(session);r System.out.println("Session creation ok ");r r r catch(HibernateException e)r r e.printStackTrace();r r return session;r r r public static void closeSession()r r Session session=(Session)tLocalsess.get();r tLocalsess.set(null);r tryr r if(session!=null&&session.isOpen())r r session.close();r System.out.println("session close ok ");r r r catch(HibernateException e)r r System.out.println("session close error! ");r r r r r public static void beginTransaction()r r Transaction tx=(Transaction)tLocaltx.get();r tryr r if(tx==null)r r tx=currentSession().beginTransaction();r tLocaltx.set(tx);r System.out.println("Transaction ok ");r r r catch(HibernateException e)r r System.out.println("Transaction error! ");r r r r public static void commitTrasaction()r r Transaction tx=(Transaction)tLocaltx.get();r tryr r if(tx!=null&&!tx.wasCommitted()&&!tx.wasRolledBack())r r tx.commit();r System.out.println("commit ok ");r r r catch(HibernateException e)r r System.out.println("commit error ");r r r r public static void rollbackTransaction()r r Transaction tx=(Transaction)tLocaltx.get();r tryr r tLocaltx.set(null);r if(tx!=null&&!tx.wasCommitted()&&!tx.wasRolledBack())r r tx.rollback();r r r catch(HibernateException e)r r r r r private static Session openSession() throws HibernateExceptionr r return getSessionFactory().openSession();r r r private static SessionFactory getSessionFactory() throws HibernateExceptionr r return sessionFactory;r r r //////////////////////r gysZrSave.jspr ////////////////////////r <%@ page contentType="text/html; charset=utf-8" language="java"%>r <%@ page import="java.util.*"%>r <%@ page import="java.util.Date"%>r <%@ page import="java.sql.*"%>r <%@ page import="com.supply.dao.hibernate.*"%>r <%@ page import="com.supply.dao.hibernate.HibernateUtil"%>r <%@ page import="com.supply.domain.Supplier"%>r <%@ page import="com.supply.dao.*"%>r <%@ page import="com.supply.dao.SupplierDao"%>r <%@ page import="com.supply.common.FormatUtil"%>r <%@ page import="com.supply.dao.SuhistoryDao"%>r <%@ page import="com.supply.domain.Suhistory"%>r r <%/////////////////////////////////////////////////%>r <%@page import="org.hibernate.*" %>r <%@page import="org.hibernate.cfg.*" %>r <%@page import="com.supply.domain.Supplier"%>r <%//////////////////////////////////////////////////%>r r <% r r r r //设置参数传递编码r request.setCharacterEncoding("utf-8");r r String gysbm=request.getParameter("gysbm");r String gyscs=request.getParameter("gyscs");r String gysmc=request.getParameter("gysmc");r float zczj=new Float(request.getParameter("zczj")).floatValue();r String zcdz=request.getParameter("zcdz");r String frdb=request.getParameter("frdb");r String qyxz=request.getParameter("qyxz");r String sfzhm=request.getParameter("sfzhm");r String qylx=request.getParameter("qylx");r String zzjg=request.getParameter("zzjg");r String swdjh=request.getParameter("swdjh");r String gskhh=request.getParameter("gskhh");r String yhzh=request.getParameter("yhzh");r String lxdz=request.getParameter("lxdz");r String lxr=request.getParameter("lxr");r String dhhm=request.getParameter("dhhm");r //重复String gsdz=request.getParameter("gsdz");r String gsyb=request.getParameter("gsyb");r String gyszt=request.getParameter("gyszt");r String gssf=request.getParameter("gssf");r String zrsj=request.getParameter("zrsj");r r //数据初始化 r SupplierDao supplierDao=new SupplierDao();r Supplier sup=new Supplier();r //保存数据r sup.setGysbm(gysbm);r sup.setGyscs(gyscs);r sup.setGysmc(gysmc);r sup.setZcdz(zcdz);r sup.setZczj(zczj);r sup.setFrdb(frdb);r sup.setSfzhm(sfzhm);r sup.setQyxz(qyxz);r sup.setQylx(qylx);r sup.setZzjg(zzjg);r sup.setSwdjh(swdjh);r sup.setGskhh(gskhh);r sup.setYhzh(yhzh);r sup.setLxdz(lxdz);r sup.setLxr(lxr);r sup.setDhhm(dhhm);r //sup.setGsdz(gsdz);r sup.setGsyb(gsyb);r sup.setGyszt(gyszt);r sup.setGssf(gssf);r sup.setZrsj(zrsj);r r if(supplierDao.saveSupplier(sup))r r out.print(" ");r r r r SuhistoryDao suhistoryDao=new SuhistoryDao();r Suhistory suhistory=new Suhistory();r r suhistory.setGysbm("aaaaaaaa");r suhistory.setSjsj("2002-1-1");r suhistory.setSjmc("3");r suhistory.setSjyy("32");r suhistory.setSphy("32");r suhistory.setBz("32");r r if(suhistoryDao.addhistory(suhistory))r r out.println("供应商成功进入预选状态!"); r r r r %>r r 问题补充: 刚跟踪了一下。事件已经写了commit代码,但是好像没有执行。不知道其中原因。 问题补充: 我 已经修改为save(),但是还是没法更新数据库。r r Transaction ok r Hibernate: r insert r intor supplierr (gysmc, zcdz, zczj, frdb, sfzhm, qyxz, qylx, zzjg, swdjh, gskhh, yhzh, lxdz, lxr, dhhm, gsdz, gsyb, gyszt, gssf, zrsj, yxsj, zzsj, qtsj, gysbm, gyscs) r valuesr (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)r commit ok r session close ok r Session creation ok r Hibernate: r selectr max(id) r fromr suhistoryr Hibernate: r insert r intor suhistoryr (gysbm, sjsj, sjmc, sjyy, sphy, bz, id) r valuesr (?, ?, ?, ?, ?, ?, ?)r session close ok 问题补充: 从上面可以看出,COMMIT确实没有提交,就直接关闭SESSION了,但是我不知道为什么会这样啊。r 谢谢各位的耐心解答。不甚感谢!!! 问题补充: 刚看到 第二次不但 事件没提交 连Transaction 都没有执行!天啦。已经困扰我两天了! 问题补充: saveOrUpdate方法跟主键有关 r r 你的pojo类对应的xml文件里指定了主键即标签 r r hibernate执行saveOrUpdate方法时 看你的主键 没有的话 他就什么都不执行了 r r saveOrUpdate(obj)你的保证obj里你set了你指定成主键那个属性。 r r 如果缓存里有了这个主键他就update了 没有就insert了r r r 谢谢大哥,我已经将saveOrUpdate()修改为 save()了 执行后成上面的结果! 问答

求助!控制台印出信息了,但是页面没有显示,为什么?

10-22

public class LogManageView rn /**rn * 登录日志首页rn * @param locrn * @param viewFactoryrn * @returnrn */rn public static View createLoginView(Locale loc,rn ViewFactory viewFactory,rn List dataList,rn HttpServletRequest request)rn BaseView baseView =new BaseView();rn VerticalPanel verticalPanel = new VerticalPanel("verticalPanel","25,25,*,*");rnrn //标题 rn HorizontalPanel hp=new HorizontalPanel("hp", "270","m-top bg-lower", null, 0, 0);rn HtmlPanel htlp=new HtmlPanel("htlp", "登录日志信息");rn hp.addSubPanel(htlp);rn //添加按钮rn ButtonBarPanel bbp=new ButtonBarPanel("");rn bbp.addButton(new ClickButton("img/b/srch.gif", "查询", "VM(this).cmd('search')"));rn rn //添加按钮路径rn Command search=new SubmitCommand("search", "logIndex.sp?act=loginSearch", null, false);rn baseView.addCommand(search);rn //查询条件rn HorizontalPanel horizon=new HorizontalPanel("hpsrhcop", "260,260,*");rn rn FormPanel form=new FormPanel("form");rn form.setScroll(Scroll.hidden);rn Text login=new Text("login_name", "登录名", "", -1);rn login.setWidth("150");rn form.add(login);rn horizon.addSubPanel(form);rn FormPanel panel=new FormPanel("panel");rn panel.setScroll(Scroll.hidden);rn DatePicker date=new DatePicker("login_time", "登录时间", null,DatePicker.DATE);rn panel.add(date);rn horizon.addSubPanel(panel,bbp);rn //列表rn GridPanel grid = new GridPanel("grid");rn int cp = 1;rn setData(grid, dataList, cp);rn grid.addHiddenColumn(0, "loginId");rn grid.addSelectitem("loginId", "50");rn grid.addTextColumn(1, "number", "序号", "50", "number");rn grid.addTextColumn(2, "loginName", "用户名", "150", "loginName");rn grid.addTextColumn(3, "loginTime", "登录时间", "100", "loginTime");rn grid.addTextColumn(4, "loginIp", "登录IP", "100", "loginIp");rn grid.addTextColumn(5, "remark", "备注", "100", "remark");rn rn // 翻页栏rn if (dataList != null) rn String currentPage = request.getParameter("cp");rn PagePanel page=new PagePanel("page");rn if (currentPage!= null && currentPage.trim().length() > 0) rn cp = Integer.parseInt(currentPage);rn rn verticalPanel.addSubPanel(hp,horizon,grid,page);rn baseView.addSubPanel(verticalPanel);rn rn return baseView;rn rn // 列表面板中加入数据rn public static void setData(GridPanel grid, List dataList, int cp)rn List list = new ArrayList(rn Utils.notEmpty(dataList) ? dataList.size() : 0);rn if (Utils.notEmpty(dataList)) rn for (int i = 0; i < dataList.size(); i++) rn TLoginLog loginLog = (TLoginLog) dataList.get(i);rn if (loginLog == null) continue;rn String userhql = "from TUser t where t.userCode="+loginLog.getUserCode();rn TUser user = (TUser)CommonMethods.getDAO().queryAsAnObject(userhql);rn Object[] data = new Object[6];rn data[0] = loginLog.getLoginId();rn data[1] = i+1;rn data[2] = user.getLoginName();rn data[3] = loginLog.getLoginTime();rn data[4] = loginLog.getLoginIp();rn data[5] = loginLog.getRemark();rn list.add(data);rn rn rn grid.setData(list);rn rn rnrnrn/**rn * 查询日志信息 rn * @param requestrn * @param responsern * @returnrn */ rn public void loginSearch(HttpServletRequest request,HttpServletResponse response)throws Exceptionrn Locale loc=null;rn ViewFactory viewFactory = ViewFactory.getViewFactory(FrameworkHelperrn .getSkin(request));rn CommandGroup cg = new CommandGroup(null);rn //获取页面数据rn String login_name = request.getParameter("login_name");rn String login_time = request.getParameter("login_time");rn rn // 两个文本框都为空时rn if((login_name==null||"".equals(login_name))rn &&(login_time==null||"".equals(login_time)))rn Command dialog = new AlertCommand("alt", "请输入你要查询的内容。",rn "img/p/alert-info.gif", DialogPosition.middle, 20);rn cg.add(dialog);rn outPutXML(response, cg);rn rn //登录人姓名为空,登录时间不为空时rn if((login_name==null||"".equals(login_name))rn &&(!"".equals(login_time)))rn DateFormat dft1 = new SimpleDateFormat("yyyy-MM-dd");rn Date date1 = new Date();rn date1 = dft1.parse(login_time);rn List loginlog2 = loginLogdao.queryByTime(date1);rn if(loginlog2.size()==0)rn System.out.println("*************对不起!该时间段没有登录信息*************");rn Command dialog = new AlertCommand("alt", "对不起!该时间段没有登录信息。",rn "img/p/alert-info.gif", DialogPosition.middle, 20);rn cg.add(dialog);rn outPutXML(response, cg);rn elsern // cg.add(JSCmdLib.reload("rs",LogManageView.createLoginView(loc, viewFactory, loginlog2, request) ));rn System.out.println("*************登录人姓名为空,登录时间不为空时*************");rn // cg.add(JSCmdLib.reload("rs","vm:|logIndex.sp?act=loginlogIndex"));rn outPutXML(response,LogManageView.createLoginView(loc, viewFactory, loginlog2, request));rn rn rn //登录姓名不为空,登录时间为空时rn if(!"".equals(login_name)rn &&(login_time==null||"".equals(login_time)))rn //查询用户表rn String userhql = "from TUser c where c.loginName='"+login_name+"'";rn TUser user = (TUser)CommonMethods.getDAO().queryAsAnObject(userhql);rn System.out.println(user);rn if(user==null||"".equals(user))rn Command dialog = new AlertCommand("alt", "对不起!您输入的登录名不存在。",rn "img/p/alert-info.gif", DialogPosition.middle, 20);rn cg.add(dialog);rn outPutXML(response, cg);rn elsern String loghql = "from TLoginLog t where t.userCode="+user.getUserCode();rn cg.add(JSCmdLib.reload("rsh1", "vm:|logIndex.sp?act=loginlogIndex&method=search&&hql="+loghql));rn outPutXML(response, cg);rn rn rn // 两个文本框都不为空时rn if(!"".equals(login_name)&&!"".equals(login_time))rn //查询用户表rn String userhql = "from TUser c where c.loginName='"+login_name+"'";rn TUser user = (TUser)CommonMethods.getDAO().queryAsAnObject(userhql);rn String loghql = "from TLoginLog t join TUser u on t.userCode=u.userCode where t.userCode="+user.getUserCode()+" and t.loginTime='"+login_time+"'";rn System.out.println(loghql);rn List dataList = CommonMethods.getDAO().getQueryList(loghql);rn System.out.println(dataList.size());rn outPutXML(response,LogManageView.createLoginView(loc, viewFactory, dataList, request));rn rn rn 配置文件已经配置好了,相关的对象也注入进去了,以登录名查询的时候,页面显示是正常的,但是一旦以时间查询的话,就是只有页面,没有数据,感觉想是没有刷新的样子,可是控制台已经打印出了查询的内容了,这是为什么啊,求大神们指教 论坛

没有更多推荐了,返回首页