Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Avoid 'Data truncated for column' in Mysql Database Using java?

Tags:

In my Java Program ..Hibernate Bean class define one column like..

TimtableVO.java

@Column(name="REPEAT_DAYS") private WeekdayType repeatDays;//hear WeekdayType is Enum Class 

And in my service class i'm using this TimetableVO.java bean class as..

This is my Service class:

public void createEvent(TimetableVO timetableVO) {     WeekdayType weekday = null;     for (String day : timetableVO.getTemp().split(",")) {         weekday = WeekdayType.valueOf(day);     }     timetableVO.setRepeatDays(weekday);     userDAO.createEvent(timetableVO); } 

And my DAO class i'm inserting timetableVO object..

public void createEvent(TimetableVO timetableVO) throws DataAccessException {     entityManager.persist(timetableVO); } 

But Exception will come..

Exception: Caused by: java.sql.BatchUpdateException: Data truncated for column 'REPEAT_DAYS' at row 1     at com.mysql.jdbc.PreparedStatement.executeBatchSerially(PreparedStatement.java:2045)     at coHibernate: insert into EC_TIMETABLE (CLASS_DURATION, COURSE_DURATION, COURSE_FEE, END_DATE, REPEAT_COUNT, REPEAT_DAYS, REPEAT_TYPE, SEARCH_KEY, START_DATE, S_ID) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?) m.mysql.jdbc.PreparedStatement.executeBatch(PreparedStatement.java:1468)     at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:70)     at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:268)     ... 65 more Caused by: java.sql.SQLException: Data truncated for column 'REPEAT_DAYS' at row 1     at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1073) 

EDIT: This is my WeekdayType Enum

public enum WeekdayType {     MONDAY(Calendar.MONDAY), TUESDAY(Calendar.TUESDAY), WEDNESDAY(     Calendar.WEDNESDAY), THURSDAY(Calendar.THURSDAY), FRIDAY(     Calendar.FRIDAY), SATURDAY(Calendar.SATURDAY), SUNDAY(     Calendar.SUNDAY);      private int day;      private WeekdayType(int day) {         this.day = day;     }      public int getDay() {         return day;     } } 
like image 243
Java Developer Avatar asked Dec 19 '12 10:12

Java Developer


People also ask

How do I fix data truncated for a column?

You will get this error when you insert data for the value column that exceeds 65K. To solve this problem, you need to change the type that can accept more data, such as MEDIUMTEXT or LONGTEXT , which can accommodate 16MB and 4GB.

What is meant by data truncated for column?

August 23, 2019. When you load data from file to a MySQL table, you might run into this error: Data truncated for column 'column_name' at row # That error means the data is too large for the data type of the MySQL table column.

What is data truncation error in MySQL?

The error can be caused by a data length greater than the maximum size of the field. Using MySQL as an example, a table definition follows: create table person (id int(5), name varchar(5)); The size of the name field is 5.


1 Answers

This can be caused by an ENUM in your table schema that doesn't have all the values you are trying to insert. Something like:

CREATE TABLE IF NOT EXISTS event (     weekday_type ENUM(         'SUNDAY',         'MONDAY',         'TUESDAY',         'WEDNESDAY',         'THURSDAY',         'FRIDAY'     ) NOT NULL ); 

Then when you execute an insert like

INSERT INTO event (weekday_type) VALUES ('SATURDAY'); 

using JDBC you get an exception like: Data truncated for column 'weekday_type' at row 1 because you forgot to include SATURDAY in your table definition. When the insert happens, the value gets truncated to nothing.

like image 102
Stephen Ostermiller Avatar answered Sep 28 '22 01:09

Stephen Ostermiller