Sunday, May 19, 2019

jdbcTemplate

Q- How to insert/update blog data using jdbcTemplate?

public int updateUserImage(String userId, byte[] image, String fileName) {

        System.out.println("insertImage" + jdbcTemplate);

        int statusRes = 0;

        String sql = "";

        String userIdLower = userId.toLowerCase();

        try {

          closeJdbcTemplateConnection(userId);

          TimeUnit.SECONDS.sleep(10);

         

             LobHandler lobHandler = new DefaultLobHandler();

             statusRes = jdbcTemplate.update(

                     "update  USERS set FILE_CONTENT = ?, FILE_NAME = ? WHERE lower(USER_ID) = ?",

                     new Object[] {new SqlLobValue(image, lobHandler),fileName,userIdLower},

                     new int[] {Types.BLOB,Types.VARCHAR,Types.VARCHAR});



        } catch (DataAccessException e) {

            e.printStackTrace();

        } catch (InterruptedException e) {

   // TODO Auto-generated catch block

   e.printStackTrace();

  } finally{

         try {

    jdbcTemplate.getDataSource().getConnection().close();

   } catch (SQLException e) {

    // TODO Auto-generated catch block

    e.printStackTrace();

   }

        }

      

        return statusRes;





    }


Q- How to close connection in java using jdbcTemplate?

public void closeJdbcTemplateConnection(String userId){



               String sql = "";

  try {

         // empty FILE_CONTENT and FILE_NAME before update

         sql = "update CO_USERS set FILE_CONTENT = '', FILE_NAME = '' WHERE lower(USER_ID) = '" + userId.toLowerCase() + "'";

   jdbcTemplate.update(sql);

   

   jdbcTemplate.getDataSource().getConnection().close();

  } catch (SQLException e) {

   // TODO Auto-generated catch block

   e.printStackTrace();

  }

}

No comments:

Post a Comment