Garbled characters in java and Oracle exceptions

Source: Internet
Author: User
* & Nbsp; * Test. java2008-7-30 & nbsp; * importjava. SQL. connection; importjava. SQL. driverManager; importjava. SQL. resultSet; importoracle. jdbc. oracleDriver; publicclassTest {&

/*
* Test. java 2008-7-30
*
*/
Import java. SQL. Connection;
Import java. SQL. DriverManager;
Import java. SQL. ResultSet;

Import oracle. jdbc. OracleDriver;

Public class Test {
Public static void main (String [] args) throws Exception {
DriverManager. registerDriver (new OracleDriver ());

// Normal
Connection conn = getConnection ();
ResultSet rs = conn.createstatement(cmd.exe cuteQuery (
App2db ("SELECT '123abc who am I? 'From dual "));
Rs. next ();
// Print 123abc. Who am I?
System. out. println (db2app (rs. getString (1 )));

// Exception
// Because the print ORA-00921: unexpected end of SQL command
// Result print ORA-00921 :???? SQL ????
Try {
Conn.createstatement(cmd.exe cuteQuery ("SELECT 1 from dual where ");
} Catch (Exception e ){
System. out. println (e. getMessage ());
}
// Because of this print ORA-00920: invalid relational operator
// Result print ORA-00920 :????????
Try {
Conn.createstatement(cmd.exe cuteQuery ("SELECT 1 from dual where 1 ");
} Catch (Exception e ){
System. out. println (e. getMessage ());
}
// Because the print ORA-00904: "X": invalid identifier
// Print the result ORA-00904: "X ":??????
Try {
Conn.createstatement(cmd.exe cuteQuery ("SELECT x FROM x ");
} Catch (Exception e ){
System. out. println (e. getMessage ());
}
}

Private static String app2db (String str) throws Exception {
Return new String (str. getBytes (), "ISO-8859-1 ");
}

Private static String db2app (String str) throws Exception {
Return new String (str. getBytes ("ISO-8859-1 "));
}

Private static Connection getConnection () throws Exception {
Return DriverManager. getConnection ("jdbc: oracle: thin :......");
}
}

Solution:

You can consider setting the NLS_LANG environment variable of the machine where the Oracle client is located (also the machine where your application runs) to bypass this problem.

I did a test on my machine. The results are as follows:

System Environment

Windows 2003 Simplified Chinese version, Oracle 10g

Procedure

1.execute cmd.exe to open a command line window
2. Run sqlplus username/password to enter the SQL Plus command prompt.
3. Execute the following SQL statement
SQL code
Select 'x' from dua;

Select 'x' from dua;
Note that dua was intentionally written incorrectly (dual missing the l) to get an oracle exception.
The error message is as follows:

Reference
Row 3 has an error:
ORA-00942: Table or view does not exist

4. Run quit to exit the SQL Plus command prompt.
5. Run the following command at the command line prompt of the operating system:
Java code
Set nls_lang = AMERICAN_AMERICA.WE8ISO8859P1

Set nls_lang = AMERICAN_AMERICA.WE8ISO8859P1
6. Repeat steps 2nd and 3
The error message is as follows:

Reference
ERROR at line 1:
ORA-00942: table or view does not exist


We can see that the original error message is in Chinese and the error message is in English. This is exactly what you need

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.