Spring SQL正确使用JdbcTemplate.update()的方法

时间:2016-04-02 17:59:51

标签: java spring

我正在按照here找到的教程。我正在尝试通过我正在开展的一个名为票务系统的独立项目来跟进它。所以我创建了这个方法:

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;     
}

但问题是,当我尝试使用这一行时:

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

我收到错误:

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

据我所知,我提供的参数可能不是使用update()方法的权利,我仍在学习SpringJDBC。有人可以告诉我是否有另一种方法可以使用这种方法来满足我提供的所有参数?

1 个答案:

答案 0 :(得分:2)

您应该将所有内容放在update()中,然后传递给Object[],而不是传递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); } 中的变量。

以下是代码段:

clfdlg