Oracle9i SQLJ Developer's Guide and Reference Release 1 (9.0.1) Part Number A90212-01 |
|
This chapter discusses internal operations and functionality of the Oracle SQLJ translator and runtime.
The following topics are covered:
The following topics summarize the operations executed by the SQLJ translator during a translation:
In this first phase of SQLJ translation, a SQLJ parser and a Java parser are used to process all the source code and check syntax.
As the SQLJ translator parses the .sqlj
file, it invokes a Java parser to check the syntax of Java statements and a SQLJ parser to check the syntax of SQLJ constructs (anything preceded by #sql
). The SQLJ parser also invokes the Java parser to check the syntax of Java host variables and expressions within SQLJ executable statements.
The SQLJ parser checks the grammar of SQLJ constructs according to the SQLJ language specification. It does not check the grammar of the embedded SQL operations, however. SQL syntax is not checked until the semantics-checking step.
This syntax-check will discover errors such as missing semi-colons, mismatched curly braces, and obvious type mismatches (such as multiplying a number by a string).
If the parsers discover any syntax errors or type mismatches during this phase, then the translation is aborted, and the errors are reported to the user.
Once the SQLJ application source code is verified as syntactically correct, the translator enters into the semantics-checking phase and invokes a semantics-checker, according to user option settings. The semantics-checker verifies the validity of Java types in SQL operations (result expressions or host expressions) and optionally connects to a database to check compatibility between Java types and SQL types.
The -user
option specifies online checking, and the -password
and -url
options finish specifying the database connection if the password and URL were not specified in the -user
option. The -offline
or -online
option specifies which checker to use. The default is a checker front end called OracleChecker
, which chooses the most appropriate checker, according to whether you have enabled online checking and which JDBC driver you are using. For more information, see "Connection Options" and "Semantics-Checking Options".
Note: Semantics-checking can also be performed on a profile that was produced during a previous execution of the SQLJ translator. See "SQLCheckerCustomizer for Profile Semantics-Checking". |
The following two tasks are always performed during semantics-checking, whether offline or online:
This includes examining the SQLJ source files being translated, any .java
files entered on the command-line, and any imported Java classes whose .class
files or .java
files can be found through the classpath. SQLJ examines whether and how stream types are used in SELECT
or CAST
statements, what Java types are used in iterator columns or INTO-lists, what Java types are used as input host variables, and what Java types are used as output host variables.
SQLJ also processes FETCH
, CAST
, CALL
, SET TRANSACTION
, VALUES
, and SET
statements syntactically.
Any Java expression in a SQLJ executable statement must have a Java type valid for the given situation and usage. For example, consider the following statement:
#sql [myCtx] { UPDATE ... };
The myCtx
variable, which might be used to specify a connection context instance or execution context instance for this statement, must actually resolve to a SQLJ connection context type or execution context type.
Now consider the following example:
#sql { UPDATE emp SET sal = :newSal };
If newSal
is a variable (as opposed to a field), then an error is generated if newSal
was not previously declared. In any case, an error is generated if it cannot be assigned to a valid Java type, or its Java type cannot be used in a SQL statement (a java.util.Vector
, for example).
SELECT
or INSERT
, so that SQLJ knows what kind of operation it is. For example, the following statement will generate an error:
#sql { foo };
The following two tasks are performed only if online checking is enabled:
In the process of doing this, SQLJ verifies that the SQL objects used in your SQLJ executable statements (such as tables, views, and stored procedures) actually exist in the database. SQLJ also checks nullability of database columns whose data is being selected into iterator columns of Java primitive types, which cannot process null data. (Nullability is not checked for stored procedure and function output parameters and return values, however.)
If the semantics-checker discovers any syntax or semantics errors during this phase, then the translation is aborted and the errors are reported.
Oracle supplies Oracle-specific offline checkers, a generic offline checker, Oracle-specific online checkers, and a generic online checker. For more information about checkers, see "Offline Semantics-Checker (-offline)" and "Online Semantics-Checker (-online)".
The generic checkers assume you use only standard SQL92 and standard JDBC features. Oracle recommends that you use the Oracle-specific checkers when using an Oracle database.
Notes: The following is not checked against the database during online semantics-checking:
|
For your .sqlj
application source file, the SQLJ translator generates a .java
file and, for standard SQLJ code generation, at least one profile (either in .ser
or .class
files). A .java
file is created for your translated application source code, class definitions for private iterators and connection contexts you declared, and a profile-keys class definition generated and used internally by SQLJ.
Note: Profiles and a profile-keys class are not generated under the following circumstances:
See "Oracle-Specific Code Generation (No Profiles)". |
Once your application source code has passed the preceding syntax and semantics checks, it is translated and output to a .java
file. For standard SQLJ code generation, SQLJ executable statements are replaced by calls to the SQLJ runtime, which in turn contains calls to the JDBC driver to access the database. For Oracle-specific code generation, SQLJ executable statements are replaced by direct calls to Oracle JDBC (there are also calls to an Oracle-specific SQLJ runtime).
The generated .java
file contains all your generic Java code, your iterator class and connection context class definitions, and calls to the SQLJ runtime.
For convenience, generated .java
files also include a comment for each of your #sql
statements, repeating the statement in its entirety for reference.
The generated .java
file will have the same base name as the input .sqlj
file, which would be the name of the public class defined in the .sqlj
file (or the first class defined if there are no public classes). For example, Foo.sqlj
defines class Foo
, and source file Foo.java
will be generated by the translator.
The location of the generated .java
file depends on whether and how the SQLJ -dir
option is set. By default, the .java
file will be placed in the directory of the .sqlj
input file. See "Output Directory for Generated .java Files (-dir)" for more information.
During translation with standard SQLJ code generation, SQLJ generates a profile-keys class that it uses internally during runtime to load and access the serialized profile. This class contains mapping information between the SQLJ runtime calls in your translated application and the SQL operations placed in the serialized profile. It also contains methods to access the serialized profile.
The profile-keys class is defined in the same .java
output file that has your translated application source code, with a class name based on the base name of your .sqlj
source file as follows:
Basename_SJProfileKeys
For example, translating Foo.sqlj
defines the following profile-keys class in the generated .java
file:
Foo_SJProfileKeys
If your application is in a package, this is reflected appropriately. For example, translating Foo.sqlj
in the package a.b
defines the following class:
a.b.Foo_SJProfileKeys
When you use standard SQLJ code generation, SQLJ generates profiles that it uses to store information about the SQL operations found in the input file. A profile is generated for each connection context class that you use in your application. It describes the operations to be performed using instances of the associated connection context class, such as SQL operations to execute, tables to access, stored procedures and functions to call.
Profiles are generated in .ser
serialized resource files. If, however, you enable the SQLJ -ser2class
option, they are automatically converted to .class
files as part of the translation. (In this case, no further customization of the profile is possible. You would have to delete the .class
file and rerun the SQLJ translator to regenerate the profile.)
Profile base names are generated similarly to the profile-keys class name. They are fully qualified with the package name, followed by the .sqlj
file base name, followed by the string:
_SJProfile
n
Where n
is a unique number, starting with 0, for each profile generated for a particular .sqlj
input file.
Again using the example of the input file Foo.sqlj
, if two profiles are generated, then they will have the following base names (presuming no package):
Foo_SJProfile0
Foo_SJProfile1
If Foo.sqlj
is in the package a.b
, then the profile base names will be:
a.b.Foo_SJProfile0
a.b.Foo_SJProfile1
Physically, a profile exists as a Java serialized object contained within a resource file. Resource files containing profiles use the .ser
extension and are named according to the base name of the profile (excluding package names). Resource files for the two previously mentioned profiles will be named:
Foo_SJProfile0.ser
Foo_SJProfile1.ser
Or they will be named Foo_SJProfile0.class
and Foo_SJProfile1.class
if you enable the -ser2class
option. If you choose this option, the conversion to .class
takes place after the customization step below. See "Conversion of .ser File to .class File (-ser2class)" for more information.
The location of these files depends on how the SQLJ -d
option is set, which determines where all generated .ser
and .class
files are placed. See "Output Directory for Generated .ser and .class Files (-d)" for more information.
In a later step in the SQLJ process, your profiles are customized for use with your particular database. See "Profile Customization".
When your #sql
statements are replaced by calls to the SQLJ runtime (for standard SQLJ code generation) or Oracle JDBC (for Oracle-specific code generation), these calls implement the following steps:
A SQLJ runtime uses SQLJ statement objects that are similar to JDBC statement objects, although a particular implementation of SQLJ might or might not employ JDBC statement classes directly. SQLJ statement classes add functionality particular to SQLJ. For example:
int
or float
, which cannot take null values.
After code generation, SQLJ invokes the Java compiler to compile the generated .java
file. This produces a .class
file for each class you defined in your application, including iterator and connection context declarations, as well as a .class
file for the generated profile-keys class (presuming your application uses SQLJ executable statements). Any .java
files you specified directly on the SQLJ command line (for type-resolution, for example) are compiled at this time as well.
In the example used in "Code Generation", the following .class
files would be produced in the appropriate directory (given package information in the source code):
Foo.class
Foo_SJProfileKeys.class
.class
file for each additional class you defined in Foo.sqlj
.class
file for each iterator and connection context class you declared in Foo.sqlj
(whether public or private)
To ensure that .class
files generated by the compiler and profiles generated by SQLJ (whether .ser
or .class
) will be located in the same directory, SQLJ passes its -d
option to the Java compiler. If the -d
option is not set, then .class
files and profiles are placed in the same directory as the generated .java
file (which is placed according to the -dir
option setting).
In addition, so that SQLJ and the Java compiler will use the same encoding, SQLJ passes its -encoding
option to the Java compiler (unless the SQLJ -compiler-encoding-flag
is turned off). If the -encoding
option is not set, SQLJ and the compiler will use the setting in the JVM file.encoding
property.
By default, SQLJ invokes the standard javac
compiler of the Sun Microsystems JDK, but other compilers can be used instead. You can request that an alternative Java compiler be used by setting the SQLJ -compiler-executable
option.
For information about compiler-related SQLJ options, see the following:
After Java compilation, the generated profiles (which contain information about your embedded SQL instructions) are customized so that your application can work efficiently with your database and use vendor-specific extensions.
Note: If you use Oracle-specific code generation, SQLJ produces no profiles and skips the customization step. Your code will support Oracle-specific features through direct calls to Oracle JDBC APIs. See "Oracle-Specific Code Generation (No Profiles)" for more information. |
To accomplish customization, SQLJ invokes a front end called the customizer harness, which is a Java class that functions as a command-line utility. The harness, in turn, invokes a particular customizer, either the default Oracle customizer or a customizer that you specify by SQLJ option settings.
During customization, profiles are updated in two ways:
Without customization, you can access and use only standard JDBC types.
For example, the Oracle customizer can update a profile to support an Oracle9i PERSON
type that you had defined. You could then use PERSON
as you would any other supported datatype.
You also must customize with the Oracle customizer to utilize any of the oracle.sql
type extensions.
Be aware of the following:
.ser
files, or .jar
files containing .ser
files, on the command line. But you cannot do this in the same running of SQLJ where translations are taking place. You can specify .ser
/.jar
files to be customized or .sqlj
/.java
files to be translated and compiled, but not both. For more information about how .jar
files are used, see "Use of JAR Files for Profiles".
For more information about profile customization, see Chapter 10, "Profiles and Customization".
Also see the following for information about SQLJ options related to profile customization:
This section provides an overview of SQLJ translator messages and exit codes.
There are three major levels of SQLJ messages you might encounter during the translation phase: error, warning, and information. Warning messages can be further broken down into two levels: non-suppressible and suppressible. Therefore, there are four message categories (in order of seriousness):
You can control suppressible warnings and information by using the SQLJ -warn
option, as described below.
Error messages, prefixed by Error:
, indicate that one of the following has been encountered:
VARCHAR
into a java.util.Vector
, using an Oracle JDBC driver)
If errors are encountered during SQLJ translation, then no output is produced (.java
file or profiles), and compilation and customization are not executed.
Non-suppressible warning messages, prefixed by Warning:
, indicate that one of the following has been encountered:
SELECT
statement whose output is not assigned to anything)
SQLJ translation will complete if a non-suppressible warning is encountered, but you should analyze the problem and determine if it should be fixed before running the application. If online checking is specified but cannot be completed, offline checking is performed instead.
Suppressible warning messages, also prefixed by Warning:
, indicate that there is a problem with a particular aspect of your application, such as portability. An example of this is using an Oracle-specific type such as oracle.sql.NUMBER
to read from or write to Oracle9i.
Informational or status messages prefixed by Info:
do not indicate an error condition. They merely provide additional information about what occurred during the translation phase.
Suppressible warning and status messages can be suppressed by using the various -warn
option flags:
cast/nocast
--The nocast
setting suppresses warnings about possible runtime errors when trying to cast an object type instance to an instance of a subtype.
precision/noprecision
--The noprecision
setting suppresses warnings regarding possible loss of data precision during conversion.
nulls/nonulls
--The nonulls
setting suppresses warnings about possible runtime errors due to nullable columns or types.
portable/noportable
--The noportable
setting suppresses warnings regarding SQLJ code that uses Oracle-specific features or might otherwise be non-standard and, therefore, not portable to other environments.
strict/nostrict
--The nostrict
setting suppresses warnings issued if there are fewer columns in a named iterator than in the selected data that is to populate the iterator.
verbose/noverbose
--The noverbose
setting suppresses status messages that are merely informational and do not indicate error or warning conditions.
See "Translator Warnings (-warn)" for more information about the -warn
option and how to set the flags.
If you receive warnings during your SQLJ translation, then you can try running the translator again with -warn=none
to see if any of the warnings are of the more serious (non-suppressible) variety.
Notes: For information about particular error, warning, and information messages, see "Translation Time Messages" and "Runtime Messages". |
Table 9-2 summarizes the categories of error and status messages generated by the SQLJ translator.
In addition to the translator's error, warning, and information messages, SQLJ can produce status messages throughout all phases of SQLJ operation--translation, compilation, and customization. Status messages are output as each file is processed and at each phase of SQLJ operation.
You can control status messages by using the SQLJ -status
option. This option is described in "Real-Time Status Messages (-status)".
The following exit codes are returned by the SQLJ translator to the operating system upon completion:
.sqlj
source file to the resulting .class
file)
ser2class
conversion (the optional conversion of profile files from .ser
files to .class
files)
This section presents information about the Oracle SQLJ runtime, which is a thin layer of pure Java code that runs above the JDBC driver. When Oracle SQLJ translates your SQLJ source code using standard SQLJ code generation, embedded SQL commands in your Java application are replaced by calls to the SQLJ runtime. Runtime classes act as wrappers for equivalent JDBC classes, providing special SQLJ functionality. When the end user runs the application, the SQLJ runtime acts as an intermediary, reading information about your SQL operations from your profile and passing instructions along to the JDBC driver.
If you use Oracle-specific code generation, the SQLJ runtime layer becomes even thinner. Most of the runtime functionality is compiled directly into Oracle JDBC calls. This requires an Oracle JDBC driver, as opposed to a JDBC driver from some other vendor. See "Oracle-Specific Code Generation (No Profiles)".
Generally speaking, however, a SQLJ runtime can be implemented to use any JDBC driver or vendor-proprietary means of accessing the database. The Oracle SQLJ runtime requires a JDBC driver but can use any standard JDBC driver. To use Oracle-specific datatypes and features, however, you must use an Oracle JDBC driver. For the purposes of this document, it is generally assumed that you are using an Oracle database (or perhaps the middle-tier database cache) and one of the Oracle JDBC drivers.
The Oracle SQLJ runtime includes packages you will likely import and use directly, and others that are used only indirectly.
This section lists packages containing classes you can import and use directly in your application. Packages whose names begin with oracle
are for Oracle-specific SQLJ features.
sqlj.runtime
This package includes the ExecutionContext
class, ConnectionContext
interface, ResultSetIterator
interface, ScrollableResultSetIterator
interface, and wrapper classes for streams (BinaryStream
, AsciiStream
, and UnicodeStream
).
Interfaces and abstract classes in this package are implemented by classes in the sqlj.runtime.ref
package or by classes generated by the SQLJ translator.
sqlj.runtime.ref
The classes in this package implement interfaces and abstract classes in the sqlj.runtime
package. You will likely use the sqlj.runtime.ref.DefaultContext
class, which is used to specify your default connection and create default connection context instances. The other classes in this package are used internally by SQLJ in defining classes during code generation, such as iterator classes and connection context classes that you declare in your SQLJ code.
oracle.sqlj.runtime
This package contains the Oracle
class that you can use to instantiate the DefaultContext
class and establish your default connection. It also contains Oracle-specific runtime classes used by the Oracle implementation of SQLJ, including functionality to convert to and from Oracle type extensions.
This section lists packages containing classes that are for internal use by SQLJ.
sqlj.runtime.profile
This package contains interfaces and abstract classes that define what SQLJ profiles look like. This includes the EntryInfo
class and TypeInfo
class. Each entry in a profile is described by an EntryInfo
object (where a profile entry corresponds to a SQL operation in your application). Each parameter in a profile entry is described by a TypeInfo
object.
The interfaces and classes in this package are implemented by classes in the sqlj.runtime.profile.ref
package.
sqlj.runtime.profile.ref
This package contains classes that implement the interfaces and abstract classes of the sqlj.runtime.profile
package, and are used internally by the SQLJ translator in defining profiles. It also provides the default JDBC-based runtime implementation.
sqlj.runtime.error
This package, used internally by SQLJ, contains resource files for all generic (non-Oracle-specific) error messages that can be generated by the SQLJ translator.
oracle.sqlj.runtime.error
This package, used internally by SQLJ, contains resource files for all Oracle-specific error messages that can be generated by the SQLJ translator.
Runtime errors can be generated by any of the following:
In any of these cases, a SQL exception is generated as an instance of the java.sql.SQLException
class or a subclass (such as sqlj.runtime.SQLNullException)
.
Depending on where the error came from, there might be meaningful information you can retrieve from an exception using the getSQLState()
, getErrorCode()
, and getMessage()
methods. SQLJ errors, for example, include meaningful SQL states and messages. For information, see "Retrieving SQL States and Error Codes".
If errors are generated by the Oracle JDBC driver or RDBMS at runtime, look at the prefix and consult the appropriate documentation:
For a list of SQLJ runtime errors, see "Runtime Messages".
Oracle SQLJ uses Java's built-in capabilities for globalization support (otherwise known as National Language Support, or NLS). This section discusses the following:
Some prior knowledge of Oracle Globalization Support is assumed, particularly regarding character encoding and locales. For information, see the Oracle9i Globalization Support Guide.
There are two main areas of SQLJ globalization support:
There are three parts to this:
This determines which translations of error and status message lists are used when SQLJ outputs messages to the user, either during SQLJ translation or SQLJ runtime.
Globalization support at runtime is transparent to the user, presuming your SQLJ source code and SQL character data use only characters that are within the database character set. SQL character data is transparently mapped into and out of Unicode.
Note that for multi-language applications, it is advisable to use one of the following options:
or:
The character encoding setting for source files tells Oracle SQLJ two things:
.sqlj
and .java
input files that the SQLJ translator must read
.java
output files that it generates
By default, SQLJ uses the encoding indicated by the JVM file.encoding
property. If your source files use other encodings, then you must indicate this to SQLJ so that appropriate conversion can be performed.
Use the SQLJ -encoding
option to accomplish this. SQLJ also passes the -encoding
setting to the compiler for it to use in reading .java
files (unless the SQLJ -compiler-encoding-flag
is off).
The system character-encoding setting also determines how SQLJ error and status messages are represented when output to the user, either during translation or during runtime when the end user is running the application. This is set according to the file.encoding
property and is unaffected by the SQLJ -encoding
option.
For source file encoding, you can use the -encoding
option to specify any character encoding supported by your Java environment. If you are using the Sun Microsystems JDK, these are listed in the native2ascii
documentation, which you can find at the following Web site:
http://www.javasoft.com/products/jdk/1.1/docs/tooldocs/solaris/native2ascii.html
Dozens of encodings are supported by the Sun Microsystems JDK. These include 8859_1
through 8859_9
(ISO Latin-1 through ISO Latin-9), JIS
(Japanese), SJIS
(shift-JIS, Japanese), and UTF8
.
Be aware of the following:
\uHHHH
, where each H is a hexadecimal digit).
.sqlj
source file is read and processed during translation, error messages quote source locations based on character position (not byte position) in the input encoding.
-encoding
option or the Java file.encoding
setting, do not apply to Java properties files--sqlj.properties
and connect.properties
, for example. Properties files always use the encoding 8859_1
. This is a feature of Java in general, not SQLJ in particular. You can, however, use Unicode escape sequences in a properties file. (You can use the native2ascii
utility to determine escape sequences--see "Using native2ascii for Source File Encoding".)
SQLJ error and status reporting, either during translation or during runtime, uses the Java locale setting in the JVM user.language
property. Users typically do not have to alter this setting.
Language support is implemented through message resources that use key/value pairs. For example, where an English-language resource has a key/value pair of "OkKey", "Okay"
, a German-language resource has a key/value pair of "OkKey", "Gut"
. The locale setting determines which message resources are used.
SQLJ supports locale settings of en
(English), de
(German), fr
(French), and ja
(Japanese).
Oracle SQLJ provides syntax that allows you to set the following:
Use the SQLJ -encoding
option.
Use the SQLJ -J
prefix to set the Java file.encoding
property.
Use the SQLJ -J
prefix to set the Java user.language
property.
Use the SQLJ -encoding
option to determine the character encoding used in representing .sqlj
files read by the translator, .java
files generated by the translator, and .java
files read by the compiler. (The option setting is passed by SQLJ to the compiler, unless the SQLJ -compiler-encoding-flag
is off.)
This option can be set on the command line or SQLJ_OPTIONS
environment variable, as in the following example:
-encoding=SJIS
Or it can be set in a SQLJ properties file as follows:
sqlj.encoding=SJIS
If the encoding option is not set, then both the translator and compiler will use the encoding specified in the JVM file.encoding
property. This can also be set through the SQLJ command line, as discussed in "Setting Character Encoding and Locale for SQLJ Messages".
For more information, see "Encoding for Input and Output Source Files (-encoding)" and "Compiler Encoding Support (-compiler-encoding-flag)".
Note:
If your |
Character encoding and locale for SQLJ error and status messages output to the user, during both translation and runtime, are determined by the Java file.encoding
and user.language
properties. Although it is typically not necessary, you can set these and other JVM properties in the SQLJ command line by using the SQLJ -J
prefix. Options marked by this prefix are passed to the JVM.
Set the character encoding as in the following example (which specifies shift-JIS
Japanese character encoding):
-J-Dfile.encoding=SJIS
Set the locale as in the following example (which specifies Japanese locale):
-J-Duser.language=ja
The -J
prefix can be used on the command line or SQLJ_OPTIONS
environment variable only. It cannot be used in a properties file, because properties files are read after the JVM is invoked.
Notes:
|
For additional information about the SQLJ -J
prefix, see "Command-Line Syntax and Operations" and "Options to Pass to the Java Virtual Machine (-J)".
Following is a complete SQLJ command line, including JVM file.encoding
and user.language
settings:
sqlj -encoding=8859_1 -J-Dfile.encoding=SJIS -J-Duser.language=ja Foo.sqlj
This example uses the SQLJ -encoding
option to specify 8859_1
(Latin-1) encoding for source code representation during SQLJ translation. This encoding is used by the translator in reading the .sqlj
input file and in generating the .java
output file. The encoding is then passed to the Java compiler to be used in reading the generated .java
file. (The -encoding
option, when specified, is always passed to the Java compiler unless the SQLJ -compiler-encoding-flag
is disabled.)
For error and status messages output during translation of Foo.sqlj
, the SQLJ translator uses the SJIS
encoding and the ja
locale.
Oracle9i SQLJ adds support for Java types (Unicode character types) derived from existing character and stream types that convey expected usage for globalization support in the server. (In SQLJ it is not possible to use JDBC statement or result set methods directly that otherwise serve the purpose of globalization support, but if you are interested in information about those methods, refer to the Oracle9i JDBC Developer's Guide and Reference.)
If the database natively supports Unicode , then the types described in "Java Types for Globalization Support" below are unnecessary--globalization support will be handled transparently. It is when the database does not natively support Unicode, but has a national language character set that does support Unicode, that you will typically use these types (for columns that employ the national language character set).
Oracle9i SQLJ provides a number of Java types for globalization support. Table 9-3 notes the correspondence between these globalization support types and general-use JDBC and SQLJ character and stream types. Each globalization support type, except for NString
, is a subclass of its corresponding JDBC or SQLJ type.
In situations where your application must handle national language character strings as input parameters to the database or as output parameters from the database, use the globalization support types instead of the corresponding general-use types.
Because the oracle.sql.CHAR
class (and therefore its NCHAR
subclass) provides only constructors that require explicit knowledge of the database character set, the oracle.sql.NString
class--a wrapper for java.lang.String
--is preferable in most circumstances.
The NString
class provides simpler constructors and ensures that the national language character form of use is registered with the JDBC driver.
Following are the key NString
methods:
NString(String)
--This constructor creates an NString
instance from an existing String
instance.
String toString()
--This method returns the underlying String
instance.
String getString()
--This method also returns the underlying String
instance.
The toString()
method allows you to employ the NString
instance in string concatenation expressions (such as "a"+b
, where b
is a string). The getString()
method, provided in the CHAR
superclass, is supported as well for uniformity. In addition, the member methods of the String
class are carried over to the NString
wrapper class to allow you to write more concise code.
The following examples show use of the NString
class.
This example uses an NString
instance as an input parameter to the database.
import oracle.sql.NString; ... NString nc_name = new NString("Name with strange characters"); #sql { update PEOPLE set city = :(new NString("\ufff2")), name = :nc_name where num= :n }; ...
This example uses an NString
instance as an output parameter from the database.
import oracle.sql.NString; ... NString nstr; #sql { call foo(:out nstr) }; System.out.println("Result is: "+nstr); // or, explicitly: System.out.println("Result is: "+nstr.toString()); ...
This example uses the NString
type for an iterator column. Such usage is superfluous (given that the underlying network protocol supports national language characters implicitly), but harmless. This example also shows use of one of the String
methods, substring()
, that is carried over to NString
.
import oracle.sql.NString; import oracle.sql.NCLOB; ... #sql iterator NIter(NString title, NCLOB article); NIter nit; #sql nit = { SELECT article, title FROM page_table }; while (nit.next()) { System.out.println("<TITLE>"+nit.title()+"</TITLE>"); ... nit.article().substring(0, 1000); ... }
Note:
Using the
|
This section discusses ways to manipulate your Oracle Globalization Support configuration outside of SQLJ.
As with any end user running any Java application, those running your SQLJ application can specify JVM properties such as file.encoding
and user.language
directly, as they invoke the JVM to run your application. This determines the encoding and locale used for message output as your application executes.
They can accomplish this as in the following example:
java -Dfile.encoding=SJIS -Duser.language=ja Foo
This will use SJIS
encoding and Japanese locale.
In Java code, you can determine values of Java properties by using the java.lang.System.getProperty()
method, specifying the appropriate property. For example:
public class Settings { public static void main (String[] args) { System.out.println("Encoding: " + System.getProperty("file.encoding") + ", Language: " + System.getProperty("user.language")); } }
You can compile this and run it as a standalone utility.
There is also a getProperties()
method that returns the values of all properties, but this will raise a security exception if you try to use it in code that runs in the server.
You can get information about the java.lang.System
class at the following Web site:
http://www.javasoft.com/products/jdk/1.1/docs/api/java.lang.System.html
If you are using a Sun Microsystems JDK, there is an alternative to having SQLJ do the character encoding for your source files. You can use the utility native2ascii
to convert sources with native encoding to sources in 7-bit ASCII with Unicode escape sequences.
Run native2ascii
as follows:
% native2ascii <options> <inputfile> <outputfile>
Standard input or standard output are used if you omit the input file or output file. Two options are supported:
-reverse
(reverse the conversion; convert from Latin-1 or Unicode to native encoding)
-encoding <
encoding
>
For example:
% native2ascii -encoding SJIS Foo.sqlj Temp.sqlj
For more information see the following Web site:
http://www.javasoft.com/products/jdk/1.1/docs/tooldocs/solaris/native2ascii.html
|
Copyright © 1996-2001, Oracle Corporation. All Rights Reserved. |
|