Oracle9i SQLJ Developer's Guide and Reference Release 2 (9.2) Part Number A96655-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 or offline parsing 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 and Java application source code is verified as syntactically correct, the translator enters into the semantics-checking phase and invokes a SQL semantics-checker or a SQL offline parser or both, according to SQLJ option settings.
Setting the -user
option enables 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, typically sufficient, is a checker front end called OracleChecker
that chooses the most appropriate checker, according to whether you have enabled online checking and which JDBC driver you are using.
The -parse
option, true
by default, is for enabling the offline parser, which offers a way to verify SQL and PL/SQL syntax (but not data types against database columns) without necessitating a database connection during translation. Note that some settings of the -parse
option will override the -user
option and disable online checking.
For more information about online checking, offline parsing, and comparative features, see "Connection Options" and "Semantics-Checking and Offline-Parsing Options".
Note: For ISO code generation, 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, regardless of the status of online checking or offline parsing:
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 task is performed if either online checking or offline parsing (or both) is enabled:
The following task is performed only if online checking is enabled:
In the process of doing this, SQLJ verifies that the SQL entities 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.
For the .sqlj
application source file, the SQLJ translator generates a .java
file and, for ISO standard SQLJ code generation, at least one profile (either in .ser
or .class
files). The .java
contains your translated application source code, class definitions for any private iterators and connection contexts you declared, and, for ISO code, a profile-keys class definition generated and used internally by SQLJ.
Note: No profiles or profile-keys class are generated if you use the default Oracle-specific code generation mode ( With ISO code generation, there are no profiles or profile-keys class if you do not use any SQLJ executable statements in your code. |
For the default Oracle-specific code generation, the generated .java
file for your application contains direct calls to Oracle JDBC in place of the original SQLJ executable statements. There are also calls to an Oracle-specific SQLJ runtime.
For ISO standard SQLJ code generation, SQLJ executable statements are replaced by calls to the SQLJ runtime, which in turn contains calls to the JDBC driver.
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 you set the SQLJ -dir
option. 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.
Is you use ISO 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.
Note: If you use the default Oracle-specific code generation, no profiles or profile-keys classes are generated. |
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
If you use ISO 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, and 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 (ISO Code Generation)".
When #sql
statements are replaced by calls to Oracle JDBC (for Oracle-specific code generation) or to the SQLJ runtime (for ISO standard SQLJ 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 if you use ISO code generation (and 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
(ISO code generation only).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 and profiles (if any, 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 SQLJ support for compiler options and compiler-related SQLJ options, see the following:
After Java compilation, if you are using ISO standard code generation (-codegen=iso
), the generated profiles containing 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 the default Oracle-specific code generation ( |
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 through 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 use any of the oracle.sql
type extensions.
Notes: Be aware of the following regarding profile customization:
|
For more information about profile customization, see Appendix A, "Customization and Specialized Customizers".
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, 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 "SQLJ Translation Time Messages" and "SQLJ Runtime Messages". |
Table 9-2 summarizes the categories of error and status messages generated by the SQLJ translator.
In addition to the 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
fileser2class
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.
If you use the default Oracle-specific code generation (-codegen=oracle
), the SQLJ runtime layer becomes even thinner, with a runtime subset being used in conjunction with an Oracle JDBC driver. Most of the runtime functionality is compiled directly into Oracle JDBC calls. You cannot use a non-Oracle JDBC driver. See "Oracle-Specific Code Generation (No Profiles)".
When Oracle SQLJ translates SQLJ source code using ISO standard code generation (-codegen=iso
), 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.
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 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.
Note: These packages are included in the runtime libraries |
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, ConnectionContextFactory
interface, ResultSetIterator
interface, ScrollableResultSetIterator
interface, and wrapper classes for streams (BinaryStream
and CharacterStream
, as well as the deprecated AsciiStream
and UnicodeStream
).
Interfaces and abstract classes in this package are implemented by classes in the sqlj.runtime.ref
package or the oracle.sqlj.runtime
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 (applicable only for ISO standard code generation). 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 (for ISO standard code generation only). It also provides the default JDBC-based runtime implementation.
sqlj.runtime.error
This package, used internally by SQLJ, contains resource files for all generic (not 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 as 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 "SQLJ Runtime Messages".
Oracle SQLJ uses the Java 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 Database 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:
Notes:
|
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 generatesBy 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 (or replace "1.2" in the URL with the appropriate JDK level, such as "1.1" or "1.3"):
http://www.javasoft.com/products/jdk/1.2/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 the message resources 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 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
Note: Only a limited number of encodings might be available, depending on platform or operating system considerations. |
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) 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, either inserting them into or selecting them from national language character set columns, use the globalization support types instead of the corresponding general-use types.
The oracle.sql.CHAR
class, and therefore its NCHAR
subclass, provides only constructors that require explicit knowledge of the database character set. Therefore, 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 (or replace "1.2" in the URL with the appropriate JDK level, such as "1.1" or "1.3"):
http://www.javasoft.com/products/jdk/1.2/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 (or replace "1.2" in the URL with the appropriate JDK level, such as "1.1" or "1.3"):
http://www.javasoft.com/products/jdk/1.2/docs/tooldocs/solaris/native2ascii.html
|
Copyright © 1999, 2002 Oracle Corporation. All Rights Reserved. |
|