菜鸟学习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(); -----------------------------------------------------------------------

Java链接 Oracle11g R2的更多相关文章

  1. centos7远程安装oracle11g R2详细教程-解决一切问题

    相关链接与资源: sqldevelper(各种操作系统的oracle客户端) http://www.oracle.com/technetwork/cn/developer-tools/sql-deve ...

  2. Oracle11g R2客户端安装图文详解过程

    转: Oracle11g R2客户端安装图文详解过程 2018-06-17 13:30:26 大话JAVA的那些事 阅读数 4129更多 分类专栏: Oracle   版权声明:本文为博主原创文章,遵 ...

  3. 写给小白的JAVA链接MySQL数据库的步骤(JDBC):

    作为复习总结的笔记,我罗列了几个jdbc步骤,后边举个简单的例子,其中的try块请读者自行处理. /* * 1.下载驱动包:com.mysql.jdbc.Driver;网上很多下载资源,自己找度娘,此 ...

  4. Oracle11g R2创建PASSWORD_VERIFY_FUNCTION对应密码复杂度验证函数步骤

    Oracle11g R2创建PASSWORD_VERIFY_FUNCTION对应密码复杂度验证函数步骤 运行测试环境:数据库服务器Oracle Linux 5.8 + Oracle 11g R2数据库 ...

  5. Win7 32位安装Oracle11g R2 图解示例

    Win7 32位操作系统安装Oracle11g R2 图解示例.废话不说了,直接上图. 1.下载的两个oracle 11gR2压缩包解压到单独的文件夹中. 2.找到解压的database文件夹中的Se ...

  6. Oracle11g R2创建PASSWORD_VERIFY_FUNCTION相应password复杂度验证函数步骤

    Oracle11g R2创建PASSWORD_VERIFY_FUNCTION相应密码复杂度验证函数步骤 运行測试环境:数据库服务器Oracle Linux 5.8 + Oracle 11g R2数据库 ...

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

    ORACLE11g R2[RAC+ASM→单实例FS] 11g R2 RAC+ASMà单实例FS的DG,建议禁用OMF. 本演示案例所用环境:   primary standby OS Hostnam ...

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

    ORACLE11g R2[单实例 FS→单实例FS] 本演示案例所用环境:   primary standby OS Hostname pry std OS Version RHEL6.5 RHEL6 ...

  9. ORACLE11g R2【RAC+ASM→RAC+ASM】

    ORACLE11g R2[RAC+ASM→RAC+ASM] 本演示案例所用环境:RAC+ASM+OMF   primary standby OS Hostname node1,node2 dgnode ...

随机推荐

  1. 进阶篇:4)面向装配的设计DFA总章

    本章目的:理解装配的重要性,明确结构工程师也要对装配进行设计. 1.基础阅读 ①进阶篇:1)DFMA方法的运用: ②需要一台FDM3d打印机:请查看 基础篇:8)结构设计装备必备: 2.为什么要学习D ...

  2. 小蒟蒻的垃圾emacs配置

    (global-set-key [f9] 'compile-file) (global-set-key [f10] 'gud-gdb) (global-set-key (kbd "C-s&q ...

  3. DedeCMS实现自定义表单提交后发送指定QQ邮箱的方法

    如月cruyue在做DedeCMS自定义表单发送邮箱的教程,发现大部分都是在php文件里写死固定字段内容,这样虽然也能实现自定义表单提交后发送指定邮箱,但是很不智能,如月cruyue想要一个我们自定义 ...

  4. 微信 vue中使用video.js播放m3u8视频,解决安卓自动全屏的问题。

    最近一个项目中需要在微信中播放m3u8格式的视频,刚开始用了 vue-video-player 这个插件,在IOS手机体验良好,本以为完事了, 结果安卓手机一点播放就自动全屏,心态略崩.查了资料说是安 ...

  5. rocketmq的消息过滤-sql方式

    通常我们会使用Tag过滤 特殊情况下我们也可以使用userproperties+TAGS过滤 , sql92定义 这两种都是在服务器端完成过滤, 对于超大数据量的场景(1小时4000W+)不要在客流端 ...

  6. nginx应用

    windows: 启动:start nginx 退出:nginx -s quit 检查配置是否正确:nginx -t -c ./conf/nginx.conf 查看是否在运行:tasklist /fi ...

  7. erlang随笔3--OTP

    OTP最核心的概念就是行为.一个行为封装了某种常见的行为模式.可以把这些行为理解为某种应用程序框架.可以通过回调模块来 定制这些框架.OTP依靠行为引用了容错,扩容和动态代码升级等特性.所以在写回调模 ...

  8. 腾讯地图添加多个Marker

    //重置地图 init(){ var self = this; this.wSize = { wHeight: window.innerHeight-, wWidth: window.innerWid ...

  9. Python练习 | Web本质Socket

    #--------------------------------客户端-------------------------------------- # 导入socket库 import socket ...

  10. 与native交互时会出现的问题

    1.jsbridge:  可以用jsbridge与native交互,这属于第三方库,前端后端都需要加jsbridge 2.可以直接调用原生的方法,ios:  window.webkit.message ...