All Downloads are FREE. Search and download functionalities are using the official Maven repository.

kz.greetgo.security.session.jdbc.Update Maven / Gradle / Ivy

There is a newer version: 0.0.12
Show newest version
package kz.greetgo.security.session.jdbc;

import kz.greetgo.db.ConnectionCallback;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.ArrayList;
import java.util.List;

public class Update implements ConnectionCallback {
  private final String sql;
  private final List sqlParams;

  public Update(String sql, List sqlParams) {
    this.sql = sql;
    this.sqlParams = sqlParams;
  }

  public Update(String sql) {
    this(sql, new ArrayList<>());
  }

  @Override
  public Integer doInConnection(Connection con) throws Exception {
    try (PreparedStatement ps = con.prepareStatement(sql)) {

      int index = 1;
      for (Object param : sqlParams) {
        ps.setObject(index++, param);
      }

      return ps.executeUpdate();
    }
  }
}