150 likes | 271 Views
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
E N D
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 • 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
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
JDBC Drivers (Fig.) JDBC Database Type I “Bridge” ODBC ODBC Driver Type II “Native” CLI (.lib) Middleware Server Type III “Middleware” Type IV “Pure”
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
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
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
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
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
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)
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.
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.
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