Eclipse搭建Ruby开发环境
1.环境搭建
1.1JDK安装配置
1)点击下面网址下载JDK5.0:
http://java.sun.com/javase/downloads/index_jdk5.jsp
2)下载完成后,双击图标进行安装,安装过程中选择安装目录(例如:C:jdk1.5.0_11)
3)配置JDK环境变量:
(1)右击"我的电脑",点击"属性",选择"高级"选项卡,点击"环境变量",出现环境变量属性窗口如下:
图1
(2)建第一个系统变量:JAVA_HOME值:C:jdk1.5.0_11
图2
(3)建第二个系统变量:CLASSPATH值:.;%JAVA_HOME%lib
图3
(4)修改系统变量PATH值,在原有值前加上%JAVA_HOME%bin;
图4
备注:
必须加到PATH值最前面;
(5)测试j2sdk的安装与环境变量设置是否正确:
在命令提示符中输入:java-version
图5
1.2Eclipse安装配置
1)下载Eclipse及插件:
Eclipse3.2.2下载地址:
http://www.eclipse.org/downloads/download.php?file=/eclipse/downloads/drops/R-3.2.2-200702121330/eclipse-SDK-3.2.2-win32.zip
Eclipse3.2.2多国语言包下载地址:
http://www.eclipse.org/downloads/download.php?file=/eclipse/downloads/drops/L-3.2.1_Language_Packs-200609210945/NLpack1-eclipse-SDK-3.2.1-win32.zip
RDT(RubyDevelopmentTool)下载地址:
http://downloads.sourceforge.net/rubyeclipse/org.rubypeople.rdt-0.8.0.604272100PRD.zip
2.配置Eclipse:
(1)将下载的eclipse-SDK-3.2.2-win32.zip解压到C:eclipse
(2)将下载的多国语言包解压到C:eclipselanguage
注意:
language目录下有一个eclipse子目录,然后才是features、plugins目录;
(3)建立语言包的link文件:在C:eclipse目录下新建一个目录并取名为links,在该目录中新建一个文本文件并取名为language.link(扩展名必须为.link),然后在language.link文件中加一行文字:
path=C:eclipselanguage
目录结构如下:
图6
(4)将下载的RDT文件解压到C:eclipse对应的features、plugins目录;
(5)运行C:eclipseeclipse.exe,界面如下:
图7
2、Ruby及插件安装配置
a)下载RUBY及插件
ruby下载地址:
http://rubyforge.org/frs/download.php/18566/ruby186-25.exe
ruby-dbi插件下载地址:
http://rubyforge.org/frs/download.php/12368/dbi-0.1.1.tar.gz
ruby-oci8插件下载地址:
http://rubyforge.org/frs/download.php/16630/ruby-oci8-1.0.0-rc1.tar.gz
http://rubyforge.org/frs/download.php/16631/ruby-oci8-1.0.0-rc1-mswin32.rb
watir下载地址:
http://rubyforge.org/frs/download.php/5677/watir-1.4.1.exe
watir中文补丁下载地址:
http://rubyforge.org/tracker/download.php/104/489/3232/440/watir-cn.zip
b)安装配置:
i.下载Ruby后,双击进行安装,安装过程中选择安装目录(例如:C:ruby);
ii.下载Watir后,双击进行安装,安装过程中选择安装目录(例如:C:ProgramFilesWatir)
iii.下载dbi-0.1.1.tar.gz后,解压后运行目录下的setup.rb;
iv.下载ruby-oci8-1.0.0-rc1.tar.gz后,解压后运行目录下的setup.rb;
v.运行ruby-oci8-1.0.0-rc1-mswin32.rb;
vi.将watir-cn.rb拷贝到C:rubylibrubysite_ruby1.8目录;
c)配置Ruby解释器
(1)打开Eclipse主界面,右击"我的电脑",点击"窗口",选择"首选项",进入对话框中"Ruby>InstalledInterpreters"入口,如下:
图8
(2)点击"add",在对话框中输入信息后,如下:
图9
(3)点击"确定",完成配置。
2.Ruby操作Oracle
2.1connect
require'dbi'
dbh=DBI.connect('DBI:OCI8:HOST','NAME','PASSWORD')
dbh.disconnect
备注:
HOST:oracle服务器IP或主机名
NAME:登录Oracle的用户名
PASSWORD:登录Oracle的密码
2.2CreateTable
require'dbi'
dbh=DBI.connect('DBI:OCI8:HOST','NAME','PASSWORD')
dbh.do("CREATETABLEstates(
idCHAr(2)PRIMARYKEY,
nameVARCHAr2(15)NOTNULL,
capitalVARCHAr2(25)NOTNULL)")
dbh.disconnect
2.3Insert
方法一:
require'dbi'
dbh=DBI.connect('DBI:OCI8:HOST','NAME','PASSWORD')
sqlInsert="INSERTINTOstates(id,name,capital)
VALUES('AL','Alabama','Birmingham')"
dbh.commit
dbh.disconnect
方法二:
require'dbi'
dbh=DBI.connect('DBI:OCI8:HOST','NAME','PASSWORD')
sqlInsert="INSERTINTOstates(id,name,capital)
VALUES(?,?,?)"
dbh.do(sqlInsert,"AL","Alabama","Birmingham")#参数化
dbh.do(sqlInsert,"AZ","Arizona","Phoenix")
dbh.commit
dbh.disconnect
2.4Update
方法一:
require'dbi'
dbh=DBI.connect('DBI:OCI8:HOST','NAME','PASSWORD')
dbh.do("UPDATEstatesSETid='ME'WHERename='Maine'")
dbh.commit
dbh.disconnect
方法二:
require'dbi'
dbh=DBI.connect('DBI:OCI8:HOST','NAME','PASSWORD')
sqlCapitalsUpdate="UPDATEstatesSETcapital=?WHEReid=?"
dbh.do(sqlCapitalsUpdate,"Montgomery","AL")#参数化
dbh.do(sqlCapitalsUpdate,"Harrisburg","PA")#参数化
dbh.do("UPDATEstatesSETid='ME'WHERename='Maine'")
dbh.commit
dbh.disconnect
2.5Select
require'dbi'
dbh=DBI.connect('DBI:OCI8:HOST','NAME','PASSWORD')
rs=dbh.prepare('SELECT*FROMstates')
rs.execute
whilersRow=rs.fetchdo
prsRow#输出结果
#Alternativeoutput:putsrsRow
#Alternativeoutput:pprsRow
end
rs.finish
dbh.disconnect
备注:
rsRow[Column][Row]:查询结果的地Column行第Row列值
如:rsRow[0][0]
rsRow[0][0]:第1行第1列值,值为:AL
输出结果如下:
图10