javamysqlapache-commons-dbutils

DBUtils QueryRunner insert on duplicate key update


I'm starting to use the DBUtils lib, but most of my querys are "INSERT ON DUPLICATE KEY UPDATE", I reseached but I haven't found information about how to implement that with the QueryRunner object.

Is there a way?


Solution

  • Not sure what the problem is... here is an example in which you, in effect, UPSERT on but also update the audit_date field with a new date and audit_user with the current logged in user.

    new QueryRunner().insert(connection, "INSERT INTO security\n"
                        + "(user_name,\n"
                        + "domain,\n"
                        + "security_permissions,\n"
                        + "audit_user)\n"
                        + "VALUES\n"
                        + "(?,?,?,?)ON DUPLICATE KEY UPDATE security_permissions= values(security_permissions),audit_user = values(audit_user), audit_date=CURRENT_TIMESTAMP;", resultHandler,
                        securityTableModelRow.getUser_name(),
                        securityTableModelRow.getDomain(),
                        securityTableModelRow.getSecurity_permissions(),
                        Main.security.getCurrentLoggedInUser());