- 浏览: 684565 次
- 性别:
- 来自: 中山
文章分类
最新评论
-
wuhuizhong:
jFinal支持Rest风格吗?可以想spring mvc那样 ...
在JFinal的Controller中接收json数据 -
wuhuizhong:
在jfinal中应如何获取前端ajax提交的Json数据?ht ...
在JFinal的Controller中接收json数据 -
wuhuizhong:
jfinal如何处理json请求的数据:问题: 在某些api接 ...
在JFinal的Controller中接收json数据 -
wuhuizhong:
Ubuntu14.04 安装 Oracle 11g R2 Ex ...
Oracle 11g release 2 XE on Ubuntu 14.04 -
alanljj:
这个很实用,已成功更新,谢过了!
odoo薪酬管理模块l10n_cn_hr_payroll
This article explains how to use the JRuby programming language with Oracle Database 11g Release 2 . JRuby allows the Java platform and extensive libraries to be augmented with an easy to use and powerful scripting language.
Software Requirements
The following software can be used:
Oracle Database 11g Release 2 | Oracle Database Software Downloads |
JDK 1.6 or later | Java SE Downloads |
JRuby 1.5.6 | jruby.org |
Oracle Database 11g Release 2 JDBC Driver | Download ojdbc6.jar from Oracle Database 11g Release 2 JDBC Drivers . Drivers for other versions of Oracle or the JDK can be downloaded from the JDBC Driver Downloads page. |
Sample Code | The example code can be downloaded here . |
The examples will also work with some other versions.
Install each component using its recommended install instructions.
Demonstration Setup
In a terminal window set the Oracle environment and verify Oracle's standard demonstration Human Resources (HR) schema is installed:
$ sqlplus hr/welcome [. . .] SQL> select table_name from user_tables; TABLE_NAME ------------------------------ COUNTRIES JOBS EMPLOYEES LOCATIONS DEPARTMENTS JOB_HISTORY REGIONS
If the schema is not available, see Installing the HR Schema
in the Oracle Database Sample Schemas 11g
Release 2 (11.2)
guide.
Verify JDK 1.6 is installed:
$ java -version java version "1.6.0_23" Java(TM) SE Runtime Environment (build 1.6.0_23-b05) Java HotSpot(TM) 64-Bit Server VM (build 19.0-b09, mixed mode)
Set JRUBY_HOME to the directory where JRuby is installed and verify JRuby runs:
$ export JRUBY_HOME=$HOME/jruby-1.5.6 $ export PATH=$PATH:$JRUBY_HOME/bin $ jruby -v jruby 1.5.6 (ruby 1.8.7 patchlevel 249) (2010-12-03 9cf97c3) (Java HotSpot(TM) 64-Bit Server VM 1.6.0_23) [amd64-java]
Copy the Oracle JDBC driver ojdbc6.jar into the $JRUBY_HOME/lib
directory:
$ ls -l $JRUBY_HOME/lib total 20084 -rw-r--r-- 1 pas usergrp 8102598 Dec 3 08:58 jruby.jar drwxr-xr-x 13 pas usergrp 4096 Dec 3 08:57 native -rw-r--r-- 1 pas usergrp 2152051 Feb 7 17:34 ojdbc6.jar drwxr-xr-x 6 pas usergrp 4096 Dec 3 08:58 ruby
If JDK 1.5 is installed then copy ojdbc5.jar instead.
Running the Demonstration
The sample code shows several different data operations using JRuby and Oracle Database.
The jruby_plsql.sql file creates a PL/SQL package for showing how to call PL/SQL from JRuby:
-- jruby_plsql.sql create or replace package emp_pack as function get_employee_name (p_employee_id in number) return varchar2; procedure delete_department (p_department_id in number); end emp_pack; / show errors; create or replace package body emp_pack as function get_employee_name (p_employee_id in number) return varchar2 is l_name varchar2(46) := null; begin select first_name || ' ' || last_name into (l_name) from employees where employee_id = p_employee_id; return l_name; end get_employee_name; procedure delete_department (p_department_id in number) is begin delete from departments where department_id = p_department_id; commit; end delete_department; end emp_pack; / show errors;
The package can be installed using SQL*Plus:
$ sqlplus hr/welcome SQL> @jruby_plsql.sql Package created. No errors. Package body created. No errors.
Verify installation by explicitly calling the package:
SQL> select emp_pack.get_employee_name(199) from dual; EMP_PACK.GET_EMPLOYEE_NAME(199) ----------------------------------- Douglas Grant
The file jdbc_connection.rb contains code for connecting to the database using the Oracle JDBC driver.
It creates a new JRuby class OracleConnection that exposes attributes of DriverManager which is responsible for connecting to Oracle via the registered subprotocol OracleDriver.
# jdbc_connection.rb require 'java' java_import 'oracle.jdbc.OracleDriver' java_import 'java.sql.DriverManager' class OracleConnection @conn = nil def initialize (user, passwd, url) @user = user @passwd = passwd @url = url # Load driver class oradriver = OracleDriver.new DriverManager.registerDriver oradriver @conn = DriverManager.get_connection url, user, passwd @conn.auto_commit = false end # Add getters and setters for all attrributes we wish to expose attr_reader :user, :passwd, :url, :connection def close_connection() @conn.close() unless @conn end def prepare_call(call) @conn.prepare_call call end def create_statement() @conn.create_statement end def prepare_statement(sql) @conn.prepare_statement sql end def commit() @conn.commit end def self.create(user, passwd, url) conn = new(user, passwd, url) end def to_s "OracleConnection [user=#{@user}, url=#{@url}]" end alias_method :to_string, :to_s end
Edit test_oracle.rb to define the HR schema username,
password and connection string. This example shows the database is
running on the local machine with the service name of orcl.
The file shows four common uses of SQL and PL/SQL.
# test_oracle.rb require 'jdbc_connection' # Edit these for your database schema user = "hr " passwd = "welcome " url = "jdbc:oracle:thin:@localhost/orcl " # Helpers to call our PL/SQL package PLSQL_BLOCK_FUNCTION_CALL = <<EOF begin :1 := emp_pack.get_employee_name(:2); end; EOF PLSQL_BLOCK_PROCEDURE_CALL = <<EOF begin emp_pack.delete_department(:1); end; EOF print "Run at #{Time.now} using JRuby #{RUBY_VERSION}\n\n" insert_stmt, select_stmt, rest, delete_stmt, function_stmt = nil begin conn = OracleConnection.create(user, passwd, url) # Display connection using the to_s method of OracleConnection puts conn, "\n" # Insert a new Department puts "1. Inserting department 281 into the DEPARTMENTS table" insert_sql = "insert into departments values (:1, :2, NULL, NULL)" insert_stmt = conn.prepare_statement(insert_sql) insert_stmt.set_int 1, 281 insert_stmt.set_string 2, "JRuby Department" insert_stmt.execute conn.commit # Select all departments puts "2. Showing all DEPARTMENTS rows" select_sql = "select department_id, department_name from departments order by 1" select_stmt = conn.create_statement rset = select_stmt.execute_query select_sql while (rset.next) puts " Department [#{rset.getInt(1)}, #{rset.getString(2)}]" end # Call a PL/SQL package function to display an employee name puts "3. Finding name of employee 200" function_stmt = conn.prepare_call PLSQL_BLOCK_FUNCTION_CALL function_stmt.set_int 2, 200 function_stmt.register_out_parameter 1, 12 function_stmt.execute_update puts " Employee 200 is #{function_stmt.get_string 1}\n" # Call a PL/SQL package procedure to delete a department record puts "4. Deleting department 281 from the DEPARTMENTS table" delete_stmt = conn.prepare_call PLSQL_BLOCK_PROCEDURE_CALL delete_stmt.set_int 1, 281 delete_stmt.execute_update rescue puts "\n** Error occured **\n" puts "Failed executing Oracle demo from JRuby ", $!, "\n" ensure # We have to ensure everything is closed here. if (!insert_stmt.nil?) insert_stmt.close end if (!select_stmt.nil?) select_stmt.close end if (!rset.nil?) rset.close end if (!delete_stmt.nil?) delete_stmt.close end if (!function_stmt.nil?) function_stmt.close end conn.close_connection end print "\nEnded at #{Time.now}\n"
After connecting, the file uses the exposed OracleConnection attributes to show these database operations:
- Inserting a new department 281. The department identifier and name are bound, allowing efficient use of database resources.
- Querying all department records.
- Calling a PL/SQL function to find an employee name. The name is returned as an "OUT" parameter of the anonymous PL/SQL block in PLSQL_BLOCK_FUNCTION_CALL.
- Deletion of the newly created department 281 by calling a PL/SQL procedure.
Run the test program with JRuby:
$ jruby test_oracle.rb Run at Fri Feb 11 14:54:25 -0800 2011 using JRuby 1.8.7 OracleConnection [user=hr, url=jdbc:oracle:thin:@localhost/orcl] 1. Inserting department 281 into the DEPARTMENTS table 2. Showing all DEPARTMENTS rows Department [10, Administration] Department [20, Marketing] Department [30, Purchasing] Department [40, Human Resources] Department [50, Shipping] Department [60, IT] Department [70, Public Relations] Department [80, Sales] Department [90, Executive] Department [100, Finance] Department [110, Accounting] Department [120, Treasury] Department [130, Corporate Tax] Department [140, Control And Credit] Department [150, Shareholder Services] Department [160, Benefits] Department [170, Manufacturing] Department [180, Construction] Department [190, Contracting] Department [200, Operations] Department [210, IT Support] Department [220, NOC] Department [230, IT Helpdesk] Department [240, Government Sales] Department [250, Retail Sales] Department [260, Recruiting] Department [270, Payroll] Department [281, JRuby Department] 3. Finding name of employee 200 Employee 200 is Jennifer Whalen 4. Deleting department 281 from the DEPARTMENTS table Ended at Fri Feb 11 14:54:32 -0800 2011
If the test is changed to insert department 100 instead of 281, an error stack will be displayed:
** Error occurred ** Failed executing Oracle demo from JRuby java.sql.SQLIntegrityConstraintViolationException: ORA-00001: unique constraint (HR.DEPT_ID_PK) violated
Congratulations, you have just learned how to access an Oracle 11g Release 2 database using JRuby!
发表评论
-
用函数unistr将Oracle数据库中的Unicode转换为中文
2016-07-19 11:51 7943例子: DECLARE V_EXT_DES V ... -
ORACLE APPLICATION EXPRESS 5.0 升级
2016-05-12 11:43 594Oracle11GR2 XE 缺省是安装了oracle ap ... -
Oracle ACL(Access Control List)
2016-05-12 11:36 899在oralce 11g中假如你想获取server的ip或者h ... -
了解systemstate dump
2016-04-26 14:09 497当数据库出现严重的性能问题或者hang了的时候,我们非常需要 ... -
通过ORACLE的UTL_HTTP工具包发送包含POST参数的请求
2016-03-18 16:25 5171DECLARE req utl_http. ... -
Shell: extract more from listener.log(分析监听日志)
2016-03-16 14:57 1164统计一天内每小时的session请求数 # fgrep ... -
ORA-01031: insufficient privileges 问题解决笔记
2016-02-01 15:53 1195A) File $Oracle_HOME/network/a ... -
listener.log中报Warning: Subscription For Node Down Event Still Pending问题的解决方法
2016-01-07 16:34 1641一套Oracle 10.2.0.1 for aix的数据库环 ... -
Oracle触发器和MySQL触发器之间的区别
2015-11-19 12:55 684Oracle触发器格式: CREATE [OR RE ... -
查询正在执行的存储过程
2015-11-13 09:27 20631、找正在执行的PROCEDURE的 sid ,serial# ... -
undo表空间损坏的处理过程
2015-10-14 13:49 1227磁碟陣列故障,分區/rman上包括undo和archivel ... -
登录oracle资料库时很久无反应的问题处理一例
2015-10-11 10:56 1005原因是系统存在僵死的进程,促使session处于激活状态.首 ... -
TNS-12560问题解决
2015-10-01 19:52 626tnsping远程主机实例出现TNS-12560: TNS ... -
查看undo中sql语句的占用情况
2015-08-06 17:18 1784查看undo中sql语句的占用情况 select * ... -
Install Open System Architect And ODBC Instant Client
2015-05-21 14:03 760How to Install Open System Arc ... -
恢复oracle中用pl sql误删除drop掉的表
2015-04-03 16:12 564查看回收站中表 select object_name,or ... -
在Oracle Linux 6.6上安装Oracle 10gR2
2015-01-15 15:36 2698查看硬體配置 # df -h Filesystem ... -
kill
2015-01-03 11:36 469--根据某一对象查询进程 col owner fo ... -
Oracle 数据库Storage存储迁移笔记
2014-12-27 11:08 9971.确认数据文件、控制文件、临时文件、日志文件 位置 / ... -
異地備份資料庫的開啟步驟
2014-11-19 14:03 496使用EMC設備執行異地備份, 資料庫的複製是開啟的狀態下, ...
相关推荐
ruby连接oracle驱动包,支持32位与64位操作系统 Developing applications ...Build Ruby and Ruby on Rails applications using the ruby-oci8 driver or JRuby with the Oracle Enhanced Adapter for ActiveRecord.
Deploying with JRuby: Deliver Scalable Web Apps using the JVM
**JRuby 1.7.8 版本详解** JRuby 是一个开源的、高性能的 Ruby 解释器,它能够将 Ruby 代码编译成 Java 字节码,从而在 Java 虚拟机(JVM)上运行。JRuby 的出现使得 Ruby 开发者能够利用 JVM 的强大功能和丰富的库...
标题“jruby-src-1.0”指的是JRuby的一个特定版本——1.0的源代码。JRuby是一种实现Ruby编程语言的Java版本,它允许在Java平台上运行Ruby代码,同时也支持与Java应用程序进行无缝集成。这个压缩包“jruby-src-1.0....
标题 "org.jruby.rar" 暗示我们正在讨论一个与 JRuby 相关的压缩文件,其中可能包含了 JRuby 的源代码、库文件或者其他相关资源。JRuby 是一种 Java 实现的 Ruby 解释器,它允许 Ruby 代码在 JVM(Java 虚拟机)上...
**JRuby 安装与配置指南** JRuby 是一个基于 Java 平台的 Ruby 实现,它允许开发者利用 Ruby 语言的优势同时享受 Java 生态系统的强大功能。在本文中,我们将详细探讨如何安装和配置 JRuby,特别是针对 JRuby 1.1.2...
JRuby是Java平台上的一个实现,它允许开发者在Java虚拟机(JVM)上运行Ruby编程语言。这个“jruby-bin-1.3.1.zip”压缩包是JRuby的一个早期版本,专为Ruby语言的研发提供必要的工具和环境。在这个版本中,重点是兼容...
jruby安装包 和 jruby的lib库 JRuby是面向Ruby、基于Java虚拟机(JVM)的一种解释程序,它结合了Ruby语言的简易性和功能强大的JVM的执行机制,包括与Java库 全面集成。Rails彻底加快及简化了Web应用的开发,不过它让...
### JRuby在Java测试中的应用 #### JRuby简介 JRuby是一种使Ruby语言能在Java平台上运行的实现方式。它不仅保留了Ruby语言简洁且强大的特性,还充分利用了Java平台的强大功能,如性能优化、垃圾回收机制等。由于...
《jRuby on Rails WEB2.0》:将Ruby on Rails融入Java平台的实践指南 《jRuby on Rails WEB2.0》是一部由Ola Bini撰写的书籍,深入探讨了如何将Ruby on Rails这一敏捷开源框架与Java平台相结合,以构建高效、灵活的...
**JRuby:Ruby on Windows的完美融合** JRuby是一个开源的、基于Java平台的Ruby语言实现,它允许开发者在Ruby环境中使用Java类库,并且可以让Java应用程序无缝地调用Ruby代码。标题中的“jruby_windows_1_6_4安装...
2. **Java平台**:Java平台由Sun Microsystems(现在归Oracle所有)开发,包括Java虚拟机(JVM)、Java类库和开发工具。JVM允许开发者编写一次代码,到处运行,具有跨平台的特性。 3. **JRUBY**:JRUBY 是 Ruby 的...
在IT行业中,尤其是在Java与Ruby混合开发的场景中,JRuby是一个重要的工具,它允许开发者在Java平台上运行Ruby代码。由于JRuby是基于JVM(Java Virtual Machine)的,因此可以利用Java生态中的各种工具来对其进行...
It is expected to be compatible with Ruby 2.3.x and stay in sync with C Ruby. JRuby 9.1.15.0 is our latest release… Major features of JRuby 9000: Ruby 2.x compatibility A new optimizing runtime ...
jruby-1.3.1.jar包 解决org.jruby.exceptions.JumpException 等报错
JRuby的版本"jruby-jruby-7bee00d"可能是一个特定的Git提交版本,这表明它是一个开发版本,可能包含了最新的功能和改进。通常,这样的版本会包含一些实验性的特性,对于开发者来说,他们可以提前试用新功能并提供...
jruby-complete-1.7.19.jar,java版本的ruby解析器
标题 "jruby-1.5.5+OperaWatir+RSpec" 暗示了这是一个关于使用 JRuby 1.5.5 版本、OperaWatir 和 RSpec 进行自动化测试的项目或者资源集合。现在,我们将深入探讨这三个关键组件以及它们在 IT 领域中的应用。 JRuby...