

下載本文檔
版權(quán)說(shuō)明:本文檔由用戶(hù)提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
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.第七章 數(shù)據(jù)庫(kù)enter he database tables, something we cover later 14 Java數(shù)據(jù)庫(kù)連接在TCP/IP 協(xié)議14 Java數(shù)據(jù)庫(kù)連接在TCP/IP 協(xié)議Control Protocol)。該過(guò)程類(lèi)似WEBWEB80號(hào)端口,并且對(duì)請(qǐng)求進(jìn)行響應(yīng)。然而,與WEB服務(wù)器不同的是,數(shù)據(jù)庫(kù)都使用不同的語(yǔ)言。即使是綜合應(yīng)用,ODBC(開(kāi)放式數(shù)據(jù)庫(kù)互聯(lián),或JDBCO
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶(hù)所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶(hù)上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶(hù)上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶(hù)因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 客戶(hù)經(jīng)理年終個(gè)人工作總結(jié)模版
- 社區(qū)護(hù)理資源配置優(yōu)化策略
- 快速充電技術(shù)的探索
- 風(fēng)險(xiǎn)管理套期保值講解
- 火電廠生產(chǎn)工藝流程
- 養(yǎng)老護(hù)理標(biāo)準(zhǔn)化流程
- 余姚四中教師考試試題及答案
- 有關(guān)古代法律的考試題及答案
- 銀行行長(zhǎng)面試題目及答案
- 老人晨起護(hù)理
- 托育機(jī)構(gòu)消防安全培訓(xùn)
- 《現(xiàn)代庫(kù)存管理:模型、算法與Python實(shí)現(xiàn)》 課件全套 楊超林 第1-17章 現(xiàn)代庫(kù)存管理概述-某家電企業(yè)H的制造網(wǎng)絡(luò)庫(kù)存優(yōu)化實(shí)戰(zhàn)
- (正式版)QBT 5998-2024 寵物尿墊(褲)
- (正式版)HGT 6276-2024 雙酚F型環(huán)氧樹(shù)脂
- 補(bǔ)習(xí)班輔導(dǎo)班學(xué)員合同協(xié)議書(shū)范本
- 操作系統(tǒng)智慧樹(shù)知到期末考試答案2024年
- 離婚案件中夫妻房產(chǎn)分割問(wèn)題研究
- APQP全套表格范例
- 《馬說(shuō)》復(fù)習(xí)課件
- 【可行性報(bào)告】2023年房屋租賃行業(yè)項(xiàng)目可行性分析報(bào)告
- 大規(guī)模模型蒸餾技術(shù)
評(píng)論
0/150
提交評(píng)論