




已阅读5页,还剩3页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
0外文原文AlmostallJavaprogramsdealwithdatainsomeway.Youhaveusedprimitivetypes,objects,arrays,linkedlists,andotherdatastructuresuptothispoint.TodayyouworkwithdatainamoresophisticatedwaybyexploringJavaDatabaseConnectivity(JDBC),aclasslibrarythatconnectsJavaprogramstorelationaldatabases.Java6includesJavaDB,asmallrelationaldatabasethatspartoftheJavaDevelopmentKit,makingiteasierthanevertoincorporateadatabaseintoyourapplications.TodayyouexploreJDBCandExtensibleMarkupLanguage(XML)inthefollowingways:1.UsingJDBCdriverstoworkwithdifferentrelationaldatabases2.AccessingadatabasewithStructuredQueryLanguage(SQL)3.ReadingrecordsfromadatabaseusingSQLandJDBC4.AddingrecordstoadatabaseusingSQLandJDBC5.CreatinganewJavaDBdatabaseandreadingitsrecordsJavaDatabaseConnectivity(JDBC)isasetofclassesthatcanbeusedtodevelopclient/serverapplicationsthatworkwithdatabasesdevelopedbyMicrosoft,Sybase,Oracle,Informix,andothersources.WithJDBC,youcanusethesamemethodsandclassesinJavaprogramstoreadandwriterecordsandperformotherkindsofdatabaseaccess.Aclasscalledadriveractsasabridgetothedatabasesourcetherearedriversforeachofthepopulardatabases.Client/serversoftwareconnectsauserofinformationwithaproviderofthatinformation,anditsoneofthemostcommonplaceformsofprogramming.YouuseiteverytimeyousurftheWeb:Awebbrowserclientrequestspages,imagefiles,andotherdocumentsusingauniformresourcelocator,orURL.Webserversprovidetherequestedinformation,ifitcanbefound,fortheclient.Oneofthebiggestobstaclesfacedbydatabaseprogrammersisthewidevarietyofdatabaseformatsinuse,eachwithitsownproprietarymethodofaccessingdata.Tosimplifyusingrelationaldatabaseprograms,astandardlanguagecalledSQL(StructuredQuery1Language)hasbeenintroduced.Thislanguagesupplantstheneedtolearndifferentdatabase-queryinglanguagesforeachdatabaseformat.JavaDB,thedatabaseincludedinJava6,supportsSQL.Indatabaseprogramming,arequestforrecordsinadatabaseiscalledaquery.UsingSQL,youcansendcomplexqueriestoadatabaseandgettherecordsyourelookingforinanyorderyouspecify.Considertheexampleofadatabaseprogrammeratastudentloancompanywhohasbeenaskedtoprepareareportonthemostdelinquentloanrecipients.TheprogrammercoulduseSQLtoqueryadatabaseforallrecordsinwhichthelastpaymentwasmorethan180daysagoandtheamountdueismorethan$0.00.SQLalsocanbeusedtocontroltheorderinwhichrecordsarereturned,sotheprogrammercangettherecordsintheorderofSocialSecuritynumber,recipientname,amountowed,oranotherfieldintheloandatabase.AllthisispossiblewithSQLtheprogrammerdoesntneedanyoftheproprietarylanguagesassociatedwithpopulardatabaseformats.SQListheindustry-standardapproachtoaccessingrelationaldatabases.JDBCsupportsSQL,enablingdeveloperstouseawiderangeofdatabaseformatswithoutknowingthespecificsoftheunderlyingdatabase.JDBCalsosupportstheuseofdatabasequeriesspecifictoadatabaseformat.TheJDBCclasslibrarysapproachtoaccessingdatabaseswithSQLiscomparabletoexistingdatabase-developmenttechniques,sointeractingwithanSQLdatabasebyusingJDBCisntmuchdifferentthanusingtraditionaldatabasetools.JavaprogrammerswhoalreadyhavesomedatabaseexperiencecanhitthegroundrunningwithJDBC.TheJDBClibraryincludesclassesforeachofthetaskscommonlyassociatedwithdatabaseusage:1.Makingaconnectiontoadatabase2.CreatingastatementusingSQL3.ExecutingthatSQLqueryinthedatabase4.ViewingtheresultingrecordsTheseJDBCclassesareallpartofthejava.sqlpackage.DatabaseDrivers2JavaprogramsthatuseJDBCclassescanfollowthefamiliarprogrammingmodelofissuingSQLstatementsandprocessingtheresultingdata.Theformatofthedatabaseandtheplatformitwaspreparedondontmatter.Thisplatform-anddatabaseindependenceismadepossiblebyadrivermanager.TheclassesoftheJDBCclasslibraryarelargelydependentondrivermanagers,whichkeeptrackofthedriversrequiredtoaccessdatabaserecords.Youllneedadifferentdriverforeachdatabaseformatthatsusedinaprogram,andsometimesyoumightneedseveraldriversforversionsofthesameformat.JavaDBincludesitsowndriver.JDBCalsoincludesadriverthatbridgesJDBCandanotherdatabase-connectivitystandard,ODBC.TheJDBC-ODBCBridgeODBC,MicrosoftscommoninterfaceforaccessingSQLdatabases,ismanagedonaWindowssystembytheODBCDataSourceAdministrator.ThisisrunfromControlPanelonaWindowssystem;togetthereonmostversionsofWindows,clickStart,Settings,ControlPanel,ODBCDataSources.OnWindowsXP,chooseStart,ControlPanel,PerformanceandMaintenanceinCategoryVieworStart,ControlPanelinClassicViewandthenchooseAdministrativeTools,DataSources(ODBC).ODBCdatasourcescanbesetupfromwithinsomedatabaseprograms.Forexample,whenanewdatabasefileiscreatedinLotusApproach,usershavetheoptionofassociatingitwithanODBCdriver.AllODBCdatasourcesmustbegivenshort,descriptivenames.Thenameisusedinside.Javaprogramswhenaconnectionismadetothedatabasethatthesourcerefersto.OnaWindowssystem,afteranODBCdriverisselectedandthedatabaseiscreated,theyshowupintheODBCDataSourceAdministrator.Figure18.2showsanexampleofthisforadatasourcenamedWorldEnergy.YourfirstprojecttodayisaJavaapplicationthatusesaJDBC-ODBCbridgetoconnecttoanAccessfile.TheAccessfileforthisprojectisworld20.mdb,adatabaseofworldenergystatisticspublishedbytheU.S.EnergyInformationAdministration.TheCoaltableinthisdatabaseincludesthreefieldsyouwillbeusingintheproject:1.Country32.Year3.AnthraciteProductionThedatabaseusedinthisprojectisincludedonthisbooksofficialwebsiteat.Tousethisdatabase,youmusthaveanODBCdriveronyoursystemthatsupportsAccessfiles.UsingtheODBCDataSourceAdministrator(orasimilarprogramifyoureonanon-Windowssystem),youmustcreateanewODBCdatasourceassociatedwithworld20.mdb.OthersetupworkmightbeneededdependingontheODBCdriverspresentonyoursystem,ifany.ConsultthedocumentationincludedwiththeODBCdriver.Afteryouhavedownloadedworld20.mdbtoyourcomputerorfoundanotherdatabasethatscompatiblewiththeODBCdriversonyoursystem,thefinalstepingettingthefilereadyforJDBC-ODBCistocreateadatasourceassociatedwithit.UnlikeotherinputoutputclassesinJava,JDBCdoesntuseafilenametoidentifyadatafileanduseitscontents.Instead,atoolsuchastheODBCDataSourceAdministratorisusedtonametheODBCsourceandindicatethefilefolderwhereitcanbefound.IntheODBCDataSourceAdministrator,clicktheUserDSNtabtoseealistofdatasourcesthatareavailable.Toaddanewoneassociatedwithworld20.mdb(oryourowndatabase),clicktheAddbutton,chooseanODBCdriver,andthenclicktheFinishbutton.ASetupwindowopensthatyoucanusetoprovideaname,shortdescription,andotherinformationaboutthedatabase.ClicktheSelectbuttontofindandchoosethedatabasefile.Figure18.3showstheSetupwindowusedtosetupworld20.mdbasadatasourceintheODBCDataSourceAdministrator.AfteradatabasehasbeenassociatedwithanODBCdatasource,workingwithitinaJavaprogramisrelativelyeasyifyouareconversantwithSQL.ThefirsttaskinaJDBCprogramistoloadthedriver(ordrivers)thatwillbeusedtoconnecttoadatasource.AdriverisloadedwiththeClass.forName(String)method.Class,partofthejava.langpackage,canbeusedtoloadclassesintotheJavainterpreter.TheforName(String)methodloadstheclassnamedbythespecifiedstring.AClassNotFoundExceptioncanbethrownbythismethod.4AllprogramsthatuseanODBCdatasourceusesun.jdbc.odbc.JdbcOdbcDriver,theJDBC-ODBCbridgedriverincludedwithJava.LoadingthisclassintoaJavainterpreterrequiresthefollowingstatement:Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”);Afterthedriverhasbeenloaded,youcanestablishaconnectiontothedatasourcebyusingtheDriverManagerclassinthejava.sqlpackage.ThegetConnection(String,String,String)methodofDriverManagercanbeusedtosetuptheconnection.ItreturnsareferencetoaConnectionobjectrepresentinganactivedataconnection.Thethreeargumentsofthismethodareasfollows:1.AnameidentifyingthedatasourceandthetypeofdatabaseconnectivityusedtoreachitnAusername2.ApasswordThelasttwoitemsareneededonlyifthedatasourceissecuredwithausernameandapassword.Ifnot,theseargumentscanbenullstrings(“”).Thenameofthedatasourceisprecededbythetextjdbc:odbc:whenusingtheJDBCODBCbridge,whichindicatesthetypeofdatabaseconnectivityinuse.ThefollowingstatementcouldbeusedtoconnecttoadatasourcecalledPayrollwithausernameof“Doc”andapasswordof“1rover1”:Connectionpayday=DriverManager.getConnection(“jdbc:odbc:Payroll”,“Doc”,“1rover1”);Afteryouhaveaconnection,youcanreuseiteachtimeyouwanttoretrieveorstoreinformationfromthatconnectionsdatasource.ThegetConnection()methodandallotherscalledonadatasourcethrowSQLExceptionerrorsifsomethinggoeswrongasthedatasourceisbeingused.SQLhasitsownerrormessages,andtheyarepassedalongaspartofSSQLExceptionobjects.AfteryouhaveaStatementobject,youcanuseittoconductanSQLquerybycallingtheobjectsexecuteQuery(String)method.TheStringargumentshouldbeanSQLquerythatfollowsthesyntaxofthatlanguage.ThisSQLqueryretrievesseveralfieldsforeachrecordinthedatabaseforwhichtheCountryfieldisnotequaltonull.TherecordsreturnedaresortedaccordingtotheirCountryfield,soAfghanistanwouldprecedeBurkinaFaso.5中文翻译Java程序是用来处理数据的,原始数据,对象,数组,链表和其他结构都是为了这一个目的。今天来接触一个更复杂的数据库连接JDBC,它是一个类库,用来与数据库进行连接。Java6中包含的JavaDB,一个小型关系型数据库的部分Java开发工具包,使得它比以往更容易把一个数据库到应用程序中。现在说明JDBC和可扩展标记语言(XML)的使用方法:使用JDBC驱动程序与不同的关系数据库工作访问与结构化查询语言(SQL)数据库使用SQL和JDBC从数据库中读取记录使用SQL和JDBC添加记录到数据库创建一个新的JavaDB数据库和读取其记录Java数据库连接(JDBC)是一组可以用来开发类与微软,Sybase开发数据库工作的客户机/服务器应用程序。使用JDBC,你可以用在Java程序中相同的方法和类来读取和写记录,并执行其他类型的数据库访问。称为驱动程序的类作为一个桥到数据库源有驱动程序为每个流行的数据库。客户端/服务器软件的信息的用户与该信息提供者连接时,并且它是编程中最普遍的形式之一。您可以使用它,你每次网上冲浪:一个Web浏览器客户端请求的网页,图像文件及其他文件使用一个统一资源定位符或URL。对于客户端来说,可以发现Web服务器提供所需的信息。对于数据库程序员来说最大的一个障碍是,对于不同的数据库,每个数据访问其自己的专有的方法。为了简化使用关系型数据库的程序,标准的语言称为SQL(结构化查询语言)已经出台。这个语言代替了去学习多种数据库语言的困境。在数据库编程,用于在一个数据库中的记录的请求被称为查询。运用SQL中,您可以发送复杂的查询到数据库,并得到你要找的记录以任意顺序指定。以一个数据库程序员的例子来说,一个程序员要求准备拖欠贷款人的报告。程序员可以使用SQL来查询所有记录的数据库中,而最后还款超过180天前和应付的金额超过$0.00元。SQL还可以用来控制该顺序返回记录,所以程序员可以得到社会安全号码,收件人姓名,所欠金额,或其他领域贷款的顺序记录。这一切都只需要SQL语言,程序员不需要学习任何的专有的数据库语言。JDBC支持SQL,使开发人员无需知道使用广泛的数据库格式底层数据库的细节。JDBC也支持使用数据库查询特定到数据库的格式。将JDBC类库的方式来访问数据库与SQL相媲美现有的数据库开发技术,所以用SQL数据库通过使用交互JDBC是不是比使用传统数据库工具太大的不同。Java程序员谁已经有一些数据库的经验,可以在支持JDBC的平台上使用。下面说明JDBC的使用步骤:1.制作到数据库的连接2.创建使用SQL的声明3.执行的SQL查询数据库4.查看结果记录另:这些JDBC类都是java.sql包的一部分。在JDBC数据库编程中经常要使用ODBC。所以,在讲述JDBC的驱动程序分类6之前,首先介绍什么是ODBC。ODBC是指OpenDataBaseConnectivity,即开放数据库互连,它建立了一组规范,并且提供了一组对数据库访问的标准API(应用程序编程接口),这些API利用SQL来完成其大部分任务。ODBC也提供了对SQL的支持。.JDBC驱动程序由实施了这些接口的类组成,JDBC的总体结构有四个组件:应用程序、驱动程序管理器、驱动程序和数据源。将JDBC转换成ODBC驱动器,依靠ODBC驱动器和数据库通信。在这种方式下,ODBC驱动程序和桥代码必须出现在用户的每台机器中,这种类型的驱动程序最适合于企业网(这种网络上客户机的安装不是主要问题),或者是用Java编写的三层结构的应用程序服务器代码。本地API一部分用Java来编写的驱动程序。这种类型的驱动程序把客户机API上的JDBC调用转换为Oracle、Sybase、Informix、DB2或其他DBMS的调用。像桥驱动程序一样,这种类型的驱动程序,要求将某些二进制代码加载到每台客户机上。JDBC网络纯Java驱动程序将JDBC转换为与DBMS无关的网络协议,这种协议又被某个服务器转换为一种DBMS协议。这种网络服务器中间件能够将它的纯Java客户机连接到多种不同的数据库上,所用的具体协议取决于提供者。通常,这是最为灵活的JDBC驱动程序。所有这种解决方案的提供者,都提供适合于Intranet用的产品。为了使这些产品支持Internet,它们必须处理Web所提出的安全性、通过防火墙的访问等额外要求,几家提供者正将JDBC驱动程序,加到他们现有的数据库中间件产品中。本地协议纯Java驱动程序类型的驱动程序将JDBC调用直接转换为DBMS所使用的网络协议,这将允许从客户机机器上直接调用DBMS服务器,是Intranet访问的一个很实用的解决方法。由于许多这样的协议都是专用的,因此数据库提供者自己将是主要来源。ODBC数据源,可设置从内部数据库中的一些程序。例如,当一个新的数据库文件是在Lotus方法创建的,用户有关联的选项它与ODBC驱动程序。所有的ODBC数据源必须给予简短,描述性的名称。这个名字是内部使用Java程序时建立连接到源指的是数据库。在Windows系统上,后一个ODBC驱动程序中选择并创建数据库时,他们显示在ODBC数据源管理器。你今天的第一个项目是使用JDBC-ODBC桥来连接Java应用程序到一个Access文件。该项目的Access文件是world20.mdb,世界能源统计数据库美国能源信息署公布的。该数据库中的表包括你将在项目中使用三个字段:1.国家2.出厂年份3.产量要使用这个数据库,你必须有一个ODBC驱动程序在系统支持上访问文件。使用ODBC数据源管理器(或类似的程序,如果你是在非Windows系统)上,您必须创建关联的新的ODBC数据源world20.mdb其他安装工作可能会因您的系统上的ODBC驱动程序是必要的,如果有的话。这是与您的系统,在得到该文件的最后一步上的ODBC驱动程序不兼容准备JDB
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 高铁安检安全知识培训内容课件
- 高血脂科普课件
- 110kV变电站(升压站)工程施工组织设计
- 可研及设计评审服务合同
- 电网建设工程造价咨询合同
- 电缆质量知识培训总结课件
- 电站抢修知识培训课件
- 五四制 统编版 语文 一年级 上册 我是小学生【新课标版】课件
- 电石炉净化设备培训知识课件
- 电焊行业专业知识培训课件
- 2025年北京市中考语文真题(含答案)
- 学前教育学完整-2017课件
- 小学英语“教学评一体化”实施
- 浙江省地方课程《人自然社会》课件
- 新版现代西班牙语第二册课后答案
- CS4000高级过程控制实验装置设备操作说明书
- 上海港港口拖轮经营人和港口拖轮名录
- T-CAMET 04017.1-2019 城市轨道交通 全自动运行系统规范 第1部分:需求
- 肾癌伴下腔静脉癌栓诊治共识课件
- 企业安全标准化班组建设PPT课件
- 超长混凝土结构温度应力分析
评论
0/150
提交评论