Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8701711
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T02:28:02+00:00 2026-06-13T02:28:02+00:00

It’s been a while since had to implement this pattern from the beginning so

  • 0

It’s been a while since had to implement this pattern from the beginning so I am a bit confused on what I am missing about using this pattern with spring jdbc. Here is a snippet from my BaseDao:

    protected BaseDao(RowMapper<T> rowMapper, String tableName, JdbcTemplate jdbcTemplate)
   {
      this.rowMapper = rowMapper;
      this.tableName = tableName;
      this.findByIdSql = "SELECT * FROM " + tableName + " WHERE id = ?";
      this.jdbcTemplate = jdbcTemplate;
   }

   public T findById(final Integer id)
   {
      if (id == null)
      {
         return null;
      }

      Object[] params = { id };
      return jdbcTemplate.queryForObject(findByIdSql, params, rowMapper);
   }

   public T save(final T dto)
   {
      if (dto == null)
      {
         return null;
      }

      try
      {
         if (dto.isNew())
         {
            final Integer newId = jdbcTemplate.update("INSERT INTO " + tableName);
         }
      }
      catch (UncategorizedSQLException e)
      {
         logger.error("****************" + e.getMessage(), e);
         throw new RuntimeException("Could not persist: " + e.getMessage());
      }
   }

   public void delete(final T dto)
   {
      if (dto == null)
      {
         final String errorMessage = "Can't delete a null object";
         logger.warn(errorMessage);
         throw new RuntimeException(errorMessage);
      }

      if (!dto.cameFromDatabase())
      {
         throw new RuntimeException("Can't delete an object that didn't come from the database");
      }

      try
      {

      }
      catch (JdbcUpdateAffectedIncorrectNumberOfRowsException e)
      {
         final String message = "The delete failed on " + dto + ".  " + e.getMessage();
         logger.error(message);
         throw new RuntimeException(message, e);

      }
   }

As you can see I got my findById working and able to handle any class thrown at it. The trouble is I don’t recall on how to “generify” the save (handles insert and update) and delete using simply the jdbctemplate. Can this not be accomplished? Does every DAO need to define these two methods itself? I don’t see how jdbc template can flex to writing seperate insert, update, delete statements. Browsing the web I see tons of examples using hibernate or an entitymanager. Is this the route I should take? Or what is the plainly obvious step I am missing here?

I know the save and delete are not filled out but I’m just wondering how to handle the line

final Integer newId = jdbcTemplate.update("INSERT INTO " + tableName);

To handle any DTO thrown at it.

Thank you kindly!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T02:28:03+00:00Added an answer on June 13, 2026 at 2:28 am

    You are in a good path, JdbcTemplate method update can handle insert/delete/update operations

    An example quoted from this article demonstrating an insert operation:

    private DataSource dataSource;
    private JdbcTemplate jdbcTemplate;
    
    public void setDataSource(DataSource dataSource) {
        this.dataSource = dataSource;
    }
    
    public void insert(Customer customer){
    
        String sql = "INSERT INTO CUSTOMER " +
            "(CUST_ID, NAME, AGE) VALUES (?, ?, ?)";
    
        jdbcTemplate = new JdbcTemplate(dataSource);
    
        jdbcTemplate.update(sql, new Object[] { customer.getCustId(),
            customer.getName(),customer.getAge()  
        });
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Does anyone know how can I replace this 2 symbol below from the string
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.