Spring与Hibernate Dao中操作数据库的另外方式
2008-10-24 13:03
525 查看
// search Person Object
/**
* @param firstname
* @param lastname
* @return
*/
public List searchPerson(final String firstname, final String lastname) {
return getHibernateTemplate().executeFind(new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
List list = new ArrayList();
String sql = " select pid,firstname,lastname from person where firstname like '"
+ firstname
+ "%' and lastname like '"
+ lastname
+ "%' order by lastname;";
PreparedStatement pstmt = null;
Connection conn = null;
ResultSet rs = null;
try {
conn = session.connection();
pstmt = conn.prepareStatement(sql);
rs = pstmt.executeQuery();
while (rs != null && rs.next()) {
String pid = rs.getString(1);
String firstname = rs.getString(2);
String lastname = rs.getString(3);
Person person = new Person();
person.setPid(pid);
person.setFirstname(firstname);
person.setLastname(lastname);
list.add(person);
}
} catch (Exception e) {
log.error(e);
} finally {
try {
if (rs != null)
rs.close();
} catch (Exception e) {
log.error(e);
} finally {
try {
if (pstmt != null)
pstmt.close();
} catch (Exception e) {
log.error(e);
}
}
}
return list;
}
});
}
/**
* @param firstname
* @param lastname
* @return
*/
public List searchPerson(final String firstname, final String lastname) {
return getHibernateTemplate().executeFind(new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
List list = new ArrayList();
String sql = " select pid,firstname,lastname from person where firstname like '"
+ firstname
+ "%' and lastname like '"
+ lastname
+ "%' order by lastname;";
PreparedStatement pstmt = null;
Connection conn = null;
ResultSet rs = null;
try {
conn = session.connection();
pstmt = conn.prepareStatement(sql);
rs = pstmt.executeQuery();
while (rs != null && rs.next()) {
String pid = rs.getString(1);
String firstname = rs.getString(2);
String lastname = rs.getString(3);
Person person = new Person();
person.setPid(pid);
person.setFirstname(firstname);
person.setLastname(lastname);
list.add(person);
}
} catch (Exception e) {
log.error(e);
} finally {
try {
if (rs != null)
rs.close();
} catch (Exception e) {
log.error(e);
} finally {
try {
if (pstmt != null)
pstmt.close();
} catch (Exception e) {
log.error(e);
}
}
}
return list;
}
});
}
相关文章推荐
- spring,hibernate,dao操作数据库模…
- Spring中使用HibernateDaoSupport来进行数据库的操作
- 为struts+spring+hibernate增加显示数据库操作语句功能
- springBoot+Jpa(hibernate)数据库基本操作
- Spring+SpringMVC+Hibernate整合操作数据库 概述
- Spring框架针对dao层的jdbcTemplate操作crud之delete删除数据库操作 Spring相关Jar包下载
- spring+hibernate架构中Dao访问数据库的几种方法
- 使用Spring对JDBC的Dao的支持类操作数据库
- spring boot 拦截器 注入dao 操作数据库时dao 为null
- 解决mybatis继承SqlSessionDaoSupport方式操作数据库不打印sql日志的问题。
- spring+hibernate架构中Dao访问数据库的几种方法
- 分析总结Spring管理Hibernate中Dao层访问数据库两种方式
- hibernate操作数据库时报错解决方式
- hibernate的dao操作不能提交到数据库问题的解决
- Spring使用DAO完成数据库操作
- 配置DataSource方法(BasicDataSource为例) 和 具体的数据库操作(HibernateDaoSupport、HibernateTemplate和jdbcTemplate)
- SSH之继承HibernateDaoSupport对数据库的操作和不继承HibernateDaoSupport对数据库的操作的方法
- [讨论]有关Spring+Hibernate更新数据库的操作
- Spring整合Hibernate开发 - 使用事务管理器操作数据库 - 主配置文件
- hibernate操作数据库方式总结