ORACLE自带的JDBC源代码解析
约定: 1、如果出现 java.lang.UnsatisfiedLinkError: do_open,则你需要把 DriverManager.getConnection() 方法的 url 修改成 jdbc:oracle:thin:@127.0.0.1:1521:oradb,具体原因未知; 2、如果出现 java.sql.SQLException: 不支持的字符集: oracle-characte
约定:1、如果出现 java.lang.UnsatisfiedLinkError: do_open,则你需要把 DriverManager.getConnection() 方法的 url 修改成 jdbc:oracle:thin:@127.0.0.1:1521:oradb,具体原因未知;
2、如果出现 java.sql.SQLException: 不支持的字符集: oracle-character-set-852,则你需要把 nls_charset12.zip加入你的工程中(此文件与 classes12.zip 同目录);
下面我就把文件夹\samples\oci8\object-samples下的文件做一个详细的功能描述:
1、PersonObject.java
这个例子演示了表 people 中存在ADT字段 empid,其类型为 PERSON,而且类型 PERSON中存在ADT字段 home,其类型为 ADDRESS,而且类型 ADDRESS是一个ADT。
如果使用常规SQL语句,其插入语句与在sql/plus中无异,即:使用构造函数嵌套构造。
另有一种方法,使用 STRUCT 的构造函数 STRUCT(StructDescriptor, Connection, Object[]) 构造出一个STRUCT对象,即一个ADT对象。同时,如果有嵌套则需要嵌套构造ADT对象。最后通过 PreparedStatement的 setObject方法指定ADT对象即可。
读取数据时则采用与上述方法相逆的办法:如果是简单类型,则直接读取;如果是ADT,则使用ResultSet的getObject(),再强制转换成STRUCT,然后调用STRUCT的getAttributes()方法取得 Object[] 类型数据,如是递归。
2、SQLDataExample.java与EmployeeObj.java
此例与1中相似,也是对ADT的处理,不同的是类型没有嵌套。
比较而言,2比1的代码简洁了很多,不过也是付出了代价:为类型 EMPLOYEE 抽象出一个类EmployeeObj,它实现了SQLData接口,并重写了三个方法(必须的)。
前台的调用比1中的第二种方法简洁了很多,只需要直接使用PreparedStatement的 setObject方法指定ADT对象即可(不过需要指定其类型为OracleTypes.STRUCT)。读取时也可直接使用OracleResultSet的getObject()并强制转换成EmployeeObj对象即可。
真可谓是:有得必有失!!
另,此例中有几处需要注意的地方:
2.1 EmployeeObj.java中的 import oracle.jdbc2.*; 改成 import oracle.jdbc.*; 原因未知;
2.2 SQLDataExample.java 中的 Dictionary map = conn.getTypeMap(); 改成 java.util.Map map = conn.getTypeMap(); 原因:NOTE: This class(指的是Dictionary) is obsolete. New implementations should implement the Map interface, rather than extendidng this class.(来源:javadoc);
2.3 SQLDataExample.java 中的 pstmt.executeQuery(); 改成 pstmt.executeUpdate(); 更合理些,因为这是更新而非查询(不改也不会影响功能,只是建议);
3、CustomDatumExample.java与Employee.java
此例与2完全相同。不同的是采用了另外一种抽象技术,并实现了接口CustomDatum 与 CustomDatumFactory,并重写了二个方法toDatum()与create()。在前台访问数据时亦有少许不同:采用了OracleResultSet的getCustomDatum()方法并把它强制转换成Employee。从外观上看,2中SQLData接口存在于 java.sql.* 包中;而接口CustomDatum 与 CustomDatumFactory则存在于oracle.sql.*包中,可以认为是Oracle公司对自己产品的专门实现。或许有更高的性能、更小的开销?不过3不如2来得直接,个人认为。
4、ArrayExample.java
从文件名可看出,此示例演示的是VARRAY类型。
同1,插入亦有两种方法。一种可直接使用SQL;另外,可使用OraclePreparedStatement的setARRAY方法,构造ARRAY的过程与构造 STRUCT 无异。数据的读取过程与此相反:先使用OracleResultSet的getARRAY()方法取得ARRAY对象,再调用此对象的getArray()方法并强制转换成对象数组,然后对此数组操作即可。
5、PersonRef.java与StudentRef.java
不知是不是ORACLE与我们开玩笑,这两个文件除了类名不同外,其余一切相同。
这个例子给我们演示的是对象表的概念。对象表的插入与普通表没有任何不同,数据的读取首先体现在其SQL上,需要使用REF函数,然后调用ResultSet的getObject()方法并强制转换成REF对象,再利用此对象的getValue()并转换成STRUCT对象,再利用此对象 的getAttributes()方法得到Object[],然后对此数组操作即可。
6、RefClient.java与GenREF.java
This sample demonstrates using REF over two different Sessions.
类GenREF用来封装REF对象所指向的类型及其二进制内容。程序先Materialize into GenREF,然后在另一会话中De-materialize REF from GenREF,下面就与处理REF相同:利用此对象的getValue()并转换成STRUCT对象,再利用此对象 的getAttributes()方法得到Object[],然后对此数组操作即可。
7、FileExample.java与PLSQL_FileExample.java
此示例使用 BFILE 数据类型,Contains a locator(定位器) to a large binary file stored outside the database。数据插入时需要使用函数 bfilename,这个函数需要目录对象,此对象需要使用create directory mydir来创建(此用户需具有CREATE ANY DIRECTORY系统权限)。我曾在这个地方有个大教训:按着步骤做,可在读取时总是提示我没有此目录!!最后,我把BFILENAME函数的第一个参数(即目录名)大写就好了。原因:目录名在函数中区分大小写,虽然我们创建它时用的是小写,可到了中后就自动变成了大写。
读取BFILE类型的数据时,首先通过其getBinaryStream()方法得到二进制的输入流,然后操作这个输入流就可以了。The limitation is your imagination。
PLSQL_FileExample.java实现的功能与上述的相同,只不过所有针对BFILE的操作都是通过调用pl/sql匿名块来完成的,并使用了OracleCallableStatement。如果没有必要,使用前者即可。
8、LobExample.java与PLSQL_LobExample.java
此示例使用了CLOB和BLOB数据类型。
插入数据时,需要分别通过getBinaryOutputStream()和getCharacterOutputStream()得到二进制输出流和字符输出流,然后就是针对此输出流的操作了,与传统的 java I/O 相同。
读取数据时,需要分别通过getBinaryStream()和getCharacterStream()得到二进制输入流和字符输入流,然后就是针对此输入流的操作了,与传统的 java I/O 相同。
PLSQL_LobExample.java实现的功能与上述的相同,只不过所有针对CLOB和BLOB的操作都是通过调用pl/sql匿名块来完成的,并使用了OracleCallableStatement。如果没有必要,使用前者即可。
注:7、8的第二个示例中均使用到了dbms_lob程序包。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



In-depth analysis of the role and application scenarios of HTTP status code 460 HTTP status code is a very important part of web development and is used to indicate the communication status between the client and the server. Among them, HTTP status code 460 is a relatively special status code. This article will deeply analyze its role and application scenarios. Definition of HTTP status code 460 The specific definition of HTTP status code 460 is "ClientClosedRequest", which means that the client closes the request. This status code is mainly used to indicate

To update the curl version under Linux, you can follow the steps below: Check the current curl version: First, you need to determine the curl version installed in the current system. Open a terminal and execute the following command: curl --version This command will display the current curl version information. Confirm available curl version: Before updating curl, you need to confirm the latest version available. You can visit curl's official website (curl.haxx.se) or related software sources to find the latest version of curl. Download the curl source code: Using curl or a browser, download the source code file for the curl version of your choice (usually .tar.gz or .tar.bz2

iBatis and MyBatis: Differences and Advantages Analysis Introduction: In Java development, persistence is a common requirement, and iBatis and MyBatis are two widely used persistence frameworks. While they have many similarities, there are also some key differences and advantages. This article will provide readers with a more comprehensive understanding through a detailed analysis of the features, usage, and sample code of these two frameworks. 1. iBatis features: iBatis is an older persistence framework that uses SQL mapping files.

Detailed explanation of Oracle error 3114: How to solve it quickly, specific code examples are needed. During the development and management of Oracle database, we often encounter various errors, among which error 3114 is a relatively common problem. Error 3114 usually indicates a problem with the database connection, which may be caused by network failure, database service stop, or incorrect connection string settings. This article will explain in detail the cause of error 3114 and how to quickly solve this problem, and attach the specific code

[Analysis of the meaning and usage of midpoint in PHP] In PHP, midpoint (.) is a commonly used operator used to connect two strings or properties or methods of objects. In this article, we’ll take a deep dive into the meaning and usage of midpoints in PHP, illustrating them with concrete code examples. 1. Connect string midpoint operator. The most common usage in PHP is to connect two strings. By placing . between two strings, you can splice them together to form a new string. $string1=&qu

Wormhole is a leader in blockchain interoperability, focused on creating resilient, future-proof decentralized systems that prioritize ownership, control, and permissionless innovation. The foundation of this vision is a commitment to technical expertise, ethical principles, and community alignment to redefine the interoperability landscape with simplicity, clarity, and a broad suite of multi-chain solutions. With the rise of zero-knowledge proofs, scaling solutions, and feature-rich token standards, blockchains are becoming more powerful and interoperability is becoming increasingly important. In this innovative application environment, novel governance systems and practical capabilities bring unprecedented opportunities to assets across the network. Protocol builders are now grappling with how to operate in this emerging multi-chain

Analysis of new features of Win11: How to skip logging in to a Microsoft account. With the release of Windows 11, many users have found that it brings more convenience and new features. However, some users may not like having their system tied to a Microsoft account and wish to skip this step. This article will introduce some methods to help users skip logging in to a Microsoft account in Windows 11 and achieve a more private and autonomous experience. First, let’s understand why some users are reluctant to log in to their Microsoft account. On the one hand, some users worry that they

The Linux kernel is an open source operating system kernel whose source code is stored in a dedicated code repository. In this article, we will analyze the storage path of the Linux kernel source code in detail, and use specific code examples to help readers better understand. 1. Linux kernel source code storage path The Linux kernel source code is stored in a Git repository called linux, which is hosted at [https://github.com/torvalds/linux](http
