Java链接 Oracle11g R2

菜鸟学习Oracle数据库,使用Java代码链接数据库。

首先要配置Eclipse,在新建的工程中,Package Explorer->工程名->Build path->Add external archives->Oracle安装盘X:\app\admin\product\11.2.0\dbhome_1\jdbc\lib\ojdbc*.jar

ojdbc的jar包的选择参照readme文件。


For all platforms:

[ORACLE_HOME]/jdbc/lib contains:

- ojdbc5.jar
    Classes for use with JDK 1.5.  It contains the JDBC driver
    classes, except classes for NLS support in Oracle Object and
    Collection types.

- ojdbc5_g.jar
    Same as ojdbc5.jar, except that classes were compiled with
    "javac -g" and contain tracing code.

- ojdbc5dms.jar
    Same as ojdbc5.jar, except that it contains instrumentation to
    support DMS and limited java.util.logging calls.

- ojdbc5dms_g.jar
    Same as ojdbc5_g.jar, except that it contains instrumentation to
    support DMS.

- ojdbc6.jar
    Classes for use with JDK 1.6. It contains the JDBC driver classes
    except classes for NLS support in Oracle Object and Collection
    types.

- ojdbc6_g.jar
    Same as ojdbc6.jar except compiled with "javac -g" and contains
    tracing code.

- ojdbc6dms.jar
    Same as ojdbc6.jar, except that it contains instrumentation to
    support DMS and limited java.util.logging calls.

- ojdbc6dms_g.jar
    Same as ojdbc6_g.jar except that it contains instrumentation to
    support DMS.

Note: The dms versions of the jar files are the same as
    standard jar files, except that they contain additional code
    to support Oracle Dynamic Monitoring Service. They contain a
    limited amount of tracing code. These can only be used
    when dms.jar is in the classpath. dms.jar is provided as part of
    Oracle Application Server releases. As a result the dms versions
    of the jar files can only be used in an Oracle Application Server
    environment.

[ORACLE_HOME]/jdbc/doc/javadoc.tar contains the JDBC Javadoc
  for the public API of the public classes of Oracle JDBC. This
  JavaDoc is the primary reference for Oracle JDBC API extensions. The
  Oracle JDBC Development Guide contains high level discussion of
  Oracle extensions. The details are in this JavaDoc. The JavaDoc is
  every bit as authorative as the Dev Guide.

[ORACLE_HOME]/jdbc/demo/demo.tar contains sample JDBC programs.

[ORACLE_HOME]/jlib/orai18n.jar
    NLS classes for use with JDK 1.5, and 1.6.  It contains
    classes for NLS support in Oracle Object and Collection types.
    This jar file replaces the old nls_charset jar/zip files. In
    Oracle 10g R1 it was duplicated in [ORACLE_HOME]/jdbc/lib. We
    have removed the duplicate copy and you should now get it from
    its proper location.

For the Windows platform:

[ORACLE_HOME]\bin directory contains ocijdbc11.dll and
  heteroxa11.dll, which are the libraries used by the JDBC OCI
  driver.

For non-Windows platforms:

[ORACLE_HOME]/lib directory contains libocijdbc11.so,
  libocijdbc11_g.so, libheteroxa11.so and libheteroxa11_g.so, which
  are the shared libraries used by the JDBC OCI driver.

Java代码如下:

package Connection.Oracle;

import java.sql.*;

public class java_ConnectOracle_jdb {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        try {
            // 加载驱动
            Class.forName("oracle.jdbc.driver.OracleDriver");
            // 得到连接
            Connection ct = DriverManager.getConnection(
                    "jdbc:oracle:thin:@127.0.0.1:1521:orcl", "scott", "123456");
            // Connection
            // ct=DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:实例名(SID)",
            // "用户名", "密码");
            Statement sm = ct.createStatement();
            ResultSet rs = sm.executeQuery("select * from salgrade");
            System.out.println("          Grade-Losal-Hisal");
            while (rs.next()) {
                System.out.println("Salgrade: " + rs.getString(1) + " - "
                        + rs.getString(2) + " - " + rs.getString(3));
            }
            rs.close();
            sm.close();
            ct.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

效果图如下:

JDBC的使用:

Some Useful Hints In Using the JDBC Drivers
-------------------------------------------

Please refer to "JDBC Developer‘s Guide and Reference" for details
regarding usage of Oracle‘s JDBC Drivers.  This section only offers
useful hints.  These hints are not meant to be exhaustive.

These are a few simple things that you should do in your JDBC program:

 1. Import the necessary JDBC classes in your programs that use JDBC.
    For example:

      import java.sql.*;
      import java.math.*; // if needed

    To use OracleDataSource, you need to do:
      import oracle.jdbc.pool.OracleDataSource;

 2. Create an OracleDataSource instance. 

      OracleDataSource ods = new OracleDataSource();

 3. set the desired properties if you don‘t want to use the
    default properties. Different connection URLs should be
    used for different JDBC drivers.

      ods.setUser("my_user");
      ods.setPassword("my_password");

    For the JDBC OCI Driver:
      To make a bequeath connection, set URL as:
      ods.setURL("jdbc:oracle:oci:@");

      To make a remote connection, set URL as:
      ods.setURL("jdbc:oracle:oci:@<database>");

      where <database> is either a TNSEntryName
      or a SQL*net name-value pair defined in tnsnames.ora.

    For the JDBC Thin Driver, or Server-side Thin Driver:
      ods.setURL("jdbc:oracle:thin:@<database>");

      where <database> is either a string of the form
      //<host>:<port>/<service_name>, or a SQL*net name-value pair,
      or a TNSEntryName.

    For the JDBC Server-side Internal Driver:
      ods.setURL("jdbc:oracle:kprb:");

      Note that the trailing ‘:‘ is necessary. When you use the
      Server-side Internal Driver, you always connect to the
      database you are executing in. You can also do this:

      Connection conn =
        new oracle.jdbc.OracleDriver().defaultConnection();

 4. Open a connection to the database with getConnection()
    methods defined in OracleDataSource class.

      Connection conn = ods.getConnection();

-----------------------------------------------------------------------
时间: 2024-11-09 12:08:55

Java链接 Oracle11g R2的相关文章

ORACLE11g R2【RAC+ASM→单实例FS】

ORACLE11g R2[RAC+ASM→单实例FS] 11g R2 RAC+ASMà单实例FS的DG,建议禁用OMF. 本演示案例所用环境:   primary standby OS Hostname node1,node2 std OS Version RHEL6.5 RHEL6.5 DB Version 11.2.0.4 11.2.0.4 db_name stephen stephen db_unique_name stephen standby service_names stephen

ORACLE11g R2【单实例 FS→单实例FS】

ORACLE11g R2[单实例 FS→单实例FS] 本演示案例所用环境:   primary standby OS Hostname pry std OS Version RHEL6.5 RHEL6.5 DB Version 11.2.0.4 11.2.0.4 db_name stephen stephen db_unique_name stephen standby service_names stephen standby instance_name stephen standby Pri

ORACLE11g R2【RAC+ASM→RAC+ASM】

ORACLE11g R2[RAC+ASM→RAC+ASM] 本演示案例所用环境:RAC+ASM+OMF   primary standby OS Hostname node1,node2 dgnode1,dgnode2 OS Version RHEL6.5 RHEL6.5 DB Version 11.2.0.4 11.2.0.4 db_name stephen stephen db_unique_name stephen standby service_names stephen standby

ORACLE11g R2 RAC+DG FOR OEL6.5安装实施报告

虚拟机系统 ORACLE11g R2 RAC+DG FOR OEL6.5安装实施报告 目录 文档控制    1 一.开启归档(主库RAC)    1 二.开启强制logging(主库RAC)    1 三.记录信息(主库RAC)    1 四.修改hosts文件(rac ,dg上的操作)    1 五.配置监听和tns    1 六.创建参数文件(RAC.备库)    1 七.使用备份做DG    1 八.恢复备库    1 九.使用duplicate在线方式创建备库    1 十.增加stan

Linux下安装Oracle11G R2详解

环境说明: 在Vmware虚拟机上搭建的环境, 操作系统: Centos7.0 数据库版本:Oracle11G R2 系统内存:3G 硬盘:40G 安装过程中,也遇到了几个问题,此处不详细说明,下一篇专门总结遇到的问题及解决方法. 安装准备: 修改主机名 # sed -i "s/HOSTNAME=localhost.localdomain/HOSTNAME=Oracletest/" /etc/sysconfig/network # hostname Oracletest 添加主机名与I

Java链接数据库

建立数据库和Java链接 public void setConnection() {  try {   Class.forName("org.gjt.mm.mysql.Driver");   con = DriverManager.getConnection("jdbc:mysql://localhost:3306/mytest?characterEncoding=utf-8", "root", "1234");  } cat

Oracle11g R2 silent安装

一.安装虚拟机 [[email protected] ~]# pwd /root 二.安装Oracle11g R2依赖 安装Oracle依赖很多.幸好也rpm直接安装好依赖 http://www.oracle.com/technetwork/cn/articles/servers-storage-admin/ginnydbinstallonlinux6-1845247-zhs.html 1.root用户创建repo [[email protected] ~]# cd /etc/yum.repos

Linux下安装Oracle11G R2过程中遇到的问题

Linux下安装Oracle11G R2过程中遇到了几个问题,希望对大家有所帮助. 如何在linux图形化安装oracle?? 解决方法和思路: 1)可以通过安装VNC服务. 2)如果你在服务器离你很近,你直接在桌面下安装即可.(前提是linux安装了图形化桌面程序) 3)通过Xmanager Enterprise直接远程调用桌面来安装. 我采用第三种 2.   Oracle界面乱码,出现各种方格子?? 原因及解决方法:系统语言环境及字体问题. 可以安装中易宋体18030,可以解决乱码问题:或者

MyEclipse中Java链接MySQL

MyEclipse中Java链接MySQL.先解压JDBC的jar文件.配置环境. 下面我写了段Java程序加载JDBC驱动连接MySQL的代码 GetConn.java package mysql; import java.sql.*; public class GetConn {    public Connection conn = null;                        //创建Connection对象    public Connection getConnectio