JRuby, ActiveRecord, JDBC to SQL Server
I recently had to figure out a good way to export data in XML form
from a SQL Server database. I searched high and low for a good
ActiveRecord adapter written in pure Ruby to talk to SQL Server.
Unfortunately, the setup and overhead to get the right ODBC driver, DSN
configs, and all those funky libraries to work properly on any
*Nix-based machine were adding up way too quickly and complicating
matters. So back to the drawing board.
RESTful service from .NET – no
Way too much work for such a simple task that only runs but once a
week. Beside anything Microsoft is my nemesis and I am not even going to
put my toe in that water.
SQL Server bcp export – no.
The bottom line – writing code in your database is always bad news. Sure the bcp export is the quick and dirty, but it might be too
quick and dirty. It can make upgrades and migrations almost impossible.
Just think how difficult it would be to migrate from SQL Server to say
MySQL if the use of the bcp utility compounds over time. More
importantly, how do you test it?
JDBC – yes!
Really? I was kind of shocked to learn that Microsoft jumped on the
Java bandwagon and wrote a JDBC adapter. Woo-hoo! Wait…I don’t want to
write a bunch of junky DAO’s in Java.
Enter – my good friends JRuby and ActiveRecord.
I can write sweet sugary Ruby code while utilizing the power of any
or all of the existing Java libraries – which so happens to include
JDBC!
So in order to glue this together it is quite simple.
- First install JRuby, it’s simple and there are 2 million blog posts
about how to do this, all you need is Java pre-installed on your
machine. Download it here from here.
- jruby> gem install activerecord
- jruby> gem install activerecord-jdbc-adapter
- Download the SQL Server JDBC driver JAR, I recommend getting version 2 or higher
- activerecord-jdbc-adapter is a nifty JRuby specific gem that acts as
an adapter for ActiveRecord to speak JDBC (yet another thanks to Nick
Sieger).
Once your have these things installed you can write a simple Ruby
class to establish the connection and define any number of ActiveRecord
classes to map to the tables you’re after. Here is a small snippet on
how easy it is to connect to SQL Server using this technique.
#assuming this file is inline with your sqljdbc.jar or just put it into your $JRUBY_HOME/lib directory
require 'rubygems'
require 'active_record'
require 'active_record/connection_adapters/jdbc_adapter'
require 'sqljdbc4.jar'
ActiveRecord::Base.establish_connection(
:adapter => "jdbc",
:username => "XXXX",
:password => "XXXX",
:driver => "com.microsoft.sqlserver.jdbc.SQLServerDriver",
:url=> "jdbc:sqlserver://Server;databaseName=XXXX"
)
class Tclient < ActiveRecord::Base
set_table_name "CLIENT"
end
puts Tclient.column_names()
Approximately 10 lines of actual code and you’re done. It can’t be
done any more elegant or simple than that. Here is the really beauty
part, you can actually test your “script”. Use RSpec or TestUnit, but it
is tested! Let cron or whatever scheduling mechanism of your choice run
your script with confidence
分享到:
相关推荐
ActiveRecord-JDBC-Adapter提供对以下各项的完全或几乎完全支持: MySQL , PostgreSQL , SQLite3和MSSQL *(SQLServer)。 除非我们获得更多的贡献,否则我们将不会支持更多的适配器。 请注意,获得另一个适配器...
这是一个示例应用程序,试图让JRuby与ActiveRecord 6和Postgres一起运行。 我的配置适用于MRI,因此不是配置。 错误#1与版本60.2适配器 版本: Rails 6.0.3.4 activerecord-jdbcpostgresql-adapter(60.2-java) ...
**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语言的研发提供必要的工具和环境。在这个版本中,重点是兼容...
在IT行业中,尤其是在Java与Ruby混合开发的场景中,JRuby是一个重要的工具,它允许开发者在Java平台上运行Ruby代码。由于JRuby是基于JVM(Java Virtual Machine)的,因此可以利用Java生态中的各种工具来对其进行...
2. JRuby 可以与 Java 类库无缝集成,利用 Java 平台的强大功能,如 JMX、JDBC 和其他企业级框架。 3. JRuby 支持使用 JRuby-Rack 运行 Rails 应用,可以在 Java 应用服务器上部署 Ruby on Rails 项目。 4. JRuby 还...
jruby安装包 和 jruby的lib库 JRuby是面向Ruby、基于Java虚拟机(JVM)的一种解释程序,它结合了Ruby语言的简易性和功能强大的JVM的执行机制,包括与Java库 全面集成。Rails彻底加快及简化了Web应用的开发,不过它让...
《JRuby和Rails-让Ruby语言融入于Java项目》是一本深入探讨如何在Java环境中使用JRuby和Rails框架的教程。这本书旨在帮助开发者理解和实践如何将Ruby on Rails的灵活性和生产力引入到Java企业级开发中,从而提升软件...
【标题】"2009 KungFu Rails大会JRuby演讲PPT"涉及的主要知识点是JRuby和Rails框架的应用及结合。KungFu Rails大会是一个专注于Ruby on Rails技术的活动,而JRuby则是Ruby的一个重要实现,它允许开发者在Java平台上...
**JRuby:Ruby on Windows的完美融合** JRuby是一个开源的、基于Java平台的Ruby语言实现,它允许开发者在Ruby环境中使用Java类库,并且可以让Java应用程序无缝地调用Ruby代码。标题中的“jruby_windows_1_6_4安装...
《jRuby on Rails WEB2.0》:将Ruby on Rails融入Java平台的实践指南 《jRuby on Rails WEB2.0》是一部由Ola Bini撰写的书籍,深入探讨了如何将Ruby on Rails这一敏捷开源框架与Java平台相结合,以构建高效、灵活的...
JRuby的版本"jruby-jruby-7bee00d"可能是一个特定的Git提交版本,这表明它是一个开发版本,可能包含了最新的功能和改进。通常,这样的版本会包含一些实验性的特性,对于开发者来说,他们可以提前试用新功能并提供...
jruby-complete-1.7.19.jar,java版本的ruby解析器