Problem description:

Oracle database character set is a headache, especially some customers use the English character set database, but the client displayed the Chinese character set, due to historical reasons, so the character set cannot be changed. So it can only be changed by the service that queries and inserts the Chinese question (which I find very inelegant, but that’s all I can think of for now).

Solution:

1. Query the character set of the database
select * from nls_database_parameters;//View the database character setCopy the code

NLS_LANGUAGE: SIMPLIFIED CHINESE
NLS_CHARACTERSET: WE8ISO8859P1

You can see that the character set is inconsistent. As a result, garbled characters appear during the query and insertion of web service input parameters.

2. Change the input parameter characters to be consistent with those on the server and insert data analysis.

If in doubt, you can write a simple insert interface that inserts data in different character formats, like the following:

switch (type){
			case 0:
				System.out.println("The default is 0.");
				vName = new String(name.getBytes());
				break;
			case 1:
				System.out.println("1 - utf8.");
				try {
					vName = new String(name.getBytes("utf-8"));
				} catch (UnsupportedEncodingException e) {
					e.printStackTrace();
				}
				break;
			case 2:
				System.out.println("2 - GBK.");
				try {
					vName = new String(name.getBytes("gbk"));
				} catch (UnsupportedEncodingException e) {
					e.printStackTrace();
				}
				break;
			case 3:
				System.out.println("3 - iso - 8859-1:");
				try {
					vName = new String(name.getBytes("iso-8859-1"));
				} catch (UnsupportedEncodingException e) {
					e.printStackTrace();
				}
				break;
			case 4:
				System.out.println("4 - GBK - to - iso8859.");
				try {
					vName = new String(name.getBytes("gbk"),"iso-8859-1");
				} catch (UnsupportedEncodingException e) {
					e.printStackTrace();
				}
				break;
			case 5:
				System.out.println("5 - utf8 - to - iso8859.");
				try {
					vName = new String(name.getBytes("utf-8"), "iso-8859-1");
				} catch (UnsupportedEncodingException e) {
					e.printStackTrace();
				}
				break;
			case 6:
				System.out.println("6 - the default - to - iso8859.");
				try {
					vName = new String(name.getBytes(), "iso-8859-1");
				} catch (UnsupportedEncodingException e) {
					e.printStackTrace();
				}
				break;
			case 7:
				System.out.println("7 - iso8859 - to - GBK.");
				try {
					vName = new String(name.getBytes("iso-8859-1"), "gbk");
				} catch (UnsupportedEncodingException e) {
					e.printStackTrace();
				}
				break;
}
Copy the code
3. Check the result in the database

In this case, it is possible to view the parameters in the current server is still garbled, but switch to the server character set and log in to the database, you can see the correct Chinese.

We can see that Type-6 is inserted in normal Chinese. That’s the corresponding

new String(name.getBytes(), "iso-8859-1");
Copy the code
4. Final treatment
String vName = new String(name.getBytes(), "iso-8859-1"); .//vName For the next query and insert
Copy the code