• No results found

Unchecked exception

In document Complete Java J2EE Questions (Page 37-44)

1. Checked exception is catched at the compile time while unchecked exception is checked at run time. Environmental error that cannot necessarily be detected by testing;

e.g. disk full, broken socket, database unavailable, etc.

2. Unchecked exception

Errors: Virtual machine error: class not found, out of memory, no such method, illegal access to private field, etc.

Runtime Exceptions: Programming errors that should be detected in testing: index out of bounds, null pointer, illegal argument, etc.

Checked exceptions must be handled at compile time.

Runtime exceptions do not need to be. Errors often cannot be.

 What are the differences between JIT and HotSpot?

The Hotspot VM is a collection of techniques, the most significant of which is called "adaptive optimization.

The original JVMs interpreted bytecodes one at a time.

Second-generation JVMs added a JIT compiler, which compiles each method to native code upon first execution, then executes the native code. Thereafter, whenever the method is called, the native code is executed. The adaptive optimization

technique used by Hotspot is a hybrid approach, one that combines bytecode interpretation and run-time compilation to native code.

Hotspot, unlike a regular JIT compiling VM, doesn't do

"premature optimization"

 What is a memory footprint? How can you specify the lower and upper limits of the RAM used by the JVM?

What happens when the JVM needs more memory?

when JVM needs more memory then it does the garbage collection, and sweeps all the memory which is not being used.

 What are the disadvantages of reference counting in garbage collection?

An advantage of this scheme is that it can run in small chunks of time closely interwoven with the execution of the program.

This characteristic makes it particularly suitable for real-time environments where the program can't be interrupted for very long. A disadvantage of reference counting is that it does not detect cycles. A cycle is two or more objects that refer to one another, for example, a parent object that has a reference to its child object, which has a reference back to its parent. These objects will never have a reference count of zero even though they may be unreachable by the roots of the executing program. Another disadvantage is the overhead of incrementing and decrementing the reference count each time.

Because of these disadvantages, reference counting currently is out of favor.

 Is it advisable to depend on finalize for all cleanups?

The purpose of finalization is to give an opportunity to an unreachable object to perform any clean up before the object is garbage collected, and it is advisable.

 can we declare multiple main() methods in multiple classes. ie can we have each main method in its class in our program?

YES.

JDBC these tools will help DB developers to create database. But as a programmer we want to interact with the DB dynamically to execute some SQL queries from our application (Any application like C, C++, JAVA etc), and for this requirement DB vendors provide some Native Libraries (Vendor Specific) using this we can interact with the DB i.e. If you want to execute some queries on Oracle DB, oracle vendor provides an OCI (Oracle Call Interface) Libraries to perform the same.

 About ODBC: What is ODBC?

ODBC (Open Database Connectivity) is an ISV (Independent software vendor product) composes of native API to connect to different databases through via a single API called ODBC.

Open Database Connectivity (ODBC) is an SQL oriented application programming interface developed by in collaboration with IBM and some other database vendors.

ODBC comes with Microsoft products and with all databases on Windows OS.

 Advantages

 Single API (Protocol) is used to interact with any DB

 Switching from one DB to another is easy

 Doesn’t require any modifications in the Application when you want to shift from one DB to other.

 What for JDBC?

As we have studied about ODBC and is advantages and came to know that it provides a common API to interact with any DB which has an ODBC Service Provider’s Implementation written in Native API that can be used in your applications.

If an application wants to interact with the DB then the options which have been explained up to now in this book are:

1. Using Native Libraries given by the DB vendor 2. Using ODBC API

And we have listed there Advantages and Disadvantages.

But if the application is a JAVA application then the above given options are not recommended to be used due to the following reasons

1. Native Libraries given by DB vendor

a. Application becomes vendor dependent and

b. The application has to use JNI to interact with Native Lib which may cause serious problem for Platform Independency in our applications.

2. And the second option given was using ODBC API which can solve the 1.a problem but again this ODBC API is also a Native API, so we have to use JNI in our Java applications which lead to the 1.b described problem.

And the answer for these problems is JDBC (Java Data Base Connectivity) which provides a common Java API to interact with any DB.

 What is JDBC?

As explained above JDBC standards for Java Data Base Connectivity. It is a specification given by Sun Microsystems and standards followed by X/Open SAG (SQL Access Group) CLI (Call Level Interface) to interact with the DB.

Java programing language methods. The JDBC API provides database-independent connectivity between the JAVA Applications and a wide range of tabular data bases. JDBC technology allows an application component provider to:

 Perform connection and authentication to a database server

 Manage transactions

 Moves SQL statements to a database engine for preprocessing and execution

 Executes stored procedures

 Inspects and modifies the results from SELECT statements

 JDBC API

JDBC API is divided into two parts 1. JDBC Core API

2. JDBC Extension or Optional API

 JDBC Core API (java.sql package)

This part of API deals with the following futures 1. Establish a connection to a DB

2. Getting DB Details 3. Getting Driver Details

4. maintaining Local Transaction 5. executing query’s

6. getting result’s (ResultSet)

7. preparing pre-compiled SQL query’s and executing 8. executing procedures & functions

 JDBC Ext OR Optional API (javax.sql package) This part of API deals with the following futures

1. Resource Objects with Distributed Transaction Management support

2. Connection Pooling.

These two parts of Specification are the part of J2SE and are inherited into J2EE i.e. this specification API can be used with all the component’s given under J2SE and J2EE.

 JDBC Architecture:

In the above show archetecture diagram the JDBC Driver forms an abstraction layer between the JAVA Application and DB, and is implemented by 3rd party vendors or a DB Vendor.

But whoever may be the vendor and what ever may be the DB we need not to worry will just us JDCB API to give instructions to JDBC Driver and then it’s the responsibility of JDBC Driver Provider to convert the JDBC Call to the DB Specific Call.

And this 3rd party vendor or DB vendor implemented Drivers are classified into 4-Types namely

 Types Of Drivers:

1. Type-1 (JDBC ODBC-Bridge Driver) JDBC-ODBC Bridge Driver

2. Type-2 (Java-Native API Driver) Native API Partly JAVA Driver (Thick Driver)

JDBC Application

JDBC Driver

Oracle DB MS SQL

Server DB

Sybase DB JDBC

API

SP API

SP API

SP API

3. Type-3 (Java Net Protocol Driver) Intermediate DataBase Access Server

4. Type-4 (Java Native Protocol driver) Pure JAVA Driver (Thin driver)

Type-1 : JDBC-ODBC Bridge Driver :

Since ODBC is written in C-language using pointers, so JAVA does’t support pointers, a java program can’t communate directly with the DataBase. The JDBC-ODBC bridge drivertransulates JDBC API calls to ODBC API calls.

Architecture

This type of Driver is designed to convert the JDBC request call to ODBC call and ODBC response call to JDBC call.

The JDBC uses this interface in order to communicate with the database, so neither the database nor the middle tier need to be Java compliant. However ODBC binary code must be installed on each client machine that uses this driver. This bridge driver uses a configured data source.

Advantages

• Simple to use because ODBC drivers comes with DB installation/Microsoft front/back office product installation

• JDBC ODBC Drivers comes with JDK software

Disadvantages

JAVA Application

JDBC ODBC Driver

Native ODBC Client driver

Libraries

DBMS Interface

client libraries

DBMS Interface

Server Libraries DBMS

More number of layers between the application and DB.

And more number of API conversions leads to the downfall of the performance.

Slower than type-2 driver Where to use?

This type of drivers are generaly used at the development time to test your application’s.

Because of the disadvantages listed above it is not used at production time. But if we are not available with any other type of driver implementations for a DB then we are forced to use this type of driver (for example Microsoft Access).

Examples of this type of drivers JdbcOdbcDriver from sun

Sun’s JdbcOdbcDriver is one of type-1 drivers and comes along with sun j2sdk (JDK).

Setting environment to use this driver

1. Software

ODBC libraries has to be installed.

2. classpath

No additional classpath settings are required apart from the runtime jar (c:\j2sdk1.4\jre\lib\rt.jar) which is defaultly configured.

3. Path

No additional path configuration is required.

How to use this driver

1. Driver class name  sun.jdbc.odbc.JdbcOdbcDriver

2. Driver URL  dbc:odbc:<DSN>

here <DSN> (Data Source Name) is an ODBC datasource name which is used by ODBC driver to locate one of the ODBC Service Provider implementation API which can in-turn connect to DB.

Steps to create <DSN>

1. run ‘Data Sources (ODBC)’ from Control

In document Complete Java J2EE Questions (Page 37-44)