ORACLE built-in JDBC source code parsing

Source: Internet
Author: User
Tags unsupported
Conventions: 1. If java. lang. unsatisfiedLinkError: do_open, you need to set DriverManager. modify the url of the getConnection () method to jdbc: oracle: thin: @ 127.0.0.1: 1521: oradb. The specific cause is unknown. 2. If java occurs. SQL. SQLException: Unsupported Character Set: oracle-characte

Conventions: 1. If java. lang. unsatisfiedLinkError: do_open, you need to set DriverManager. modify the url of the getConnection () method to jdbc: oracle: thin: @ 127.0.0.1: 1521: oradb. The specific cause is unknown. 2. If java occurs. SQL. SQLException: Unsupported Character Set: oracle-characte

Conventions:
1. If java. lang. unsatisfiedLinkError: do_open, you need to set DriverManager. modify the url of the getConnection () method to jdbc: oracle: thin: @ 127.0.0.1: 1521: oradb. The specific cause is unknown;
2. If java. SQL. SQLException: Unsupported character set: oracle-character-set-852, you need to add nls_charset12.zip to your project (this file is in the same directory as classes12.zip );
The following describes the detailed functions of the files under the Folder \ samples \ oci8 \ object-samples:
1. PersonObject. java
This example demonstrates that the ADT field empid exists in the table "people". Its type is "PERSON" and the type "PERSON" contains the ADT field "home". Its type is "ADDRESS" and the type "ADDRESS" is an ADT.
If a regular SQL statement is used, its insert statement is no different from that in SQL/plus, that is, it is constructed using the nested constructor.
Another method is to use the STRUCT Constructor (StructDescriptor, Connection, Object []) to construct a STRUCT Object, that is, an ADT Object. At the same time, if nested, the ADT object needs to be nested. Finally, use the setObject method of PreparedStatement to specify the ADT object.
When reading data, the method is opposite to the above method: if it is a simple type, it will be directly read; if it is ADT, it will use getObject () of ResultSet and then forcibly convert it to STRUCT, then, call the getAttributes () method of STRUCT to obtain the Object [] type data, which is recursive.
2. SQLDataExample. java and EmployeeObj. java
This example is similar to "1" and is also used for ADT processing. The difference is that the type is not nested.
In comparison, the code of 2 to 1 is much simpler, but it also pays a price: Abstract A Class of EmployeeObj for the type of EMPLOYEE, which implements the SQLData interface, and rewrite the three methods (required ).
The call at the front end is much simpler than the second method in method 1. You only need to use the setObject method of PreparedStatement to specify the ADT object (however, You need to specify its type as OracleTypes. STRUCT ). You can also directly use the getObject () of the OracleResultSet and forcibly convert it to the EmployeeObj object.
It can be said that there are gains and losses !!
In this example, note the following points:
2.1 import oracle. jdbc2. * In EmployeeObj. java; changed to import oracle. jdbc. *; unknown cause;
2.2 SQLDataExample. dictionary map = conn in java. getTypeMap (); changed to java. util. map map = conn. getTypeMap (); cause: NOTE: This class (Dictionary) is obsolete. new implementations shocould implement the Map interface, rather than extendidng this class. (Source: javadoc );
2.3 change pstmt.exe cuteQuery () in SQLDataExample. java to pstmt.exe cuteUpdate (). It is more reasonable because this is an update rather than a query (it will not affect functions without modification, but is recommended only );
3. CustomDatumExample. java and Employee. java
This example is exactly the same as 2. The difference is that another abstraction technology is used, and the interfaces CustomDatum and CustomDatumFactory are implemented, and two methods toDatum () and create () are rewritten (). There is also a little difference when accessing data at the front end: the getCustomDatum () method of the OracleResultSet is used and forced to convert it to Employee. In terms of appearance, SQLData interface 2 exists in java. SQL. * package. The CustomDatum and CustomDatumFactory interfaces exist in oracle. SQL. * In the package, it can be considered as a special implementation of Oracle's own products. Maybe it has higher performance and lower overhead? However, 3 is not as direct as 2, I personally think.
4. ArrayExample. java
From the file name, we can see that this example demonstrates the VARRAY type.
In the same way as 1, there are two ways to insert. SQL can be used directly. In addition, the setARRAY method of OraclePreparedStatement can be used to construct an ARRAY. The Data Reading Process is the opposite: first use the getARRAY () method of the OracleResultSet to obtain the ARRAY object, then call the getArray () method of this object and forcibly convert it into an ARRAY of objects, then you can operate on this array.
5. PersonRef. java and StudentRef. java
I wonder if ORACLE is a joke with us. Except for the different class names, the two files share the same name.
This example demonstrates the object table concept. The insertion of an object table is no different from that of a normal table. Data Reading is first reflected in its SQL statement. You need to use the REF function and then call the getObject () function of ResultSet () method and force convert to REF Object, then use the getValue () of this Object and convert it to a STRUCT Object. Then use the getAttributes () method of this Object to get the Object [], and then operate on this array.
6. RefClient. java and GenREF. java
This sample demonstrates using REF over two different Sessions.
Class GenREF is used to encapsulate the type and binary content pointed to by the REF object. The program first Materialize into GenREF, and then De-materialize REF from GenREF in another session. The following is the same as processing REF: Use the getValue () of this object and convert it into a STRUCT object, use the getAttributes () method of this Object to get the Object [] And then operate on this array.
7. FileExample. java and PLSQL_FileExample.java
This example uses the BFILE data type, Contains a locator (locator) to a large binary file stored outside the database. The bfilename function is required for data insertion. This function requires a directory object. This object must be created using create DIRECTORY mydir (this user must have the create any directory system permission ). I once had a major lesson in this place: follow the steps to always prompt that I do not have this directory when reading !! In the end, I can write the first parameter of the BFILENAME function (that is, the directory name) in upper case. Cause: the directory name is case-sensitive in the function. Although it is created in lower case, it is automatically converted to upper case when it is in progress.
When reading data of the BFILE type, first obtain the binary input stream through its getBinaryStream () method, and then operate the input stream. The limitation is your imagination.
PLSQL_FileExample.java implements the same functions as above, except that all BFILE-oriented operations are completed by calling pl/SQL anonymous blocks and using OracleCallableStatement. If not necessary, use the former.
8. LobExample. java and PLSQL_LobExample.java
The CLOB and BLOB data types are used in this example.
When inserting data, you must use getBinaryOutputStream () and getCharacterOutputStream () to obtain the binary output stream and character output stream, and then perform operations on the output stream, same as traditional java I/O.
When reading data, you must use getBinaryStream () and getCharacterStream () to obtain the binary input stream and character input stream, and then perform operations on the input stream, which is the same as the traditional java I/O.
PLSQL_LobExample.java implements the same functions as above, except that all operations on CLOB and BLOB are completed by calling pl/SQL anonymous blocks and using OracleCallableStatement. If not necessary, use the former.

Note: The dbms_lob package is used in the second example of 7 and 8.

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.