aspose file tools*
The moose likes JDBC and the fly likes What happens when the length of the text exceed the limit of the column VARCHAR Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "What happens when the length of the text exceed the limit of the column VARCHAR" Watch "What happens when the length of the text exceed the limit of the column VARCHAR" New topic
Author

What happens when the length of the text exceed the limit of the column VARCHAR

Tejas Jain
Ranch Hand

Joined: Mar 04, 2008
Posts: 119
I use VARCHAR(4000) for a column in a table in MySQL. 4000 is the max char allowed in MySQL. What will happen if the text length exceeds the 4000? Does the DB (MySQL or Oracle) automatically chop off anything that above 4000, but still allows you to insert or update the text of the first 4000 chars? Or the DB will reject the entire message if it over 4000? Any error messages?

How the length of the text is measured for the non-English languages, the raw length of the encoding protocol (e.g., UTF8)?


"Knowing is not enough, you must apply... Willing is not enough, you must do."
--Bruce Lee
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 29274
    
140

Most databases give you an error. I recommend trying it to be sure on your database.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Martin Vajsar
Sheriff

Joined: Aug 22, 2010
Posts: 3445
    
  47

Don't know how about MySQL, but in Oracle the 4000 size limit counts in bytes. Therefore, if your String in database encoding is represented using some multi-byte characters, it may not fit into a VARCHAR2 though its length in Java is less than 4000 characters.

I don't know how to determine the size of the String in database encoding in a general case. You'd probably have to obtain the database encoding and use it to convert the String into a byte array, then look at the length of the array. It might be easier to just catch the database exception that would be generated by using a String that is too long.

If you do need to store longer strings, you'll need to use a CLOB (maximal size of a CLOB is database dependent, but generally counts in the gigabytes).
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: What happens when the length of the text exceed the limit of the column VARCHAR
 
Similar Threads
clob setting
Sorting Query problem in MySQL
Problem when inserts data
compatibility
mysql LongText mapping in Hibernate