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

kz.greetgo.security.session.jdbc.SelectDateOrNull 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.sql.ResultSet;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class SelectDateOrNull implements ConnectionCallback {

  private final String sql;
  private final List sqlParams;

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

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

  @Override
  public Date doInConnection(Connection con) throws Exception {
    try (PreparedStatement ps = con.prepareStatement(sql)) {
      int index = 1;
      for (Object param : sqlParams) {
        ps.setObject(index++, param);
      }
      try (ResultSet rs = ps.executeQuery()) {
        if (!rs.next()) return null;
        Timestamp timestamp = rs.getTimestamp(1);
        if (timestamp == null) return null;
        return new Date(timestamp.getTime());
      }
    }
  }
}