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