1 / 15

JDBC Connection And Programming

JDBC Connection And Programming. RMI. CORBA. JDBC. Java database Programming. java.net. Overview: Java can access any database vendor. TCP/IP. Network OS. Pros using JDBC. SQL-Level 100% Pure Java Keep it simple High-performance Leverage existing database technology

grace-guy
Download Presentation

JDBC Connection And Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. JDBC Connection And Programming

  2. RMI CORBA JDBC Java database Programming java.net Overview: Java can access any database vendor TCP/IP Network OS

  3. Pros using JDBC • SQL-Level • 100% Pure Java • Keep it simple • High-performance • Leverage existing database technology • why reinvent the wheel? • Use strong, static typing wherever possible • Use multiple methods to express multiple functionality • Java allows N-Tier Architecture, especially with RMI and JDBC • flexible: can change one part without affecting others • can connect to different databases without changing code • specialization: presentation / business logic / data management • can cache queries

  4. JDBC Architecture • Java code calls JDBC library • JDBC loads a driver • Driver talks to a particular database • Can have more than one driver -> more than one database • Ideal: can change database engines without changing any application code. • JDBC Drivers • Type I: “Bridge” • Type II: “Native” • Type III: “Middleware” • Type IV: “Pure” Application JDBC Driver

  5. JDBC Drivers (Fig.) JDBC Database Type I “Bridge” ODBC ODBC Driver Type II “Native” CLI (.lib) Middleware Server Type III “Middleware” Type IV “Pure”

  6. Type I Drivers: • Use bridging technology • Requires installation/configuration on client machines • Not good for Web • e.g. ODBC Bridge • Type II Drivers: • Native API drivers • Requires installation/configuration on client machines • Used to leverage existing CLI libraries • Usually not thread-safe • Mostly obsolete now • e.g. Intersolv Oracle Driver, WebLogic drivers

  7. Type III Drivers: • Calls middleware server, usually on database host • Very flexible -- allows access to multiple databases using one driver • Only need to download one driver • But it’s another server application to install and maintain • e.g. Symantec DBAnywhere • Type III Drivers: • 100% Pure Java -- the Holy Grail • Use Java networking libraries to talk directly to database engines • Only disadvantage: need to download a new driver for each database engine • e.g. Oracle, mSQL

  8. java.sql • JDBC is implemented via classes in the java.sql package :This package provides the APIs for accessing and processing data which is stored in the database especially relational database by using the java programming language. • Figure: JDBC Class Usage Driver Manager Driver Connection Statement ResultSet

  9. Driver Manager • Connection getConnection • (String url, String user, String password) • Connects to given JDBC URL with given user name and password • Throws java.sql.SQLException • returns a Connection object • Connection • A Connection represents a session with a specific database. • Within the context of a Connection, SQL statements are executed and results are returned. • Can have multiple connections to a database • NB: Some drivers don’t support serialized connections • Fortunately, most do (now) • Also provides “metadata” -- information about the database, tables, and fields • Also methods to deal with transactions

  10. Statements: • A Statement object is used for executing a static SQL statement and obtaining the results produced by it. • Resultset: • A Result Set provides access to a table of data generated by executing a Statement. • Only one Result Set per Statement can be open at once. • The table rows are retrieved in sequence. • A Result Set maintains a cursor pointing to its current row of data. • The 'next' method moves the cursor to the next row. • you can’t rewind

  11. Resultset Methods • String getString(int columnIndex) • boolean getBoolean(int columnIndex) • byte getByte(int columnIndex) • short getShort(int columnIndex) • int getInt(int columnIndex) • long getLong(int columnIndex) • float getFloat(int columnIndex) • double getDouble(int columnIndex) • Date getDate(int column Index) • Time getTime(int columnIndex) • Timestamp getTimestamp(int columnIndex)

  12. Java Beans • JavaBeans acts as a Bridge between proprietary component models and provides a seamless and powerful means for developers to build components that run in ActiveX container applications. • A Javabean is just a java class with the following requirements. • i) It has a public no-args constructor • ii) It has 'set' and 'get' methods for its properties. • iii) It may have any general functions. • iv) If required it must be Serializable. • However,It is not necessary always that a bean should have properties. • If there are no properties, we need not provide 'set' & 'get' methods either. • ( Even the no-args constructor is provided by the compiler by default!) • If the bean uses library classes alone, it is automatically serializable. • In that case, it becomes just a class for encapsulating some functionality (ie) business logic.

  13. Such a bean , may or may not have a visual representation. As the field has moved away from the desktop, to webserver, there is no point in providing a visual represenation for a component which works in server side as the user cannot interact with it directly. (Such a bean used by jsp in webserver, is exactly similar to ASP component). ( So no worry about jar file ,BDK, BeanBox etc traditionally associated with JavaBean) • Let us now create such a bean. The Tomcat server searches for any classes in c:\tomcat\webapps\root\web-inf\classes folder. We create a subfolder under classes folder and name it 'ourbeans'. • Therefore, we specify the package information in the first line of our jspbean as : • package ourbeans; • Beans also have persistence, which is a mechanism for storing the state of a component in a safe place. This would allow, for example, a component (bean) to "remember" data that a particular user had already entered in an earlier user session.

  14. Difference between Java Beans and EJB • JavaBeans may be visible or nonvisible at runtime. Forexample, the visual GUI component may be a button,listbox,graphic or a chart.An EJB is a nonvisual ,remote object. • JavaBeans are intended to be local to a single process and are primarly intended to run on the client side.Although one can develop server-side JavaBeans,it is far easier to develop them using the EJB specification instead. EJB's are remotely executable components or businessobjects that can be deployed only on the server. • JavaBeans is a component technology to create generic Java components that can be composed together into applets and applications.Even though EJB is a component technology,it neitherbuilds upon nor extends the original JavaBean specification. • JavaBeans have an external interface called the properties interface, which allows a builder tool to interpret the functionality of the bean.EJBs have a dployement descriptor that describes its functionality to an external builder tool or IDE • JavaBeans may have BeanInfoclasses,property editors orcustomizers

  15. Thanks

More Related