how to Deal with data base in java

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.

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值