下載本文檔
版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(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)益歸上傳用戶所有。
- 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ì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年高考語(yǔ)文復(fù)習(xí)知識(shí)清單第2章文學(xué)類(lèi)文本閱讀(一)小說(shuō)專(zhuān)題01賞析小說(shuō)故事情節(jié)(學(xué)生版+解析)
- 臍橙樹(shù)打藥安全責(zé)任書(shū)承包合同(2篇)
- 南京工業(yè)大學(xué)浦江學(xué)院《專(zhuān)業(yè)綜合實(shí)訓(xùn)(通信工程)》2022-2023學(xué)年第一學(xué)期期末試卷
- 南京工業(yè)大學(xué)浦江學(xué)院《審計(jì)學(xué)》2023-2024學(xué)年第一學(xué)期期末試卷
- 多變的紙條說(shuō)課稿
- 小石城7#樓 施工組織設(shè)計(jì)
- 南京工業(yè)大學(xué)浦江學(xué)院《建筑給水排水工程》2023-2024學(xué)年第一學(xué)期期末試卷
- 《小石潭記》說(shuō)課稿
- 小學(xué)音樂(lè)面試《哦十分鐘》說(shuō)課稿
- 南京工業(yè)大學(xué)《中日比較文學(xué)》2022-2023學(xué)年第一學(xué)期期末試卷
- HY/T 0289-2020海水淡化濃鹽水排放要求
- GB/T 26593-2011無(wú)損檢測(cè)儀器工業(yè)用X射線CT裝置性能測(cè)試方法
- GB/T 20721-2022自動(dòng)導(dǎo)引車(chē)通用技術(shù)條件
- 外包施工人員入場(chǎng)安全培訓(xùn)考試卷(項(xiàng)目經(jīng)理)
- 纖維素的分子結(jié)構(gòu)課件
- 四年級(jí)上冊(cè)第十課美化我的文字《美化我的文字》課標(biāo)版四年級(jí)上冊(cè)
- 領(lǐng)導(dǎo)力與團(tuán)隊(duì)建設(shè)教材課件
- 《思想道德與法治》 課件 第六章 學(xué)習(xí)法治思想 提升法治素養(yǎng)
- 全文圖解進(jìn)一步加強(qiáng)新時(shí)代中小學(xué)思政課建設(shè)的意見(jiàn)教育學(xué)習(xí)PPT
- 同仁堂-老字號(hào)的營(yíng)銷(xiāo)典范案例分析課件
- 《城鎮(zhèn)燃?xì)廨斉涔こ淌┕ぜ膀?yàn)收規(guī)范》CJJ33-
評(píng)論
0/150
提交評(píng)論