Class ArrayConverter

java.lang.Object
de.chojo.sqlutil.conversion.ArrayConverter

public final class ArrayConverter extends Object
Class to convert sql arrays to java arrays or collections and vice versa.
  • Method Details

    • toArray

      public static <T> T[] toArray(Array array) throws SQLException
      Convert a SQL Array into a java array of the requested return type.
      Type Parameters:
      T - type of required array
      Parameters:
      array - array
      Returns:
      array with the requested type
      Throws:
      SQLException - if an error occurs while attempting to access the array
      SQLFeatureNotSupportedException - if the JDBC driver does not support this method
    • toArray

      @Nullable public static <T> @Nullable T[] toArray(ResultSet resultSet, String column) throws SQLException
      Converts an array of a result set collumn to an java array
      Type Parameters:
      T - type of result
      Parameters:
      resultSet - result set
      column - column name
      Returns:
      new array
      Throws:
      SQLException - if the columnLabel is not valid; if a database access error occurs or this method is called on a closed result set
      SQLFeatureNotSupportedException - if the JDBC driver does not support this method
    • toArray

      @Nullable @Contract("_,_,!null -> !null") public static <T> @Nullable T[] toArray(@NotNull @NotNull ResultSet resultSet, @NotNull @NotNull String column, @Nullable @Nullable T[] def) throws SQLException
      Converts a column of a result set to an java array.
      Type Parameters:
      T - type of result
      Parameters:
      resultSet - result set
      column - name of column
      def - default value
      Returns:
      array of requested type
      Throws:
      SQLException - if the columnLabel is not valid; if a database access error occurs or this method is called on a closed result set
      SQLFeatureNotSupportedException - if the JDBC driver does not support this method
    • toCollection

      public static <T> Collection<T> toCollection(Array array) throws SQLException
      Convert a SQL Array into a java array of the requested return type.
      Type Parameters:
      T - type of required array
      Parameters:
      array - array
      Returns:
      array with the requested type
      Throws:
      SQLException - if an error occurs while attempting to access the array
      SQLFeatureNotSupportedException - if the JDBC driver does not support this method
    • toCollection

      public static <T extends Collection<T>> T toCollection(ResultSet resultSet, String column, Supplier<T> supplier) throws SQLException
      Convert a SQL Array into a java array of the requested return type.
      Type Parameters:
      T - type of result
      Parameters:
      resultSet - result set
      column - name of column
      supplier - supplier of the collection
      Returns:
      array with the requested type
      Throws:
      SQLException - if an error occurs while attempting to access the array
      SQLFeatureNotSupportedException - if the JDBC driver does not support this method
    • toSet

      public static <T> Set<T> toSet(ResultSet resultSet, String column) throws SQLException
      Converts a column of a result set to a set.
      Type Parameters:
      T - type of result
      Parameters:
      resultSet - result set
      column - name of column
      Returns:
      array as set
      Throws:
      SQLException - if a database error occurs, the JDBC type is not appropriate for the typeName and the conversion is not supported, the typeName is null or this method is called on a closed connection.
      SQLFeatureNotSupportedException - if the JDBC driver does not support this data type
    • toList

      public static <T> List<T> toList(ResultSet resultSet, String column) throws SQLException
      Converts a column of a result set to a list.
      Type Parameters:
      T - type of list
      Parameters:
      resultSet - result set
      column - column name
      Returns:
      new list
      Throws:
      SQLException - if a database error occurs, the JDBC type is not appropriate for the typeName and the conversion is not supported, the typeName is null or this method is called on a closed connection.
      SQLFeatureNotSupportedException - if the JDBC driver does not support this data type
    • toSqlArray

      public static Array toSqlArray(Connection conn, SQLType type, Collection<?> collection) throws SQLException
      Convert a java collection to an sql array of the required type.
      Parameters:
      conn - connection
      type - type of array
      collection - collection
      Returns:
      sql array
      Throws:
      SQLException - if a database error occurs, the JDBC type is not appropriate for the typeName and the conversion is not supported, the typeName is null or this method is called on a closed connection.
      SQLFeatureNotSupportedException - if the JDBC driver does not support this data type
    • toSqlArray

      public static Array toSqlArray(Connection conn, SQLType type, Object[] array) throws SQLException
      Convert a java array to an sql array of the required type.
      Parameters:
      conn - connection
      type - type of array
      array - array
      Returns:
      sql array
      Throws:
      SQLException - if a database error occurs, the JDBC type is not appropriate for the typeName and the conversion is not supported, the typeName is null or this method is called on a closed connection.
      SQLFeatureNotSupportedException - if the JDBC driver does not support this data type