We have many tool to deal with database, as giant Hibernate , famous Spring JDBC template , Spring Jpa , there are too many that , en , I can't mention all .
at first we use JDBC , haha , just like something belove code, quit a lot code
Connection con= null;
PreparedStatement pStmt=null;
ResultSet rs = null;
try{
con = ods.getConnection();
String sql = "select * from admin";
pStmt=con.prepareStatement(sql);
rs=pStmt.executeQuery();
while(rs.next())
{ }
}
catch(Exception ex) {
try{
con.rollback();
}catch(SQLException sqlex){
sqlex.printStackTrace(System.out);
}
ex.printStackTrace();
}finally{
try{
rs.close();
pStmt.close();
con.close();
}catch(Exception e){e.printStackTrace();}
}
and then Spring JDBC template
String sql = "select * from admin";
jdbcTemplate.query(sql,new RowCallbackHandler() {
public void processRow(ResultSet rs) throws SQLException {
}
} );
Spring Jpa, this example require JDK1.5+
@Repository
@Transactional
public class ArticleColumnDaoImpl implements ArticleColumnDao {
@PersistenceContext
private EntityManager entityManager;
public ArticleColumn getByid(long id) {
return entityManager.find(ArticleColumn.class,id);
}
public List<ArticleColumn> getbylayer(long layer) {
return entityManager.createQuery("select a from ArticleColumn a where a.layer =:layer").setParameter("layer",layer).getResultList();
}
public void addArticlekind(ArticleColumn articlekind) {
entityManager.persist(articlekind);
}
public void deleteArticlekind(long id) {
ArticleColumn articlekind=entityManager.find(ArticleColumn.class,id);
entityManager.remove(articlekind);
}
public void mergeArticlekind(ArticleColumn articlekind) {
entityManager.merge(articlekind);
}
}
this Jpa is quite like Hibernate , but something easier to code.
本文介绍了几种常用的数据库操作工具和技术,包括直接使用JDBC进行数据库访问的方法、利用Spring JDBC模板简化数据库操作的方式,以及采用Spring JPA实现更为简洁高效的数据库交互。
1万+

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



