先描述下问题:
我在eclipse中配置好了连接mysql数据库,数据库名:webases,表名:admin;字段:id(auto-increment),name,password;并且通过jsp可以成功插入数据,假设此时id是1000,但用select * from admin;却无法查询出刚添加的数据,手动添加一条数据再select * from admin;查询出来的数据id是10001,这说明从jsp页面向mysql插入数据成功,但是为什么查询不出来呢,迷惑中,恳请各位大侠帮忙,感激不尽!!谢谢

解决方案 »

  1.   

    Transaction t = session.beginTransaction();......t.commit();
      

  2.   

    我在jsp页面中添加一条数据,然后手动添加一条,用select * from admin;的结果是
    +-----+------+----------+
    | id  | name | password |
    +-----+------+----------+
    | 114 | yu   | yu       |
    | 115 | yuyu | yuyu     |
    | 117 | zhi  | zhi      |
    +-----+------+----------+
    3 rows in set (0.00 sec)
    也就是从jsp添加进去的那条数据成功了,因为id 号已经自增到117了,但那条数据去哪了呢,为什么查询不出来呢
      

  3.   

    AWUSOFT ,你好,这应该怎样去解决呢,新手不懂,恳请帮忙
      

  4.   

    如果 你用hibernate添加,需要事务提交.
    如果事务也提交了还不行的话加一行session.flush();//这个尽量不使用吧<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property> 
    <property name="hibernate.cache.use_second_level_cache">false</property> 
    配置文件加这两行关闭二级缓存.还有每次用完了把session.close()一下
      

  5.   

    AWUSOFT,谢谢你,我加上去了还是 不能查询出来,这是我配置连接mysql时自动生成的adminDAO.java类
    package com.hibernate;import java.util.List;
    import org.apache.commons.logging.Log;
    import org.apache.commons.logging.LogFactory;
    import org.hibernate.LockMode;
    import org.hibernate.Query;
    import org.hibernate.criterion.Example;/**
     * Data access object (DAO) for domain model class Admin.
     * @see com.hibernate.Admin
     * @author MyEclipse - Hibernate Tools
     */
    public class AdminDAO extends BaseHibernateDAO {    private static final Log log = LogFactory.getLog(AdminDAO.class); //property constants
    public static final String NAME = "name";
    public static final String PASSWORD = "password";    
        public void save(Admin transientInstance) {
            log.debug("saving Admin instance");
            try {
                getSession().save(transientInstance);
                log.debug("save successful");
            } catch (RuntimeException re) {
                log.error("save failed", re);
                throw re;
            }
        }
        
    public void delete(Admin persistentInstance) {
            log.debug("deleting Admin instance");
            try {
                getSession().delete(persistentInstance);
                log.debug("delete successful");
            } catch (RuntimeException re) {
                log.error("delete failed", re);
                throw re;
            }
        }
        
        public Admin findById( java.lang.Long id) {
            log.debug("getting Admin instance with id: " + id);
            try {
                Admin instance = (Admin) getSession()
                        .get("com.hibernate.Admin", id);
                return instance;
            } catch (RuntimeException re) {
                log.error("get failed", re);
                throw re;
            }
        }
        
        
        public List findByExample(Admin instance) {
            log.debug("finding Admin instance by example");
            try {
                List results = getSession()
                        .createCriteria("com.hibernate.Admin")
                        .add(Example.create(instance))
                .list();
                log.debug("find by example successful, result size: " + results.size());
                return results;
            } catch (RuntimeException re) {
                log.error("find by example failed", re);
                throw re;
            }
        }    
        
        public List findByProperty(String propertyName, Object value) {
          log.debug("finding Admin instance with property: " + propertyName
                + ", value: " + value);
          try {
             String queryString = "from Admin as model where model." 
              + propertyName + "= ?";
             Query queryObject = getSession().createQuery(queryString);
     queryObject.setParameter(0, value);
     return queryObject.list();
          } catch (RuntimeException re) {
             log.error("find by property name failed", re);
             throw re;
          }
    } public List findByName(Object name) {
    return findByProperty(NAME, name);
    }

    public List findByPassword(Object password) {
    return findByProperty(PASSWORD, password);
    }

        public Admin merge(Admin detachedInstance) {
            log.debug("merging Admin instance");
            try {
                Admin result = (Admin) getSession()
                        .merge(detachedInstance);
                log.debug("merge successful");
                return result;
            } catch (RuntimeException re) {
                log.error("merge failed", re);
                throw re;
            }
        }    public void attachDirty(Admin instance) {
            log.debug("attaching dirty Admin instance");
            try {
                getSession().saveOrUpdate(instance);
                log.debug("attach successful");
            } catch (RuntimeException re) {
                log.error("attach failed", re);
                throw re;
            }
        }
        
        public void attachClean(Admin instance) {
            log.debug("attaching clean Admin instance");
            try {
                getSession().lock(instance, LockMode.NONE);
                log.debug("attach successful");
            } catch (RuntimeException re) {
                log.error("attach failed", re);
                throw re;
            }
        }
    }
    再帮忙看下好吗,谢谢!!!
      

  6.   

    我用的是jdbc驱动,添加数据时调用adminDAO.java中的save()方法
    这是addminAction.java
    public ActionForward execute(ActionMapping mapping, ActionForm form,
    HttpServletRequest request, HttpServletResponse response) {
    AddAdminForm addAdminForm = (AddAdminForm) form;
      
      // TODO Add a new admin
      Admin admin = new Admin();
      admin.setName(addAdminForm.getName ());
      admin.setPassword(addAdminForm.getPassword ());
      AdminDAO adminDAO = new AdminDAO ();
      adminDAO.save(admin);
      
      return mapping.findForward("success");
     }
    }
      

  7.   

    谢谢大家,问题解决了,是事务的问题,eclipse自动生成的代码中没有加入事务处理的代码,但还有点不明白,没用事务是不是不能提交数据到数据库了,但是数据库中id又会被占用。
      

  8.   

    public void save(Admin transientInstance) { 
            log.debug("saving Admin instance"); 
            try { 
                getSession().save(transientInstance); 
                log.debug("save successful"); 
            } catch (RuntimeException re) { 
                log.error("save failed", re); 
                throw re; 
            } 
        } 
    没有使用事务.....................