

下載本文檔
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
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ù)庫enter he database tables, something we cover later 14 Java數(shù)據(jù)庫連接在TCP/IP 協(xié)議14 Java數(shù)據(jù)庫連接在TCP/IP 協(xié)議Control Protocol)。該過程類似WEBWEB80號端口,并且對請求進(jìn)行響應(yīng)。然而,與WEB服務(wù)器不同的是,數(shù)據(jù)庫都使用不同的語言。即使是綜合應(yīng)用,ODBC(開放式數(shù)據(jù)庫互聯(lián),或JDBCO
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 急救系統(tǒng)運行效果評估計劃
- 培養(yǎng)員工責(zé)任感的策略計劃
- 如何利用反饋促進(jìn)成長計劃
- 學(xué)習(xí)競賽在班級中的推廣計劃
- 如何借助跨界合作增強品牌力量計劃
- 如何通過媒體關(guān)系提升品牌曝光計劃
- 統(tǒng)籌調(diào)度部資源配置與效率提升計劃
- 化學(xué)-河南金太陽2024-2025學(xué)年高二上學(xué)期第二次月考
- 2025年發(fā)動機基本件:飛輪項目合作計劃書
- 2025年船用法蘭合作協(xié)議書
- 如果國寶會說話中國歷史文物介紹
- 2024年廣東省高三一模高考英語試卷試題答案祥解(含作文范文)
- 復(fù)用醫(yī)療器械的規(guī)范化處理流程之清洗課件
- ★滬教牛津版英語四年級上冊練習(xí)冊4a
- 2024年北京電子科技職業(yè)學(xué)院高職單招(英語/數(shù)學(xué)/語文)筆試題庫含答案解析
- 生產(chǎn)車間環(huán)境改善方案
- 第1課 古代亞非(課件)
- 2024年高考物理真題分類匯編(全一本附答案)
- 醫(yī)藥銷售月總結(jié)匯報
- 地質(zhì)勘探行業(yè)復(fù)工安全培訓(xùn)課件
- 神經(jīng)系統(tǒng)疾病的癥狀和藥物治療
評論
0/150
提交評論