title: Data truncation for enum in Java and MySQL (java.sql.SQLException: Data truncated for ...)
body: <p>I have a custom class that represents a .csv file structure, which later read in into an arrayList of this class. It has the following tags:</p>
  
  <pre><code>public class InputFile {
  
  public enum Status {
      IN_STOCK,
      OUT_OF_STOCK
  }
  
  private int lineNumber;
  private String sku;
  private Status statusCode;
  private Date orderDate;
  
  public InputFile(int lineNumber, String sku, Status status, Date orderDate) {
      this.lineNumber = lineNumber;
      this.sku = sku;
      statusCode = status;
      setOrderDate(orderDate);
  }
  
  public Status getStatusCode() {
      return statusCode;
  }
  
  public void setStatusCode(Status input) {
      statusCode = input;
  }
  </code></pre>
  
  <p>Here is the simplified .csv file:</p>
  
  <pre><code>LineNumber;SKU;Status;OrderDate
  1;553555254;IN_STOCK;2018-04-21
  2;668470653;IN_STOCK;2018-05-08
  3;899395925;OUT_OF_STOCK;2018-06-06
  </code></pre>
  
  <p>When I read these into the arrayList and put the individual statuses on consolse just to check, it shows correctly (IN_STOCK, OUT_OF_STOCK). However, when I try to insert it into the database it gets truncated:</p>
  
  <blockquote>
    <p>java.sql.SQLException: Data truncated for column 'Status' at row 1</p>
  </blockquote>
  
  <p>Here is the code for database upload:</p>
  
  <pre><code>try {
       String orderItemQuery = "INSERT INTO `order_processing`.`order_item` (`OrderItemId`, `OrderId`, `SalePrice`, `ShippingPrice`, `Sku`, `Status`)" + "VALUES (?, ?, ?, ?, ?, ?)";
  
       PreparedStatement orderItemStmt = conn.prepareStatement(orderItemQuery);
       for (InputFile i : CSVManager.getList())
       {
           orderItemStmt.setInt (1, i.getOrderItemId());
           orderItemStmt.setInt(2,i.getOrderId());
           orderItemStmt.setDouble (3, i.getSalePrice());
           orderItemStmt.setDouble (4, i.getShippingPrice());
           orderItemStmt.setString (5, i.getSku());
           orderItemStmt.setObject(6, InputFile.Status.valueOf(i.getStatusCode().toString()));
           orderItemStmt.execute();
          }
      }
      catch (Exception e)
      {
          ...
      }
  </code></pre>
  
  <p>And here is the .sql for the database table:</p>
  
  <pre><code>create table order_item
  (
    OrderItemId    int         not null
      primary key,
    OrderId        int         not null,
    SalePrice      decimal     not null,
    ShippingPrice  decimal     not null,
    TotalItemPrice decimal AS (ShippingPrice + SalePrice) not null,
    SKU            varchar(25) not null,
    Status         enum ('OUT_OF_STOCK', 'IN_STOCK') not null,
    constraint order_item_order_OrderId_fk
      foreign key (OrderId) references `order` (OrderId)
  );
  </code></pre>
  
  <p>What is missing? Why the enum gets truncated?</p>
  
  <blockquote>
    <p>EDIT: The problem was that Java enum types have a different ordinal numbers then MYSQL has. Java starts counting from 0 while MYSQL starts from 1. To solve the difference, a modification needed in the ordinal list in my enums:</p>
  </blockquote>
  
  <pre><code>    public enum Status {
      OUT_OF_STOCK (1),
      IN_STOCK (2);
  
      private int status;
  
      private Status (final int status) {
          this.status = status;
      }
  
      public int getStatus() {
          return status;
      }
  }
  </code></pre>
  
  <p>Then inserting data into the database was sample:</p>
  
  <pre><code>orderItemStmt.setInt(6, i.getStatusCode().getStatus());
  </code></pre>