`
caiaihuan
  • 浏览: 18738 次
  • 性别: Icon_minigender_2
  • 来自: 上海
最近访客 更多访客>>
社区版块
存档分类
最新评论

how to Deal with data base in java

    博客分类:
  • 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.

 

 

 

分享到:
评论

相关推荐

    Packt.Swift.Functional.Programming.2nd.Edition.2017

    Get familiar with functional and non-functional ways to deal with optionals Make use of functional data structures such as semigroup, monoid, binary search tree, linked list, stack, and lazy list ...

    Swift Functional Programming - Second Edition

    Get familiar with functional and non-functional ways to deal with optionals Make use of functional data structures such as semigroup, monoid, binary search tree, linked list, stack, and lazy list ...

    Google C++ Style Guide(Google C++编程规范)高清PDF

    How can we use a class Foo in a header file without access to its definition? We can declare data members of type Foo* or Foo&. We can declare (but not define) functions with arguments, and/or ...

    Sakemail

    Fixed a minor bug with the boundary.- Change the generator of the message id.- Added the field MessageId and InReplyTo to the TSakMsg component.- Added the field In-Reply-To that is added to the ...

    C# Game Programming Cookbook for Unity 3D - 2014

    3.4 User Data Manager (Dealing with Player Stats Such as Health, Lives, etc.)....37 3.4.1 Script Breakdown..........................................39 4. Recipes: Common Components 41 4.1 Introduction...

    google api php client

    The Google API Client Library enables you to work with Google APIs such as Google+, Drive, or YouTube on your server. These client libraries are officially supported by Google. However, the libraries...

    雷达技术知识

    compared with field measurements to evaluate accuracies and determine how water surface roughness and point density affect LiDAR measurements. Results show that LiDAR derived water surface slopes were...

    《ACE程序员指南》

    "With a large C++ code base, we rely on ACE to enable a cross-platform client-server framework for data quality and data integration. ACE has improved our design and smoothed over OS idiosyncrasies ...

    php.ini-development

    Output buffering is a mechanism for controlling how much output data ; (excluding headers and cookies) PHP should keep internally before pushing that ; data to the client. If your application's ...

    gertt点阵图读取器(opengl)

    * And Optimizing The Base Code, Making It More Flexible! * If You've Found This Code Useful, Please Let Me Know. * Visit My Site At nehe.gamedev.net */ #include &lt;stdio.h&gt;&gt; #include &lt;windows.h&gt; ...

    最土团购商业wap版测试可以

    inflating: zuitu/static/img/faq-how-it-works1.gif inflating: zuitu/static/img/faq-how-it-works2.gif inflating: zuitu/static/img/faq-how-it-works3.gif inflating: zuitu/static/img/how-it-works1.gif ...

Global site tag (gtag.js) - Google Analytics