Remove boilerplate from db code

后端 未结 5 1092
深忆病人
深忆病人 2021-01-12 09:17

It seems that every time I want to perform a db query, I have to write the following:

Connection conn = null;
Statement stmt = null;
ResultSet rset = null;

         


        
5条回答
  •  佛祖请我去吃肉
    2021-01-12 10:11

    If you already have a DataSource you can use Spring JdbcTemplate for:

    • greatly reduced boilerplate code
    • have a good sql exception hierarchy to handle common database problems with specific runtime exceptions
    • (later with further Spring usage) use declarative transaction management

    If it seems too heavy for the moment you could implement some utility classes and methods for the 'boilerplate part'. Studying the source of JdbcTemplate should help in this case.

提交回复
热议问题