Defry

博客园 首页 新随笔 联系 订阅 管理

1、获取数据库连接

Connection conn=SessionFactoryUtils.getDataSource(getSessionFactory()).getConnection(); 

2、获取sequence下一个值

Session session = null;

session = this.getSession();
   // 查询下一个sequenceName的值
   sqlBuffer.append("select ").append(sequenceName).append(".nextval as nextval from dual");
   List list = session.createSQLQuery(sqlBuffer.toString()).list();
   if (list.size() == 1) {
    return list.get(0) + "";
   } else {
    // 手动抛出异常
    StringBuffer ex = new StringBuffer();
    ex.append("获取名字为:");
    ex.append(sequenceName);
    ex.append("码异常.");
    ex.append("SQL=");
    ex.append(sqlBuffer.toString());
    throw new Exception(ex.toString());
   }

posted on 2015-06-03 09:44  Defry  阅读(115)  评论(0编辑  收藏  举报