

下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
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年中国机械水形变换式喷泉市场调查研究报告
- 合同攻略:买家首选采购合同模板
- 劳动合同提前终止声明
- 中班自我保护安全教育
- 第2课《梅岭三章》教学设计-2024-2025学年统编版语文九年级下册
- 儿科肛周脓肿护理查房
- 2024年九年级语文上册 第11课《富贵不能淫》教学实录 长春版
- 口才课寒假班课件
- 安防消防公司课件
- 小学防拐骗主题教育课件
- GB/T 7588.2-2020电梯制造与安装安全规范第2部分:电梯部件的设计原则、计算和检验
- 紧急采购申请单
- 小学道德与法治学科高级(一级)教师职称考试试题(有答案)
- 静配中心理论知识试题含答案
- 复旦大学英语水平测试大纲9300词汇表讲义
- (课件)肝性脑病
- DB63-T 1675-2018+建筑消防设施维护保养技术规范
- DB11T 1894-2021 10kV及以下配电网设施配置技术规范
- 零星材料明细单
- 施工现场安全检查记录表(周)以及详细记录
- 2022专升本无机化学试卷答案
评论
0/150
提交评论