ryekayo
ryekayo

Reputation: 2421

Spring SQL Proper way to use JdbcTemplate.update()

I am following a tutorial found here. And am trying to follow it with an independent project I am working on called the Ticketing System. So i created this method:

public void create(Integer ticketNumber, Date timeOpened, String priorityLevel, String caseOwner) {

    String sql = "insert into user_ticket (ticket_number, opened, priority, case_owner) values (?, ?, ?, ?)";
    jdbcTemplateObject.update(sql, ticketNumber, timeOpened, priorityLevel, caseOwner);

    System.out.println("Created Record Ticket Number = " + ticketNumber + " Time Opened = " + timeOpened + "Priority Level " + 
    priorityLevel + " Case Owner: " + caseOwner);
    return;     
}

But the problem is that when I try to use this line:

jdbcTemplateObject.update(sql, ticketNumber, timeOpened, priorityLevel, caseOwner);

I get an error:

The method update(String, Object[], int[]) in the type JdbcTemplate is not applicable for the arguments (String, Integer, Date, String, String)

I understand that the arguments I have provided may not be the right to use the update() method and I am still learning SpringJDBC. Can someone tell me if there is an alternative way of using this method that can satisfy all the arguments I am providing?

Upvotes: 1

Views: 743

Answers (1)

ninja.coder
ninja.coder

Reputation: 9648

Instead of passing the the variables in update() you should put everything in Object[] and then pass it.

Here is the code snippet:

public void create(Integer ticketNumber, Date timeOpened, String priorityLevel, 
                   String caseOwner) {
    String sql = "insert into user_ticket (ticket_number, opened, priority, case_owner) 
                  values (?, ?, ?, ?)";
    Object[] params = {ticketNumber, timeOpened, priorityLevel, caseOwner};
    jdbcTemplateObject.update(sql, params);

    System.out.println("Created Record Ticket Number = " + ticketNumber + 
                       " Time Opened = " + timeOpened + "Priority Level " + 
                       priorityLevel + " Case Owner: " + caseOwner);
}

Upvotes: 2

Related Questions