Garbled in Java and Oracle exceptions

Source: Internet
Author: User

/*
* 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 (). ExecuteQuery (
App2db ("select" 123abc who am I?) ' from DUAL '));
Rs.next ();
Print 123abc who am I?
System.out.println (Db2app (rs.getstring (1)));

Abnormal
Because the print ora-00921:unexpected end of SQL command
Results Print ORA-00921:???? Sql????
try {
Conn.createstatement (). ExecuteQuery ("Select 1 from DUAL WHERE");
catch (Exception e) {
System.out.println (E.getmessage ());
}
Because the print Ora-00920:invalid relational operator
Results Print ORA-00920:????????
try {
Conn.createstatement (). ExecuteQuery ("Select 1 from DUAL WHERE 1");
catch (Exception e) {
System.out.println (E.getmessage ());
}
Because the print ORA-00904: "X": Invalid identifier
Results print ORA-00904: "X":??????
try {
Conn.createstatement (). ExecuteQuery ("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: ...");
}
}

Workaround:

You might consider setting the Nls_lang environment variable for the machine where the Oracle client resides (and the machine that your application is running) to circumvent this problem

I did a test on my machine, and the results were as follows:

System environment

Windows 2003 Simplified Chinese version, Oracle 10g

Operation Steps

1. Execute cmd.exe to open a command line window
2. Execute sqlplus username/password into SQL plus command prompt
3. Execute the following SQL statement
SQL code
Select ' x ' from Dua;

Select ' x ' from Dua;
Note that Dua was deliberately written incorrectly (dual less L) in order to get an Oracle exception
You can see the error message as follows:

Reference
Line 1th Error:
ORA-00942: Table or view does not exist

4. Execute quit exit SQL Plus command prompt
5. Execute the following command at the operating system command line prompt
Java code
Set Nls_lang=american_america. We8iso8859p1

Set Nls_lang=american_america. We8iso8859p1
6. Repeat steps 2nd and 3
You can see the error message as follows:

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


As you can see, the original output error message is in Chinese, and the error message is set after the English language. That's 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.