Everyone who uses JDBC even Oracle will know such a fact: we need the library file classs12.zip exists in the $ ORACLE_HOME / JDBC / LIB directory (but still some rookie will ask this library file every forum, it is hard) But few people know that Oracle has an example of using JDBC, which exists in $ Oracle_Home / JDBC / DEMO / DEMO.ZIP. A few days ago, I learned Oracle's OOP technology and was very touched. At that time I thought: There must be some OO technology in JDBC to support it. I haven't found a suitable example for a long time, and finally find it in the Oracle installation directory. It is true that it is: walking through the iron shoes, you have to come to the family.
Agreement: 1. If java.lang.unsatisfiedlinker: do_open appears, you need to modify the URL of the drivermanager.getconnection () method to JDBC: Oracle: Thin: @ 127.0.0.1: 1521: ORADB, specific reasons 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 with classs12.zip);
Below I will make a detailed function description of the files under Folder / Samples / Oci8 / Object-Samples: 1. PersonObject.java This example demonstrates that there is ADT field Empid in the table people, which is Person, and the type Person There is ADT field Home, which is Address, and type Address is an ADT. If a regular SQL statement is used, its insert statement is not different in SQL / PLUS, namely: Using Constructor Nested Construction. Another method is used to construct a Struct object using the structural constructor struct (structdscriptor, connection, object []), an ADT object. At the same time, if there is nested, it is necessary to nearest the constructive ADT object. Finally, the ADT object can be specified by the setObject method of PreparedStatement. When reading data, it is used to reverse the above method: if it is a simple type, read directly; if it is ADT, use the resultSet's getObject (), then force to convert to Struct, then call the getAttributes () method of the Struct Object [] type data, such as recursive.
2, SQLDATAEXAMPLE.JAVA and Employeeobj.java This example is similar to 1, but also the processing of ADT, the different type is not nested. In contrast, the code of 2 to 1 is simple, but it also pays the price: for the type Employee abstract a class EMPLOYEOBJ, which implements the SQLDATA interface and rewrites three methods (must). The second method in the front desk is more concise, only need to specify the ADT object directly using PreparedStatement's setObject method (but you need to specify its type Oracletypes.struct). You can also use the oracleresultset's getObject () and force to convert to an EmployeEOBJ object. It is true that there must be lost !! Another place to pay attention to: 2.1 Import oracle.jdbc2 in Employeeobj.java. *; Change to import oracle.jdbc. *; Reasons unknown; 2.2 Dictionary map = conn.gettypemap () in sqldataexample.java; change to java.util.map map = conn.gettypeMap (); reason: Note: This class (referring to Dictionary) is Obsolete. New Implementations Should Implement The Map Interface, Rather Than Extendidng this class. (Source: javadoc); 2.3 pstmt.executeQuery (); change to pstmt.executeUpdate (); more reasonably, because this is updated rather than query (no change Will affect the function, just suggestions); 3, CustomDatumexample.java and Employee.java This example is exactly the same. Different from another abstract technology and implements interface CustomDatum and CustomDatumFactory, and override two methods Todatum () and Create (). There is also a little different when accessing data at the front desk: The getCustomDatum () method of ORACleResultSet is used and converted to EMPLOYEE. From the appearance, 2 in the SQLDATA interface exists in the java.sql. * Pack; and the interface CustomDatum and CustomDatumFactory are in Oracle.SQL. * Pack, which can be considered to be dedicated to their own products. Maybe there is a higher performance, smaller overhead? However, 3 is not as direct, individuals think.
4. Arrayexample.java can be seen from the file name that this example demonstrates a Varray type. With 1, there are two ways to insert. A SQL can be used directly; in addition, it can use the setArray method for OraclePrepareDStatement, and the process of constructing Array is not different from the construction Struct. The reading process of the data is reversed: First use the oracleresultset's getArray () method to get the Array object, then call the GetArray () method of this object and force to convert to an object array and then operate to this array.
5, PersonRef.java and StudientRef.java I don't know if Oracle is joking with us. The other is the same in addition to the class name. This example gives us a concept of object table. The insertion of the object table is not different from the normal table. The data reads first embodied on its SQL. You need to use the REF function, then call the resultTSet's getObject () method and force to convert to the REF object, then use this object getValue () And convert to a Struct object, then use the GetAttributes () method of this object to get Object [], then operate to this array. 6, Refclient.java and GenRef.java this Sample Demonstrate Using Ref over Two Different Sessions. Class GenRef is used to encapsulate the types of the REF objects and their binary content. The program first Materialize Into genref, then DE-matialize ref from Genref in another session, the same as the processing Ref: use this object's getValue () and converted into a struct object, and then use this object's GetAttributes () method to get Object [], Then operate this array.
7, FileExample.java and PLSQL_FILEEXAMPLE.JAVA This example uses bfile data type, Contains a Locator to a large binary file stored outside the database. When the data is inserted, the function BFileName is required. This function requires a directory object, which needs to be created with CREATE DIRECTORY MyDir (this user needs to have Create Any Directory system permissions). I have a lesson in this place: I don't have this directory when I do it. I have not prompted this directory when reading!! Finally, I have written the first parameter of the BFileName function (ie, the directory name). Cause: The directory name is case sensitive in the function. Although we use lowercase when we create it, it can be automatically turned to the database. When reading the BFile type data, first get a binary input stream through its getBinaryStream () method, and then operate this input stream. The Limitation IS your image. The functionality implemented by the PLSQL_FILEXAMPLE.JAVA is the same as the above, but all the operations for BFile are done by calling the PL / SQL anonymous block and uses OracleCalLABLESTATEMENT. If there is no need, use the former.
8, Lobexample.java and PLSQL_LOBEXAMPLE.JAVA This example uses the CLOB and BLOB data types. When inserting data, you need to get the binary output stream and character output stream via GetBinaryoutputStream () and getCharacteroutputStream (), which is the same for this output stream, which is the same as the traditional Java I / O. When reading data, it is necessary to obtain binary input streams and character input streams via getBinaryStream () and getCharacterStream (), and then the operation of this input stream is the same as the traditional Java I / O. The functionality implemented by the PLSQL_LOBEXAMPLE.JAVA is the same as described above, but all the operations for Clob and BLOB are done by calling the PL / SQL anonymous block and uses OracleCalLABLESTATEMENT. If there is no need, use the former. Note: DBMS_LOB packages are used in the second example of 7, 8. Above I only give explanations for the minor part of the Demo we provide, and join my own view. I also want to pass this article, playing the role of bricking jade, I hope that the majority of netizens will analyze the examples of the product, so that the original code is obtained. Your opinion, I am willing to listen !! email: zbdlmxc@163.com