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.