The Use of the Length of Varichar 2 Type Fields in Oracle

Keywords: Database Oracle encoding Tomcat

In order to commemorate the 90th anniversary of the founding of the People's Republic of China, this article is dedicated to commemorate our military's prestige!!!


I. Background of the problem

Commodities in the project were released, but they were not saved successfully.

II. Problem Orientation

Preliminary judgment to save to the database when there are errors, look at the log file, because the log file is too large to use grep to search (again explain the strength of grep)



Among them, "Reserve Product Service. update" is the key word to search, catalina.out is a log file, - n shows the number of rows where the search content is located, and - B 2 is the first two rows to display the search content (as well as the first and last rows of - C and - A).

Replace 2 with a larger value to show the following

The problem arises because the length of the insert exceeds the field length limit.

Note: In order to facilitate the analysis of error logs, the contents of the logs are modified as follows. After modification, the logs will be printed into the corresponding log files, instead of all input to the tomcat console.

Before revision
public static void dbLogError(StringBuffer errorMessage, Exception ex) {
		if (dbLog != null) {
			dbLog.error(errorMessage);
		}

		if (isPrintStackTrace && ex != null) {
			ex.printStackTrace();
		}
	}

Revised
public static void dbLogError(StringBuffer errorMessage, Exception ex) {
		if (dbLog != null) {
			dbLog.error(errorMessage);
		}

		if (isPrintStackTrace && ex != null) {
			dbLog.error(ex.getMessage(), ex);       //Print the error stack to the log file
			ex.printStackTrace();
		}
	}


III. Problem Analysis

View the settings of this field in the database

qualitystan              VARCHAR2(254) default '',
That's 254.

The front desk limits 200 words.

if(trim(document.getElementsByName('bean.qualityStan')[0].value).length > 200){
        		alert("The quality standard you entered exceeds 200 words, please re-enter!");
          		document.getElementsByName('bean.qualityStan')[0].focus();
          		return;
        	}

Why is the actual value 314 prompted in the error stack? Take a look at the description of varchar2 length in Oracle

Data Type in oracle is available for reference
http://docs.oracle.com/cd/E11882_01/server.112/e41084/sql_elements001.htm#SQLRF30020


View the encoding format used in the project
SELECT parameter, VALUE FROM nls_database_parameters WHERE parameter IN ('NLS_CHARACTERSET', 'NLS_NCHAR_CHARACTERSET');

NLS_NCHAR_CHARACTERSET AL16UTF16
NLS_CHARACTERSET ZHS16GBK

Explain:
NLS_CHARACTERSET is the character set of the database, and NLS_NCHAR_CHARACTERSET is the national character set (for the introduction of character set in Oracle, refer to http://www.cnblogs.com/KissKnife/archive/2011/11/11/2245410.html).
There are two types of character data in Oracle. VARCHAR2 stores data according to the character set of database. NVARCHAR2 stores data according to the national character set. Similarly, CHAR and NCHAR are the same. One is the database character, the other is the national character set.

NVARCHAR2(N), where N is the number of characters, not bytes. But its maximum length is in bytes, or 4000 bytes.

VARCHAR2(N), where N may refer to the number of characters or bytes. You can specify explicitly when declaring, such as VARCHAR2(10 BYTE) or VARCHAR2(10 CHAR), and when not explicitly specified, it is determined by the parameter NLS_LENGTH_SEMANTICS.


SQL> SELECT parameter, VALUE FROM nls_database_parameters;
PARAMETER                      VALUE
------------------------------ --------------------------------------------------------------------------------
NLS_NCHAR_CHARACTERSET         AL16UTF16
NLS_LANGUAGE                   AMERICAN
NLS_TERRITORY                  AMERICA
NLS_CURRENCY                   $
NLS_ISO_CURRENCY               AMERICA
NLS_NUMERIC_CHARACTERS         .,
NLS_CHARACTERSET               ZHS16GBK
NLS_CALENDAR                   GREGORIAN
NLS_DATE_FORMAT                DD-MON-RR
NLS_DATE_LANGUAGE              AMERICAN
NLS_SORT                       BINARY
NLS_TIME_FORMAT                HH.MI.SSXFF AM
NLS_TIMESTAMP_FORMAT           DD-MON-RR HH.MI.SSXFF AM
NLS_TIME_TZ_FORMAT             HH.MI.SSXFF AM TZR
NLS_TIMESTAMP_TZ_FORMAT        DD-MON-RR HH.MI.SSXFF AM TZR
NLS_DUAL_CURRENCY              $
NLS_COMP                       BINARY
NLS_LENGTH_SEMANTICS           BYTE
NLS_NCHAR_CONV_EXCP            FALSE
NLS_RDBMS_VERSION              11.2.0.4.0




Then the encoding format of the field QUALITYSTAN (varchar2(254)) is ZHS16GBK, and the length limit is 254 bytes. Chinese characters take up 2 bytes, so if it's 200 Chinese characters, it's 400 bytes, and the field length is 254 bytes. So this time is still beyond.


IV. Problem Solution

1) You can modify the length or type of the database field (using varchar2 (254char) or nvarchar2 (254))
2) Modify the front-end restriction, which is more convenient and simple
if(trim(document.getElementsByName('bean.qualityStan')[0].value).length > 120){
        		alert("The quality standard you entered exceeds 120 words, please re-enter!");
          		document.getElementsByName('bean.qualityStan')[0].focus();
          		return;
        	}


Summary: Seemingly simple questions, in fact, need a solid foundation.

Posted by Sephiriz on Sat, 08 Jun 2019 12:49:25 -0700