复制内容到剪贴板
代码:
Connection conn=
DriverManager.getConnection
("jdbc:oracle:thin:@dlsun511:1521:ora1","scott","tiger");
| | |
machine(ip@) : port# : sid
JDBC OCI:
复制内容到剪贴板
代码:
Connection conn=
DriverManager.getConnection
("jdbc:oracle:oci8[9]:@RAC","scott","tiger");
|
Net Service
JDBC THIN与JDBC THIN对比:
相同之处:
The JDBC Thin, JDBC OCI, and JDBC Server drivers all provide the same functionality. They all support the following standards and features:
* JDBC 2.0
* Partial JDBC 3.0 (in JDBC driver version 9.2)
* the same syntax and APIs
* the same Oracle extensions
至于不同之处是一个表格,不好上传,大家自己总结吧!!
主要是JDBC OCI 接口比JDBC THIN接口效率高!
How does one connect with the JDBC Thin Driver?
The the JDBC thin driver provides the only way to access Oracle from the Web (applets). It is smaller and slower than the OCI drivers.
import
java.sql.*;
复制内容到剪贴板
代码:
class dbAccess {
public static void main (String args []) throws SQLException
{
DriverManager.registerDriver (
new oracle.jdbc.driver.OracleDriver()
);
Connection conn = DriverManager.getConnection
("jdbc:oracle:thin:@dbhost:1521:ORA1", "scott", "tiger");
// @machine:port:SID, userid, password
Statement stmt = conn.createStatement();
ResultSet rset = stmt.executeQuery (
"select BANNER from SYS.V_$VERSION"
);
while (rset.next())
System.out.println (rset.getString(1)); // Print col 1
stmt.close();
}
}
How does one connect with the JDBC OCI Driver?
One must have Net8 (SQL*Net) installed and working before attempting to use one of the OCI drivers.
复制内容到剪贴板
代码:
import java.sql.*;
class dbAccess {
public static void main (String args []) throws SQLException
{
try {
Class.forName ("oracle.jdbc.driver.OracleDriver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
Connection conn = DriverManager.getConnection
("jdbc:oracle:oci8:@ORA1", "scott", "tiger");
// or oci9 @Service, userid, password
Statement stmt = conn.createStatement();
ResultSet rset = stmt.executeQuery (
"select BANNER from SYS.V_$VERSION"
);
while (rset.next())
System.out.println (rset.getString(1)); // Print col 1
stmt.close();
}
}
How does one connect with the JDBC KPRB Driver?
One can obtain a handle to the default or current connection (KPRB driver) by calling the OracleDriver.defaultConenction() method. Please note that you do not need to specify a database URL, username or password as you are already connected to a database session. Remember not to close the default connection. Closing the default connection might throw an exception in future releases of Oracle.
import
java.sql.*;
复制内容到剪贴板
代码:
class dbAccess {
public static void main (String args []) throws SQLException
{
Connection conn = (new
oracle.jdbc.driver.OracleDriver()).defaultConnection();
Statement stmt = conn.createStatement();
ResultSet rset = stmt.executeQuery (
"select BANNER from SYS.V_$VERSION"
);
while (rset.next())
System.out.println (rset.getString(1)); // Print col 1
stmt.close();
}
}
与JAVA有关的初始化参数:======================================
Executing initjvm.sql also highlights some new initsid.ora parameters that are used to support Java in your Oracle8i database. These parameters, their descriptions, and the settings required for running initjvm.sql, are all shown in the following list:
?
SHARED_POOL_SIZE? Defines the size of your shared pool in bytes. This should be set to at least 50MB to run initjvm.sql.
?
JAVA_POOL_SIZE? Defines the size of the Java pool, a new area of the SGA in Oracle8i used to store shared Java objects. This should be set to 50MB when running initjvm.sql, but can be as low as 20MB for normal use
of Java stored procedures.
?
JAVA_SOFT_SESSIONSPACE_LIMIT? Identifies a soft limit on memory used by Java in a session. The default is 1MB. If this limit is exceeded, a warning is written to the ALERT log.
?
JAVA_MAX_SESSIONSPACE_SIZE? Identifies the maximum amount of memory that can be used by a Java procedure; the default is 4GB. When the limit set by this parameter is exceeded, the executing Java procedure is killed by Oracle8i automatically.
如果将JAVA程序存放在数据库中,并运行存储在数据库中的JAVA程序,则数据库中会启用JAVA的虚拟机,为了保证JAVA虚拟机有效的运行,你需要设置上面介绍的参数。
如何将一个JAVA程序装载到数据库并且发布出去?===================================
就像前面说得,
java程序或类可以被存储到数据库中,作为PL/SQL的替换或补充。Java可以被用来作为数据库的触发器、存储过程、函数、对象的成员函数。在按照下面的过程开发完
java存储过程后,就可以从SQL或PL/SQL中调用JAVA存储过程,就像调用普通的PL/SQL过程一样。下面的代码描述了如何在SQL*PLUS中开发和使用一个 输出"Hello, World" 的JAVA程序的例子:
1. Write the Java program using a Java development environment like Jdeveloper or JBuilder.
2. Load the Java program into Oracle8i using either the create or replace
java source command, or with the LOADJAVA utility.
3. Publish your Java procedure to SQL. This step identifies your Java
procedure to SQL and PL/SQL by exposing the procedure entry point,
mapping datatypes in Java to PL/SQL or SQL, and indicating
parameter-passing between Java and PL/SQL or SQL.
(1)编写java程序
---可以直接在SQL*PLUS中创建JAVA的源文件,当然如果有已经编译好的
java class,则可以直接跳过这一步,直接到将
java程序发布出去这一步
SQL> -- first, create the Java source code
SQL> create or replace
java source named "Hello" as
public class Hello {
static public String Message(String name) {
return "Hello, " + name;
}
}
/
Java created.
(2)发布java程序
SQL> -- Now, publish it to SQL
SQL> create or replace function hello (name VARCHAR2) return VARCHAR2
as language
java name
'Hello.Message (
java.lang.String) return
java.lang.String';
Function created.
(3)使用发布的JAVA程序
SQL> -- Now, you can use the Java procedure from a SQL statement
SQL> select hello('world!') from dual;
HELLO('world!')
---------------
Hello world!
--- hello函数在8i中不支持中文,9i中支持。如:
SQL> select hello('你好!') from dual;
HELLO('你好!')
------------------
Hello, 你好!
至于其它的例子,大家可以看$ORACLE_HOME/jdbc/demo.zip文件,该文件中有利用JDBC OCI与JDBC THIN接口的各种例子。