Hibernate3.3.2版本中getSession().connection()已被弃用,替代方法SessionFactoryUtils.getDataSource(getSessionFactory()).getConnection()
来自类org.springframework.orm.hibernate3.SessionFactoryUtils
例子:
publicclass
SqlServiceImpl extendsHibernateDaoSupport {
Connection connection = null;
PreparedStatement statement = null;
public int exeSQL(final String sql) {
return (Integer) super.getHibernateTemplate().execute(
new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
connection = SessionFactoryUtils.getDataSource(
session.getSessionFactory()).getConnection();
// connection=session.connection();
statement = connection.prepareStatement(sql);
return statement.executeUpdate();
}
});
}
}
注:
1、关闭connection、statement连接
2、在applictioncontext.xml文件中配置数据源DataSource
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<!-- 其它配置信息-->
</bean>
本文介绍在Hibernate3.3.2版本中如何使用SessionFactoryUtils获取数据库连接,代替已弃用的getSession().connection()方法,并提供了一个具体的代码示例。
977

被折叠的 条评论
为什么被折叠?



