Show TOC

Value Checker APILocate this document in the navigation structure

Use

Open SQL for Java imposes limitations on the value range for the different data types. When an application tries to set a value on a PreparedStatement , and the value is out of the valid range, this leads to an SQLException , which is logged.

To avoid such exceptions, Open SQL for Java provides the Value Checker API that allows the application to check the validity of values before setting them.

The Value Checker API is part of the public Open SQL for Java API.

More information: Using Open SQL/JDBC in the Persistence Layer

The following steps show how you can use the Value Checker API:

  1. Create a Checker Factory.

    You create a Checker Factory using the createCheckerFactory method of the com.sap.sql.DatabaseServices class.

  2. Create a ColumnValueChecker .

    Using the Checker Factory, you can create a column value checker that allows you to assess the validity of values for a particular database column.

    There are two options for creating a ColumnValueChecker :

    • By specifying the JDBC type, size and decimals

    • By specifying a column object, which is obtained using the DictionaryReader API

  3. Check the value.

    Using the column value checker, you check the validity of values for the database column the checker is associated with. The column value checker offers the following sets of methods for value checking:

    • approve methods assume that the given values are valid for the associated column. If an invalid value is encountered, the approve methods return a ValueCheckerException , which is not logged.

    • check methods do not return exceptions but ValueCheckerIssue enumerators, which can be used for error classification.

Example

Example 1:

            // Construct a value checker for a given JDBC type.
// Use a check method for error classification.

String value = …

CheckerFactory checkerFactory = DatabaseServices.createCheckerFactory();
ColumnValueChecker columnValueChecker = checkerFactoy.createValueCheckerForJDBCType(Types.VARCHAR, 32, 0, true);
ValueCheckerIssue issue = columnValueChecker.checkStringValue(value);

if (issue = ValueCheckerIssue.OK) {
   // OK.
} else {
   // Handle the error.
}

         

Example 2:

            // Obtain a Column object from the DictionaryReader API.
// Construct a value checker for this Column object.
// Use an approve method to assert validity.

Connection connection = ...
String tableName = ...
String columnName = ...
String value = ...

DictionaryReader ddicReader = DatabaseServices.getDictionaryReader(connection);
Table table = ddicReader.getTable(tableName);
Column column = table.getColumn(columnName);

CheckerFactory checkerFactory = DatabaseServices.createCheckerFactory();
ColumnValueChecker columnValueChecker = checkerFactoy.createValueCheckerFromColumn(column);

try {
   columnValueChecker.approveStringValue(value);
} catch (ValueCheckerException ex) {
    ValueCheckerIssue issue = ex.getIssue();
  // Handle error.
}