- 浏览: 668458 次
- 性别:
- 来自: 中山
文章分类
最新评论
-
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 7879例子: DECLARE V_EXT_DES V ... -
ORACLE APPLICATION EXPRESS 5.0 升级
2016-05-12 11:43 544Oracle11GR2 XE 缺省是安装了oracle ap ... -
Oracle ACL(Access Control List)
2016-05-12 11:36 848在oralce 11g中假如你想获取server的ip或者h ... -
了解systemstate dump
2016-04-26 14:09 457当数据库出现严重的性能问题或者hang了的时候,我们非常需要 ... -
通过ORACLE的UTL_HTTP工具包发送包含POST参数的请求
2016-03-18 16:25 5082DECLARE req utl_http. ... -
Shell: extract more from listener.log(分析监听日志)
2016-03-16 14:57 1111统计一天内每小时的session请求数 # fgrep ... -
ORA-01031: insufficient privileges 问题解决笔记
2016-02-01 15:53 1153A) File $Oracle_HOME/network/a ... -
listener.log中报Warning: Subscription For Node Down Event Still Pending问题的解决方法
2016-01-07 16:34 1603一套Oracle 10.2.0.1 for aix的数据库环 ... -
Oracle触发器和MySQL触发器之间的区别
2015-11-19 12:55 618Oracle触发器格式: CREATE [OR RE ... -
查询正在执行的存储过程
2015-11-13 09:27 19911、找正在执行的PROCEDURE的 sid ,serial# ... -
undo表空间损坏的处理过程
2015-10-14 13:49 1188磁碟陣列故障,分區/rman上包括undo和archivel ... -
登录oracle资料库时很久无反应的问题处理一例
2015-10-11 10:56 963原因是系统存在僵死的进程,促使session处于激活状态.首 ... -
TNS-12560问题解决
2015-10-01 19:52 581tnsping远程主机实例出现TNS-12560: TNS ... -
查看undo中sql语句的占用情况
2015-08-06 17:18 1710查看undo中sql语句的占用情况 select * ... -
Install Open System Architect And ODBC Instant Client
2015-05-21 14:03 717How to Install Open System Arc ... -
恢复oracle中用pl sql误删除drop掉的表
2015-04-03 16:12 529查看回收站中表 select object_name,or ... -
在Oracle Linux 6.6上安装Oracle 10gR2
2015-01-15 15:36 2602查看硬體配置 # df -h Filesystem ... -
kill
2015-01-03 11:36 430--根据某一对象查询进程 col owner fo ... -
Oracle 数据库Storage存储迁移笔记
2014-12-27 11:08 9431.确认数据文件、控制文件、临时文件、日志文件 位置 / ... -
異地備份資料庫的開啟步驟
2014-11-19 14:03 452使用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.
Now you can bring the best of Ruby into the world of Java, with Using JRuby. Come to the source for the JRuby core team’s insights and insider tips. You’ll learn how to call Java objects seamlessly ...
JRuby is a fast, scalable, and powerful JVM language with all the benefits of a traditional Ruby environment. JRuby deployments have fewer moving parts and consume less memory than traditional Ruby. ...
Deploying with JRuby: Deliver Scalable Web Apps using the JVM
jruby是一个ruby的解析器,可将ruby解析为java文件,jruby作为ruby解析器时刻调用java代码
jruby安装包 和 jruby的lib库 JRuby是面向Ruby、基于Java虚拟机(JVM)的一种解释程序,它结合了Ruby语言的简易性和功能强大的JVM的执行机制,包括与Java库 全面集成。Rails彻底加快及简化了Web应用的开发,不过它让...
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-src-1.0.zip
jruby-1.3.1.jar包 解决org.jruby.exceptions.JumpException 等报错
本文介绍了JRuby的安装与配置。JRuby的安装需要JRuby以及JDK,本文使用的版本分别是JRuby 1.1.2以及Java 1.6。至于JRuby配置,文章主要介绍了测试JRuby安装的方法。
jruby-complete-1.7.19.jar,java版本的ruby解析器
NetBeans Ruby and Rails IDE with JRuby 2009,大家看标题就知道了啊。。好东西啊,刚得到就发上来了。。
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 ...
org.jruby.rar
jruby-bin-1.3.1.zip 用于jruby语言研发的必要工具
JRuby window X64 开发软件
jRuby On Rails WEB 2.0
JRuby语言实战技术,java下运用ruby
jruby 1.9.6 win64
JRuby在Java测试中的应用,使用脚本语言,高效快进行Java程序测试。