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 - ojdbc5_g.jar - ojdbc5dms.jar - ojdbc5dms_g.jar - ojdbc6.jar - ojdbc6_g.jar - ojdbc6dms.jar - ojdbc6dms_g.jar Note: The dms versions of the jar files are the same as [ORACLE_HOME]/jdbc/doc/javadoc.tar contains the JDBC Javadoc [ORACLE_HOME]/jdbc/demo/demo.tar contains sample JDBC programs. [ORACLE_HOME]/jlib/orai18n.jar For the Windows platform: [ORACLE_HOME]\bin directory contains ocijdbc11.dll and For non-Windows platforms: [ORACLE_HOME]/lib directory contains libocijdbc11.so, |
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的更多相关文章
- centos7远程安装oracle11g R2详细教程-解决一切问题
相关链接与资源: sqldevelper(各种操作系统的oracle客户端) http://www.oracle.com/technetwork/cn/developer-tools/sql-deve ...
- Oracle11g R2客户端安装图文详解过程
转: Oracle11g R2客户端安装图文详解过程 2018-06-17 13:30:26 大话JAVA的那些事 阅读数 4129更多 分类专栏: Oracle 版权声明:本文为博主原创文章,遵 ...
- 写给小白的JAVA链接MySQL数据库的步骤(JDBC):
作为复习总结的笔记,我罗列了几个jdbc步骤,后边举个简单的例子,其中的try块请读者自行处理. /* * 1.下载驱动包:com.mysql.jdbc.Driver;网上很多下载资源,自己找度娘,此 ...
- Oracle11g R2创建PASSWORD_VERIFY_FUNCTION对应密码复杂度验证函数步骤
Oracle11g R2创建PASSWORD_VERIFY_FUNCTION对应密码复杂度验证函数步骤 运行测试环境:数据库服务器Oracle Linux 5.8 + Oracle 11g R2数据库 ...
- Win7 32位安装Oracle11g R2 图解示例
Win7 32位操作系统安装Oracle11g R2 图解示例.废话不说了,直接上图. 1.下载的两个oracle 11gR2压缩包解压到单独的文件夹中. 2.找到解压的database文件夹中的Se ...
- Oracle11g R2创建PASSWORD_VERIFY_FUNCTION相应password复杂度验证函数步骤
Oracle11g R2创建PASSWORD_VERIFY_FUNCTION相应密码复杂度验证函数步骤 运行測试环境:数据库服务器Oracle Linux 5.8 + Oracle 11g R2数据库 ...
- ORACLE11g R2【RAC+ASM→单实例FS】
ORACLE11g R2[RAC+ASM→单实例FS] 11g R2 RAC+ASMà单实例FS的DG,建议禁用OMF. 本演示案例所用环境: primary standby OS Hostnam ...
- ORACLE11g R2【单实例 FS→单实例FS】
ORACLE11g R2[单实例 FS→单实例FS] 本演示案例所用环境: primary standby OS Hostname pry std OS Version RHEL6.5 RHEL6 ...
- ORACLE11g R2【RAC+ASM→RAC+ASM】
ORACLE11g R2[RAC+ASM→RAC+ASM] 本演示案例所用环境:RAC+ASM+OMF primary standby OS Hostname node1,node2 dgnode ...
随机推荐
- python学习,day3:函数式编程
调用函数来实现文件的修改(abc.txt),并增加上时间,调用的是time模块, 需要注意的是,每个函数一定要用‘’‘ ‘’’ 标注下函数说明 # coding=utf-8 # Author: RyA ...
- More Effective C++ 35个改善方法
美·Scott Meyers 侯捷 More Exceptional C++: 40 New Engineering Puzzles, Programming Problems, and Soluti ...
- JavaScript 流程控制器
已知有流程step1.step2.step3.step4.step5 , 如何控制输出下面过程 例如: 1:step1.step2.step3.step2.step3.step4.step5 2:st ...
- ifconfig command not found
CentOS minimal 命令做了修改 可以运行 ip addr
- lucene原理
lucene查找原理: https://yq.aliyun.com/articles/581877
- 读取obj文件用Mesh创建实例化
using UnityEngine; using System.Collections; using System.IO; using System.Collections.Generic; usin ...
- spring-boot启动后在浏览器打开指定页面
来自:https://stackoverflow.com/questions/27378292/launch-browser-automatically-after-spring-boot-webap ...
- Nginx的Permission denied错误
Nginx的Permission denied错误 环境: CentOS7 问题描述 今天搭建了nginx+uwsgi+django的环境,之后通过浏览器访问遇到下面问题: 2017/03/31 19 ...
- Ubuntu14.04搭建Oracle instantClient 11.2.0.4并配置cx_Oracle5.1.2
一.配置Oracle instantClient 11.2.0.4 1.下载Oracle客户端: 打开http://www.oracle.com/technetwork/topics/linuxx86 ...
- webHttpBinding
[ServiceContract] public interface IHanger { /// <summary> /// 根据请求的用户的所属的组织,决定工单发布到哪个吊挂产线 /// ...