try catch问题,数据不执行

StopFoolish 2017-03-25 05:05:40
这个为方法。。数据库students表为:


public String getNewSid() {
Transaction tx = null;
String hql = "";
String sid = null;
try {
Session session = MyHibernateSessionFactory.getSessionFactory().getCurrentSession();
tx = session.beginTransaction();
// 获得当前学生的最大编号
hql = "selesct max(sid) from students";
Query query = session.createQuery(hql);
sid = (String) query.uniqueResult();

if (sid == null || "".equals(sid)) {
// 给一个默认的最大编号
sid = "S0000001";
} else {
String temp = sid.substring(1);// 取后七位数字
int i = Integer.parseInt(temp);// 转成数字
System.out.println("!!!!!!!!2=" + sid);
i++;
temp = String.valueOf(i);
int len = temp.length();
for (int j = 0; j < 7 - len; j++) {
temp = "0" + temp;
}
sid = "S" + temp;
}
tx.commit();
return sid;
} catch (Exception ex) {
ex.printStackTrace();
tx.commit();
return null;
} finally {
if (tx != null) {
tx = null;
}
}
}


test测试为:

@Test
public void testGetNewSid() {
StudentsDAOImpl sdao = new StudentsDAOImpl();
System.out.println(sdao.getNewSid());
}



不输出信的sid。设置了断点发现,不执行tyr catch 语句,不知原因。
...全文
332 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
不执行tyr catch 语句是什么意思,程序卡那了还是结束了
StopFoolish 2017-03-25
  • 打赏
  • 举报
回复
来个大爷,帮帮忙吧。
StopFoolish 2017-03-25
  • 打赏
  • 举报
回复
来个大神给解答一下,很费解的。
StopFoolish 2017-03-25
  • 打赏
  • 举报
回复

package db;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;

public class MyHibernateSessionFactory {
	private static SessionFactory sessionFactory;// 会话工厂属性
	// 构造方法私有化,保证单例模式

	private MyHibernateSessionFactory() {

	}

	public static SessionFactory getSessionFactory() {
		if (sessionFactory == null) {
			// ==null,证明没有初始化过.我们必须初始化
			Configuration config = new Configuration().configure();
			ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(config.getProperties())
					.buildServiceRegistry();
			sessionFactory = config.buildSessionFactory(serviceRegistry);
			return sessionFactory;
		} else {
			return sessionFactory;
		}

	}
}


51,408

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧