

下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、ChaptersALMOST ALL APPLICATIONS MUST deal with saving and preserving data. Although some infor-mation can be stored in property files and the system environment, using a database is a natural way to ease development and increase the scalability of an application. Even if applicationisntprimarilydesi
2、gnedforstoringandcataloginginformation,itcanstillbenefithavingeasystodatastorageandDatabases are the lifeblood of many systemsChaptersALMOST ALL APPLICATIONS MUST deal with saving and preserving data. Although some infor-mation can be stored in property files and the system environment, using a data
3、base is a natural way to ease development and increase the scalability of an application. Even if applicationisntprimarilydesignedforstoringandcataloginginformation,itcanstillbenefithavingeasystodatastorageandDatabases are the lifeblood of many systems and applications, and being able understand the
4、m and manipulate them comfortably is important to the career of a engineer. Data management is such a large t a database team is egral part of medium- to large-databaseanization, and anization can have a number of In this chapter, well discuss how to connect to a database using JDBC (Java Connectivi
5、ty). Well use database for the chapter les, and well try to keep code as portable sible. (Well discuss portability and how to use complicated designsinChapters13andhisUnderstandingdatabaseUsingJavaDatabaseConnectivity Basic connection poolingAleapplicationillustratingbasicDatabaseConnectivityandBefo
6、re we look in t JDBC, lets look at the big picture. When a database starts, it requests by listening to a network port, often using TCP/IP ernetProtocol).The situationogoustoa web tstarts, listens toportand then responds to requests. However, unlike a web server, no two databases speak exactly same
7、language. Even a complex t deals with raw streams of data needs a toetheconnectiontothettranslatormechanismisadatabaseThe translation layer can be a t is o a program, or it can be ODBC(opendatabaseconnectivity)oraJDBCdriver.Compilingaoaprogramcan the most fossilized approach; however, it itable for
8、rare s when determinedinadvanceandwhentheextratime ittakestoloadadriverisLoadable drivers made available with ODBC. ODBC drivers were written in C C+,andanindividualdriverhadtobecreatedanddistributedforeachoperatingsystem.good C/C + t twists the bits of customized objects and handles huge amounts of
9、 data noeasytask.Therefore,newODBCdriverswerereleasedwhencould get them out, and if your operating system wasnt supported or supported quickly, all you could dowas get in line tocomplain.Writing drivers foreverybody was alot of work before Java.JDBC takes advantage of Javas virtual machine layer and
10、 Javas ability to produce write-once-run-anywhere code. Early on, there was a hodgepodge of JDBC drivers, many of which were hybrid ODBC/JDBC drivers. Today, every major database mak rovides pure Java database drivers.The JDBC classes and objects related to the use of JDBC are found in the java.sql
11、javax.sql HowJDBCYou mustexplicitly loada JDBC o the he JavaJDBC takes advantage of Javas virtual machine layer and Javas ability to produce write-once-run-anywhere code. Early on, there was a hodgepodge of JDBC drivers, many of which were hybrid ODBC/JDBC drivers. Today, every major database mak ro
12、vides pure Java database drivers.The JDBC classes and objects related to the use of JDBC are found in the java.sql javax.sql HowJDBCYou mustexplicitly loada JDBC o the he Java runtime YoucandosobycallingtheregisterDrivermethodon thejava.sql.DriverManager objectlikethis: DriverManager.registerDriver(
13、 new oracle.jdbc.driver.OracleDriver();Or you can use the more general dynamic class loading method java.lang.Class.forName() In both cases, the name in parentheses is the packaged ame of the driver. The -2.0.4-bin.jaronthe Class.forName registerDriverthe class to be dynamically o the Java runtime t
14、hejava.sql.DriverManagertoexplicitlyloadthenamedclass.Thes Class.forName merely t the Java runtime environment can find the amed; s of resumably signals the runtime presence of the classes. either case, a flawed or corrupted driver can still throw an exception on attempting a orperhapsafaultydriverw
15、illcauseanerror becauseitsona deprecatedmethod. Atany erformsmorerigorouschecking,anditsmorelikelytothrowanajava.sql.DriverManagerobject.Thereareseveraloverloadedfunctioncallsignaturesg a connection. Each takes a URL-based connection string, a URL string, a properties object, URL-basedconnectionstri
16、ng,andtwoadditionalstringsforusernameandLetslookatanleoftheconnectionstringandseehowitscn=This string includes the connection method ), the database driver type, machine name withthe database followedbya colon andthe port numberthe databaseis listening to, the default startup database, and another c
17、olon separating the username and password.To prevent , the his chapters code use the most explicit separation of Listing 7.1, the JdbcSetupTest JSP, tests the plain setup of database and the tshouldbe heclasspath.Figure7.1showstheresultofasfulLISTING7.1:To help you troubleshoot your JDBC installatio
18、n, the JDBC setup test page an page if an Exception is thrown. The error page, which is created in Listing 7.2, provides some troubleshooting information, and its shown in Figure 7.2.LISTING7.2:All the le . The setup of required to run the exhis chaptertyou create one databaseandfourSQLtables is det
19、ailed in Appendix A. All you need to generate the hischapteristheSQLscriptshowninListingWithout too much effort, you should be able to create All the le . The setup of required to run the exhis chaptertyou create one databaseandfourSQLtables is detailed in Appendix A. All you need to generate the hi
20、schapteristheSQLscriptshowninListingWithout too much effort, you should be able to create these tables in another database. y kept the hese chapter les simple to avoid undue hassles during tgreSQLisanotheropen-sourcetcouldalso beused for the hisAdvancedand usually expensivedatabase systems offer the
21、 ability to use stored procedures, which can be a tremendous help in factoring out SQL manipulations and building a more robust object-oriented application.Tip: Dont neglect proper database design and sane practi . If youre erested in further study, mendJoeCelkosSQLforSmartiesaufmann,1999)andC.J. Da
22、tesroductiontoSystems(Addison-Wesley1999)forathoroughexplanationofinningsofdatabaseOnce you establish a JDBC connection, you must eractions with the using s ements (java.sql.S ement ). A JDBC s ement encapsulates a single database call, a packaging of a SQL s ement. If the object already exists in t
23、he database, such as a stored procedure, use a CallableS ement (java.sql.CallableS ement). Stored procedures require an advancedunderstanding ofSQL, andwell look at the advantages of stored procedures in ements ds to the database, but query results are returned only in (java.sql.ResultSet). The resu
24、lt set object is a full-fledged Java object with lots of Each result set packages the rows returned by a ement or a ement as an item. shenextrowofaresult,callthenext()memberNote: Tos the data, youmust call the next member function of a ResultSet: next true theres data inside the ResultSet, and if th
25、e ResultSet is empty, next returns false. You must call next attemptingtoreadanyoftheOnce next() called on the ResultSet, the data values for each column are sed requestinghe header field he SQLement. For le,if the SQLquery Selectaccount_id,the datavalue issedbycallingFigure 7.3 illustrates these pr
26、inciples. To get these results, the SimpleConnect JSP Listing 7.4) requires users to this chapter.第七章 数据库enter he database tables, something we cover later 14 Java数据库连接在TCP/IP 协议14 Java数据库连接在TCP/IP 协议Control Protocol)。该过程类似WEBWEB80号端口,并且对请求进行响应。然而,与WEB服务器不同的是,数据库都使用不同的语言。即使是综合应用,ODBC(开放式数据库互联,或JDBCO
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 江西省信丰县2025届初三第二学期学分认定考试化学试题含解析
- 陕西省榆林市米脂县重点中学2025年初三第二次模拟考试试卷英语试题含答案
- 来凤县2025年数学三下期末经典模拟试题含解析
- 江西工业职业技术学院《全球化与企业文化》2023-2024学年第一学期期末试卷
- 江苏省盐城市大冈初中市级名校2024-2025学年初三质量检测试题(三)英语试题含答案
- 江苏省苏州市太仓达标名校2025年初三5月月考(一轮检测试题)语文试题含解析
- 互联网时代的亲子教育
- DB32/T+5096-2025+汽车充(换)电站雷电防护装置检测技术规范
- 邯郸市高三下学期第一次模拟考试理综生物试题
- 销售年终述职与总结报告(素材15篇)
- 中美贸易战浅析
- 部编版语文四年级下册第四单元课外同步阅读练习及答案(两篇)
- 2024年4月自考00158资产评估试题及答案含评分标准
- 2024年陕西秦农农村商业银行股份有限公司招聘笔试参考题库含答案解析
- 工地图文广告清单及报价(2家)
- 冠状动脉造影健康宣教
- 碳酸盐岩的成岩作用课件
- 消防内务条令全文文档
- 全国优质课一等奖初中音乐《姹紫嫣红》课件
- 科伦员工手册
- 学校食堂饮食保障供餐应急预案
评论
0/150
提交评论