Oracle self-brought JDBC source code resolution

Source: Internet
Author: User
Tags anonymous array character set constructor create directory insert interface zip
oracle| Source Code
People who have used JDBC even ORACLE will know the fact that we need the library file Classes12.zip exists in the $oracle_home/jdbc/lib directory (but there are still some rookie often in the forum to seek this library files, really hard) But few know that ORACLE has also prepared an example of using JDBC for us, which exists in the $ORACLE _home/jdbc/demo/demo.zip.
Some time ago, I learned about Oracle's OOP technology and was very touched. At the time, I thought: there must be some OO technology in JDBC to support it. For a long time no suitable example was found, and it was finally found in Oracle's installation directory. Really is: Pale no place, get all without effort.

Conventions:
1. If Java.lang.UnsatisfiedLinkError:do_open appears, you need to modify the URL of the Drivermanager.getconnection () method to jdbc:oracle:thin:@ 127.0.0.1:1521:ORADB, the specific reasons unknown;
2, if appear java.sql.SQLException: Unsupported character set: oracle-character-set-852, then you need to add Nls_charset12.zip to your project (this file and Classes12.zip same directory);

Below I will be the folder Amples\oci8\object-samples under the file to do a detailed functional description:
1, Personobject.java
This example demonstrates the existence of a ADT field empid in table people, whose type is person, and the presence of ADT field home in type person, whose type is address, and the type address is a ADT.
If you use a regular SQL statement, the INSERT statement is the same as in Sql/plus, that is, using a constructor nesting construct.
Another method is to construct a object[object, a STRUCT object, using the STRUCT constructor STRUCT (Structdescriptor, Connection, ADT]). Also, nested constructed ADT objects are required if there is a nested nesting. Finally, the ADT object can be specified by PreparedStatement SetObject method.
When reading the data, it is reversed with the above method: if it is a simple type, read directly, or if it is ADT, use ResultSet's GetObject (), then cast to struct, and then call struct () method to obtain Object[] Type data, as recursion.

2, Sqldataexample.java and Employeeobj.java
This example is similar to 1 and also handles ADT, with the difference that the type is not nested.
In comparison, 2:1 of the code is a lot simpler, but it pays the price: abstract a class employeeobj for type EMPLOYEE that implements the SqlData interface and overrides three methods (required).
The call to the foreground is much simpler than the second method in 1, simply by specifying the ADT object directly using the PreparedStatement SetObject method (though you need to specify its type as Oracletypes.struct). You can also use the Oracleresultset GetObject () directly when reading and cast to Employeeobj objects.
What can be said is: There will be lost!!
In addition, there are several places to note in this example:
Import oracle.jdbc2.* in the 2.1 Employeeobj.java; Changed into import oracle.jdbc.*; The reason is unknown;
The Dictionary map = Conn.gettypemap () in 2.2 Sqldataexample.java; Change to Java.util.Map Map = Conn.gettypemap (); Reason: Note:this class (refers to dictionary) is obsolete. New implementations should implement the Map interface, rather than extendidng this class. (Source: Javadoc);
The Pstmt.executequery () in the 2.3 Sqldataexample.java; Changed into Pstmt.executeupdate (); More reasonable, because this is updated rather than query (do not change will not affect the function, but recommended);

3, Customdatumexample.java and Employee.java
This example is exactly the same as 2. The difference is to adopt another kind of abstraction technology, realize the interface customdatum and Customdatumfactory, and rewrite two methods Todatum () and create (). There is a slight difference when accessing data in the foreground: the Oracleresultset getcustomdatum () method is used and forced into employee. From the outward appearance, the SqlData interface in 2 exists in the java.sql.* package, while the interface customdatum and customdatumfactory exist in the oracle.sql.* package, which can be considered as Oracle Company's special implementation of its own products. Perhaps with higher performance, smaller overhead? However, 3 is not as good as the other direct, personally think.

4, Arrayexample.java
As you can see from the filename, this example shows the Varray type.
With 1, there are also two ways to insert. One can use SQL directly, in addition, the Oraclepreparedstatement SetArray method can be used to construct the array process and construct STRUCT. The reading process of the data is reversed by using the Oracleresultset GetArray () method to get the array object, calling the GetArray () method of the object and casting it to an array of objects, and then manipulating the array.

5, Personref.java and Studentref.java
I don't know if Oracle is joking with us, these two files are the same except for the class name.
This example shows us the concept of the object table. The insertion of the object table is not any different from the normal table, the reading of the data is first reflected on its SQL, the REF function is required, and then the ResultSet GetObject () method is called and coerced into a ref object, and the object's GetValue () is used. and convert it to a struct object, then use the object's GetAttributes () method to get object[], and then manipulate the array.

6, Refclient.java and Genref.java
This sample demonstrates the using REF over two different Sessions.
Class Genref is used to encapsulate the type and its binary content that a ref object points to. The program materialize into Genref and then de-materialize the ref from Genref in another session, which is the same as working with ref: Use the GetValue () of this object and convert it to a struct object, and then use the object The GetAttributes () method gets object[], and then you can manipulate the 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 the database. Data insertion requires the use of a function bfilename, which requires a directory object that needs to be created using the Create directory Mydir (this user needs to have the Create any directory system permissions). I have a big lesson in this place: Follow the steps, you can always prompt me to read without this directory!! Finally, I'll capitalize the first argument of the Bfilename function (that is, the directory name). Reason: The directory name is case-sensitive in the function, and although we created it in lowercase, it automatically becomes uppercase when it is in the database.
When reading a bfile type of data, you first get the binary input stream through its getbinarystream () method, and then manipulate the input stream. The limitation is your imagination.
Plsql_fileexample.java implements the same functionality as above, except that all operations against bfile are done by calling Pl/sql anonymous blocks and using Oraclecallablestatement. If not necessary, use the former.

8, Lobexample.java and Plsql_lobexample.java
This example uses the CLOB and BLOB data types.
When inserting data, the binary output stream and the character output stream need to be obtained separately via Getbinaryoutputstream () and Getcharacteroutputstream (), and then the operation for this output stream is the same as the traditional Java I/O.
When reading data, the binary input stream and the character input stream need to be obtained separately through Getbinarystream () and Getcharacterstream (), and then the operation for this input stream is the same as the traditional Java I/O.
Plsql_lobexample.java implements the same functionality as above, except that all operations against CLOB and BLOBs are done 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.

I'm only giving an explanation of the tiny part of the demo that Oracle gave us and adding my own ideas. I also want to pass this article, played a role in the hope that the vast number of users of the product samples for analysis, so that the most authentic code.

Your opinion, I would like to listen!! Email:zbdlmxc@163.com


Related Article

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.