0% found this document useful (0 votes)
13 views25 pages

CH 1

This document provides an introduction to the J2EE platform, covering its architecture, APIs, and components like JDBC for database connectivity. It explains various architectural styles such as single-tier, two-tier, three-tier, and N-tier architectures, detailing their advantages and disadvantages. Additionally, it discusses the role of containers, specifically Tomcat as a web container, and outlines the structure and requirements for developing applications using J2EE.

Uploaded by

vivekkanet11
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views25 pages

CH 1

This document provides an introduction to the J2EE platform, covering its architecture, APIs, and components like JDBC for database connectivity. It explains various architectural styles such as single-tier, two-tier, three-tier, and N-tier architectures, detailing their advantages and disadvantages. Additionally, it discusses the role of containers, specifically Tomcat as a web container, and outlines the structure and requirements for developing applications using J2EE.

Uploaded by

vivekkanet11
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

Sub : J2EE Chapter – 1 B.C.A.

Sem -5

1 The J2EE Platform,JDBC(Java Database


Connectivity)
Topics Covered
1. Introduction to J2EE
2. Enterprise Architecture Style.
3. Enterprise Architecture
4. J2EE Platform.
5. J2EE API’s.
6. Introduction to J2EE APIs
7. introduction to container
8. Tomcat as a web container
9. Introduction.
10. JDBC Architecture
11. Datatypes in JDBC
12. Processing Queries
13. Database exception handling
14. Database Driver
15. JDBC API for connectivity
16. Connection
17. Driver Manager class
18. Statement
19. Prepared Statement
20. Callable Statement
21. ResultSet
22. Other JDBC API
23. Database Meta Data
24. Result set Meta Data
25. Connecting with Databses(MySQL,Oracle,Access)
Sub : J2EE Chapter – 1 B.C.A. Sem -5

Introduction to J2EE
 Core java application only provides facility to work with windows based applications.
 If we required developing the enterprise level application, it will not work.
 Advance java programming provides the facility to develop the Enterprise based and
Internet based Application.
 The J2EE is an open source.
 It reduces the cost of developing multi-tier applications.
 It provides a number of APIs to develop the different types of applications, such as
JDBC(Java Database Connectivity) used for making connection between the java
programs and database, JSP and servlets to create the dynamic web page, RMI(Remote
Method Invocation) to create the remote objects.
 J2EE platforms also include concept of containers to provide the interface between the
clients, server and the database.
 J2EE provides many framework such as JSF, spring, structs, hibernate to develop very
attractive, secure, user-friendly, database driven enterprise application.

1. Introduction
 The Tier
 A tier is an abstract concept that defines a group of technologies that provides one
or more services to its clients.
 In simplest words, tier is collection of similar technologies in a group.

 Introduction to Enterprise Application Design Framework


 Enterprise Application is divided in to following six (6) logical layers which are
related to the client tier, middle tier, and database tier. It defines which layer
belongs to which tier.
 Presentation Manager
 It defines User interface
 It is located on the client tier.
 Manages the information displayed to the user.
 Presentation Logic
 Defines the Navigation system of the user interface
 Focused on how and what will be displayed to the user.
 It may locate with the client tier or business tier or the database tier, based
on thin client and thick client.
 Application Logic
 Defines the actual application logic with it.
 It may be, connectivity with the database, validation over the various inputs
etc.
 It may locate with the client tier or business tier or the database tier, based
on thin client and thick client.
 Business Logic
 Contains business rules of applications.
Sub : J2EE Chapter – 1 B.C.A. Sem -5

 Should be shared with whole application.


 It may located with business tier or the database tier, based on thin client and
thick client.
 Database Logic
 Defines the table structure and relation among the tables of database
 Includes various constraints of the table.
 Located with the database tier.
 Database Manager
 Stores the persistent (constant) data.
 Always located with the database tier.
Enterprise Architecture Style
 Single tier Architecture
 When User inputs, its verifications, business logic, and data access all these are
combined together in a single computer such kind of architecture is known as Single
tier architecture.
 In this all layers are located on a single machne.
 Advantages
 Easy to manage
 Data consistency is simple due to data stored at single location.
 Disadvantages
 Single storage is not sufficient due to large number of data
 Sharing of data in large amount is not possible.
 Multiple user can’t be handled

 Two tier Architecture


 Two tier architecture is divided into two separate tiers, client machine and database
server machine (server).
 The application includes the presentation and business logic.
 Data accessed by connecting client machine to a database which is lying on another
machine (i.e. server)
 In this architecture there are various types of clients which are discussed below.
 Advantages
 Any changes made in data access logic will not affect the presentation and
the business logic
Sub : J2EE Chapter – 1 B.C.A. Sem -5

 Using this type of architecture it is easy to develop an application.


 Disadvantages
 Only supports limited number of users.
 Because each client requires its own connections and each connection
requires CPU and memory.
 So, when number of connection increases the database performance
decreases.
 Thin Client
 In two tier architecture, presentation manager resides only with the client
tier then such client is called Thin client, while other presentation logic,
application logic, business logic, data logic and database manager reside
with the server side.

 Thick Client
 In two tier architecture, presentation manager, presentation logic and
application logic are resides in client tier then such architecture is called
thick client.
 Other like business logic, data logic and database manager resides with the
server side
Sub : J2EE Chapter – 1 B.C.A. Sem -5

 Normal Client
 In two tier architecture, if the presentation manager and presentation logic
resides in the client tier then the client is called Normal Client.
 Other like application logic, business logic, data logic and database manager
resides with the server side.

 Three tier Architecture


 Applications which are divided into three tiers client tier, middle tier (business tier)
and database tier is known as three tier architecture application.
 Logic is physically divided into separate tier.
 The presentation layer and logic runs on the client machine.
 Application and business logic runs on J2EE server and database logic is there with
database layer.
Sub : J2EE Chapter – 1 B.C.A. Sem -5

 Thin Client
 In three tier architecture if the presentation manager is resides only with the
client tier then client is called as thin client.
 Presentation logic, application logic and business logic are with the business
tier
 Database logic and database manager are with the database tier (EIS –
Enterprise Information System.)
 Thick Client
 In three tier architecture if the presentation manager ,presentation logic,
application logic resides with the client tier then client is called as thick
client.
 Business logic only with the business tier
 Database logic and database manager are with the database tier (EIS –
Enterprise Information System.)
 Advantages
 Improves scalability since application server are deployed on many
machines.
 Database connection not required longer from each and every client, only
requires connections from a smaller number of application server.
 Better reusability due to same logic can be initiated from many clients.
 Security is ensured because not direct access to the database.
 Disadvantages
 Increases complexity
 N-tier architecture
 Application divided into more than three tier can be called as N-tier architecture.
 In N-tier architecture it is not decided how many tier can be present.
Sub : J2EE Chapter – 1 B.C.A. Sem -5

 Number of tiers in application is depends on the computing and network hardware


on which application is to be deployed.
 Basically divided into client tier, web tier, business (EJB) tier and database tier
(EIS).
 Client tier
 Collection of user interface for user request and the print the response
 Runs on the client machine.
 Uses browser or applet as client side application.
 Web tier
 Collection of JSP and servlet dynamic webpages to handle the HTTP
specific request logons, session , accesses the business services and response
back to the client.
 Business tier
 Collection of business logic for J2EE application. For example EJB
(Enterprise JavaBeans)
 Using such business tier, same business logic can support different types of
client like browser, WAP, other stand-alone application.
 Database (EIS) tier
 EIS tier consist of the DBMS/RDBMS.
 Handles User SQL requests and generates appropriate response based on the
queries.
 Stores all persistent data in database
 Advantages
 Separation of User interface logic, business logic is done
 Business logic resides on small number of centralized machines.
 Easy to maintain, to manage, to scale, loosely coupled etc., Modification is
easy
 Disadvantages
 More complex structure
 Difficult to setup and maintain all separated layers
Sub : J2EE Chapter – 1 B.C.A. Sem -5

 Enterprise Architecture
 Java 2 Enterprise edition is basically developed for commercial project and web-
solution.
 Business solution for commercial project solved using multi-tier architecture.
 The J2EE platform use multi-tier distributed application model for enterprise
application.
 By dividing application logic into various component according to its tasks or
functions and various application components that are gathered J2EE application are
installed on different machine depending on the tier.
 Following tiers are available for J2EE.
 Client tier (Client machine)
 Web tier (J2EE server)
 Business tier ( J2EE server)
 EIS tier (database server)
Sub : J2EE Chapter – 1 B.C.A. Sem -5

J2EE Platform
 Introduction to editions of Java.
 J2SE – Java 2 standard edition normally used for developing desktop
application.
 J2EE – Java 2 enterprise edition used for applications which are run on
server example websites.
 J2ME – Java 2 Micro Edition (mobile) used for developing small scale
device like mobile, PDA
 What is J2EE ?
 J2EE is an open standard which is provided by Sun micro system (now
Oracle) for applications which runs on server.
 Provides Multi-tier architecture.
 Includes J2SE + most of other java technologies.
 (JavaMail, Servlet, EJB, JMS, JSF)
Sub : J2EE Chapter – 1 B.C.A. Sem -5

 J2EE as a multi-tier architecture.

 J2EE has following tiers in its architecture


 Client tier
 Web tier
 Business tier (EJB tier)
 Database tier (EIS tier)

J2EE API’s
 JDBC (Java Database Connectivity)
 Used to connect Java Database application with any relational database.
 Can access well know database like MySQL, Oracle, MS – access.
 RMI (Remote Method Invocation)
 Used to access object method running on different machine
 RPC (Remote Procedure Call)
 EJB (Enterprise JavaBeans)
 Used to server side components
 Consisting business logic for J2EE application
 JS (Java Servlet)
 Object oriented abstraction for building dynamic web application.
 JSP (Java Server Pages)
 Provides easy way for building dynamic web-applications
 JMS (Java Message Service)
 Provides Java API for message passing , publish and subscribe the types of message
oriented middleware service.
 JNDI (Java Naming and Directory Interface)
 Provides different types of naming and directory service.
 JTA (Java Transaction API)
Sub : J2EE Chapter – 1 B.C.A. Sem -5

 Used for Implementing distributed transaction application

 JM (Java Mail)
 Provides platform independent and protocol independent framework to build java
based mail application

Introduction to Container
 Container
 They are interface between a component and Client (lower level code) or
platform oriented functionality which supports component.
 Container provides communication between client and components.
 Types of Container
 Applet Container
 Application Container
 EJB Container
 Web Container
Sub : J2EE Chapter – 1 B.C.A. Sem -5

 Applet Container
 One kind of client container which is used to manage execution of the applet
on the browser.
 Application Container
 It is client container which is used to manage application client and their
component
 EJB Container
 It is one kind of server container.
 Used to manage enterprise beans components.
 Web container
 It is also sever container which is used to manage execution of JSP pages
and servlet components

Tomcat as a web container


 Tomcat is a one type of server container which is used to manage and execute the
JSP pages and servlet components.
 Tomcat is a powerful web container provided by Apache
 Using Tomcat we can develop servlet and JSP application.
 Tomcat is free web container that can be used to run code of JSP servlet.
 We can debug JSP and servlet pages using Tomcat before deploying on the server.
 Using JSP and servlets we can develop dynamic web pages.
 Apache most common HTTP web server on the internet which is use Tomcat is
chosen to be official sun web container.

 Basic Requirements
 Sun Java JDK 5.0 or higher
 Tomcat 6.0 or higher
 Eclipse or Netbeans (Editor)
 We can use any editor like notepad, wordpad, notepad++ and Dreamweaver
to create, edit webpage’s coding

 Tomcat Directory Structure


 Bin
 All the scripts and batch files are stored in this folder which are used
to startup a tomcat server.
 Conf
 All the configuration file for a global and server configuration and it
is also contains user authentication settings.
 Log
 All the server logs resides in this folder
 Lib
 JAR files resides in this folder which is used by tomcat
 Webapps
 All the JSP and servlet application folders are stored in this folder.
 Work
Sub : J2EE Chapter – 1 B.C.A. Sem -5

 All the temporary and precompiled files are stored in this folder.
Introduction

 JDBC stands for Java Database Connectivity. The java provides JDBC API to
create java application that is capable of interacting with a database. As we know
that java is an ideal language for persistent data storage.
 By using class inheritance and data encapsulation, a java application developed to
work with one type of data storage can be ported or extended to work with another.
 An example for that is an application currently working with a RDBMS (Relational
Database Management System) that is extended to use RMI to store data in file.
 A general misunderstanding with java is that database access is possible only with
the JDBC API. Even though the JDBC provides the lower level classes to manage
database connections and transaction, it is not compulsory component.
JDBC Vs ODBC
 ODBC can not directly used with java because it uses a C interface. Calls From java
to native C code have number of drawbacks in the security, implementation,
robustness, and automatic portability of application.
 ODBC makes use of pointers which have been totally removed from java.
 ODBC mixes simple advance feature of ODBC driver manager and driver on all
client machines. JDBC drivers are written in java and JDBC code is automatically
installable secure and portable on all java platforms from network computers to
mainframes.
 JDBC API is an natural java interface and is built on ODBC retain some of the basic
features of ODBC like X/Open SQL call level interface
JDBC API
 The JDBC API is collection of methods, classes and interface that enable java
application to communicate with database. For this, database should be installed on
the computer and the RDBMS should provide a driver. JDBC classes and interface
defined in the package java.sql constitute the JDBC API.
 The Java program invokes the method of the JDBC API. The JDBC API then calls
the JDBC driver and submits the queries to the SQL statements that a database can
understand. After the query has been performed, the JDBC driver retrieves the result
of the query form the database.
 The result is converted into the JDBC API classes that are used by the java
Program. Then the java program obtain the result of the query.

JDBC Architecture
 The JDBC architecture describes the interaction of JDBC API with java application
and java applet. JDBC API consists of several call level interface with JDBC Driver
Manager and JDBC drivers for defining database.
 JDBC API is responsible for transferring data between an application and a
database.
Sub : J2EE Chapter – 1 B.C.A. Sem -5

Datatypes in JDBC
 The following table summarizes the default JDBC data type that the java data type is
converted when you call the setXXX() method of preparedStatement and
CallableStatement.

SQL JDBC/Java setXXX()


VARCHAR Java.lang.String setString()
CHAR Java.lang.String setString()
LONGVARCHAR Java.lang.String setString()
BIT Boolean setBoolean()
NUMERIC Java.math.BigDecimal setBigDecimal()
TINYINT Byte setByte()
SMALLINT Short setShort()
INTEGER Int setInt()
BIGINT Long setLong()
REAL Float setFloat
FLOAT Float setFloat()
DOUBLE Double setDouble()
DATE Java.sql.Date setDate()
ARRAY Java.sql.Array setArray()
STRUCT Java.sql.Struct setStruct()
TIME Java.sql.Time setTime()
Sub : J2EE Chapter – 1 B.C.A. Sem -5

Types of Database Driver


JDBC Driver Type

 JDBC Driver is a software component that enables java application to interact with
the database.
 Database vendor provide a driver along with the database. These database driver are
used to communicate with java programs.
 There are 4 types of JDBC drivers:
 JDBC-ODBC bridge driver
 Native-API driver (partially java driver)
 Network Protocol driver (fully java driver)
 Thin driver (fully java driver)
1. JDBC-ODBC Bridge
 The JDBC-ODBC bridge driver converts all JDBC call into ODBC calls and send
them to the ODBC driver. The ODBC driver then forwards the call to the database
server.
 Figure shows communication between java application and Database using JDBC-
ODBC Bridge. E.g. MS Access do not provide JDBC driver. Instead they provide
Open Database Connectivity(ODBC) drivers. One can access such databases by
using the ODBC-JDBC Bridge.

 Advantages
 easy to use.
 can be easily connected to any database.
 Disadvantages
 Performance degraded because JDBC method call is converted into the
ODBC funcion calls.
 The ODBC driver needs to be installed on the client machine.
2. Native-API / Partly-Java driver
 This driver communicates directly the database server. Therefore the vendor
database library needs to be loaded on each client computer.
Sub : J2EE Chapter – 1 B.C.A. Sem -5

 The type 2 driver converts JDBC calls into database-specific calls for databases.
Example: Oracle will have oracle native API.

 Advantage:
 Performance upgraded than JDBC-ODBC bridge driver.
 Disadvantage:
 The Native driver needs to be installed on the each client machine.
 The Vendor client library needs to be installed on client machine.
3. Net-Protocol / all-Java Driver
 The JDBC Type 3 driver, or the net-protocol/all-java driver, follows a three-tiered
approach. In the three-tiered approach, JDBC database requests are passed to a
middle-tier server.
 The middle-tier server then translates the requests and passes to the database-
specific native-connectivity interface and forward the requests to the database
server. If the middle-tier server is written in Java, it can use the Type 1 or Type 2
drivers to forward the requests to the database server.
Sub : J2EE Chapter – 1 B.C.A. Sem -5

 Advantage:
 No client side library is required because of application server that can
perform many tasks like auditing, load balancing, logging etc.
 Disadvantages:
 Network support is required on client machine.
 Requires database-specific coding to be done in the middle tier.
 Maintenance of Network Protocol driver becomes costly because it requires
database-specific coding to be done in the middle tier.
4. Native-Protocol / All-Java Driver

 These drivers are commonly implemented in java to achieve platform


independence.
 These drivers convert JDBC calls into vendor specific DBMS protocol. Therefore
client applications can communicate directly with the database server when Type 4
driver are used.
Sub : J2EE Chapter – 1 B.C.A. Sem -5

 Advantage:
 Better performance than all other drivers.
 No software is required at client side or server side.
 Disadvantage:
 Drivers depend on the Database.

JDBC API for database connectivity


1 Connection
 A Connection is the session between java application and database. The Connection
interface is a factory of Statement, PreparedStatement, and DatabaseMetaData i.e.
object of Connection can be used to get the object of Statement and
DatabaseMetaData. The Connection interface provide many methods for transaction
management like commit(),rollback() etc.
 Methods
 1) public Statement createStatement(): creates a statement object that can be used
to execute SQL queries.
 2)public Statement createStatement(int resultSetType,int
resultSetConcurrency): Creates a Statement object that will generate ResultSet
objects with the given type and concurrency.
 3) public void setAutoCommit(boolean status): is used to set the commit
status.By default it is true.
 4) public void commit(): saves the changes made since the previous
commit/rollback permanent.
 5) public void rollback(): Drops all changes made since the previous
commit/rollback.
 6) public void close(): closes the connection and Releases a JDBC resources
immediately.
Sub : J2EE Chapter – 1 B.C.A. Sem -5

2. Driver Manager Class


 The DriverManager Class belongs to java.sql package. It consists of the static
methods to manage the JDBC Driver must register with the DriverManager class.
There are many JDBC Driver for Oracle. In short DriverManager class controls the
interface between the application and JDBC Driver.
 In DriverManager class important methos is getConnection() method which is used
to establish the connection with the different database servers. It has three
overloaded signatures (parameters)
Syntax:-
 public static Connection getConnection(String url) throws SQLException
 public static Connection getConnection(String url,Properties Info) throws
SQLException
 public static Connection getConnection(String url,String username,String
Password) throws SQLException

3 Statement
 The interface object is used to execute the SQL statement and obtain the result
produced after executing the statement. It is having following different types of
methods to execute the different types of SQL Statements.
 Methods
executeQuery():
 This method is used to fetch the records from the database by using select query. It
can be only return the single ResultSet object at a time. It passed a parameter with it
which is SQL statement which selects the records from the database. This method
throws SQLException.
 Syntax :-
 public ResultSet executeQuery(String sql) throws SQLException
executeUpdate() :
 This methods is used to perform the insert, update, delete operation on the records
of the database. It returns the integer value which indicates no. of records updated
the records in the database. It passes a SQL statement as a parameter which updates
the records of the database. This method throws SQLException.
 Syntax :-
 Public int executeUpdate(String sql) throws SQLException
execute() :
 It is used to execute the SQL statements which returns the multiple results. It returns
the boolean indicating value if it returns true that the next result is ResultSet object
and if it returns false then defines no. of records updated or no more result is there.
This method throws SQLException.
 Syntax :-
 Public boolean execute(String sql) throws SQLException

4 Prepared Statement
 PrepareStatement Interface is derived from the Statement Interface. Disadvantages
of Statement object are that it executes a simple SQL statement with no parameters
and the SQL statement is not precompiled.
 But PreparedStatement object uses a template to create a SQL request and use a
PreparedStatement to send precompiled SQL statements with one or more
Sub : J2EE Chapter – 1 B.C.A. Sem -5

parameters, So sometimes it is more convenient to use a PreparedStatment object


for sending SQL statements to database.
 If you want to execute a SQL statements many times, PreparedStatement object
reduces execution time in comparison to Statement object.
 Examples :-
String sql = “insert into data(ID, Name) values (?,?)”;
PreparedStatement ps = con.preparedStatement(sql);
ps.setInt(1,10);
ps.setString(2,”ABC”);
SetXXX Methods :
(1) setInt(int parameterIndex, int x)
(2) setChar(int parameterIndex, Char x)
(3) setString(int parameterIndex, String x)
(4) setFloat(int parameterIndex, float x)
(5) setDouble(int parameterIndex, double x)
(6) setLong(int parameterIndex, long x)
(7) setShort(int parameterIndex, short x)
(8) setDate(int parameterIndex, Date x)
(9) setByte(int parameterIndex, byte x)
(10)setBlob(int parameterIndex, Blob x)
 Methods:
 Same as Statement
 executeQuery()
 executeUpdate
 execute()

5 Callable Statement
 A CallableStatement object provides a way to call stored procedure in a standard
way for all DBMSs.
 A stored procedure is stored in a database; the call to the stored procedure is what a
CallableStatement object contains.
 This call is written in an escape syntax that may take one of two forms: one form
with a result parameter, and the order without one.
 A result parameter, a kind of OUT parameter, is the return value for the stored
procedure.
 Both forms may have a variable number of parameters used for IN parameters, OUT
parameters or both IN OUT parameters.
 A question mark serves as a placeholder for a parameter.
 Syntax for Stored Procedure with IN parameters
{ call procedure_name[(?,?,...)] }
 Syntax for Stored Procedure with IN and OUT parameters
{ ? = call procedure_name[(?,?,...)] }
 Syntax for Stored Procedure with no parameters
{ call procedure_name }
Sub : J2EE Chapter – 1 B.C.A. Sem -5

6 ResultSet
 It is used to access the data of the table from database.
 Result Set object stores the data of the table by executing the query.
 It also maintains the cursor position for navigation of the data.
 Cursor can move on first, next, previous and last position from the current row
position.
 It also fetch the data from the table using getXXX methods depends on column
type.
 It can fetch the data either passing column name or index number with getXXX
methods.
 Index number always starts with one.
 Fields of ResultSet Interface
 CONCUR_READ_ONLY
It defines the ResultSet object can not be modified.
 CONCUR_UPDATEBLE
It defines the ResultSet object can be modified.
 TYPE_FORWARD_ONLY
It defines the cursor from the current row can move forward only.
 TYPE_SCROLL_INSENSITIVE
It defines that cursor can scroll but can not be modified or updated
 TYPE_SCROLL_SENSITIVE
It defines that cursor can scroll and also can be modified or updated.
 getXXX Method
getString()
getInt()
getBoolean()
getDouble()
getFloat()
getDate()
getLong()
getShort()
getByte()
getBlob()
 Navigation Methods
 first(): It is used to move the cursor at the first record of the table.
 last(): It is used to move the cursor at the last record of the table.
 previous(): It is used to move the cursor at the previous record from the
current position of the table.
 next():It is used to move the cursor at the next record from the current
position of the table.
 afterLast(): It is used to move the cursor after the last record of the table.
 beforeFirst(): It is used to move the cursor before the first record of the
table.
 relative(int row): It is used to move the cursor on the relative no. of rows .
 absolute(int row): It is used to move the cursor on the absolute row.
Sub : J2EE Chapter – 1 B.C.A. Sem -5

Other JDBC API


1Database Meta Data
 DatabaseMetaData interface provides methods to get meta data of a database such
as database product name, database product version, driver name, name of total
number of tables, name of total number of views etc.
 Database Metadata is used to store following information of Database Metadata
object and information as below.
 What tables are available ?
 What is our user name as known to the databse?
 Is the database in read only mode ?
 Commonly used methods of DatabaseMetaData interface
 public String getDriverName()throws SQLException: it returns the name of the
JDBC driver.
 public String getDriverVersion()throws SQLException: it returns the version
number of the JDBC driver.
 public String getUserName()throws SQLException: it returns the username of
the database.
 public String getDatabaseProductName()throws SQLException: it returns the
product name of the database.
 public String getDatabaseProductVersion()throws SQLException: it returns the
product version of the database.

2Result set Meta Data


 This interface provides methods for obtaining information about the types and
properties of the columns in a ResultSet object. ResultSet MetaData is used to store
following information of ResultSet object and information as below.
 What is the number of columns in the Result Set?
 What is a column’s name?
 What is column’s SQL type?
 What is the column’s normal maximum width in chars?
 What is column’s number of decimal digits?
 Can you put a NULL in this column?
 Can column can be use in a where clause?
 The metadata means data about data i.e. we can get further information from the
data. If you have to get
 metadata of a table like total number of column, column name, column type etc. ,
ResultSetMetaData
 interface is useful because it provides methods to get metadata from the ResultSet
object.
 Commonly used methods of ResultSetMetaData interface
 public int getColumnCount()throws SQLException: it returns the total number
of columns in the ResultSet object.
 public String getColumnName(int index)throws SQLException: it returns the
column name of the specified column index.
 public String getColumnTypeName(int index)throws SQLException: it returns
the column type name for the specified index.
Sub : J2EE Chapter – 1 B.C.A. Sem -5

 public String getTableName(int index)throws SQLException: it returns the table


name for the specified column index.

Extra (asked in last exam)


Difference between Statement and Prepared Statement
1. The PreparedStatement is a slightly more powerful version of a Statement,
and should always be at least as quick and easy to handle as a Statement.
2. The PreparedStatement may be parametrized while statement is not
3. PreparedStatement is sub interface of statement inferface
4. A Statement will always proceed through the steps for each SQL query sent to the
database while prepared statement pre-executes some of those steps.

Connecting with other databases(Mysql,Access,Oracle)


Connect with Access
 To connect with access first create database in Microsoft access 2007.
 Then install ucanaccess driver.
 Then write p26

Connect with Oracle


 To connect with oracle data base first of all create oracle procedure.
 First of all start oracle database.
 start sql*plus enter scott as username and tiger as password.
 Then create std table and enter data in a table.
 Create table std(id number(3), name varchar2(30));
 Insert into std values(1,’AAAAA’);
 Insert into std values(2,’BBBBB’);
 Then write procedure
create or replace PROCEDURE SearchStudent(name1 in varchar2,name2 in
varchar2) is
BEGIN
Update Std SET name=name2 where name=name1;
END;
 Now, create a DSN for oracle
1) click on control panel
2) select administrative tools
3) select data sources(ODBC)
4) click on add button
5) select odbc driver for oracle
6) click on finish button
7) give data source name
8) click on ok button
 write the java program
import java.sql.*;
public class Pro34 {
Sub : J2EE Chapter – 1 B.C.A. Sem -5

public static void main(String[] args)


{
Connection con;
CallableStatement cst;
ResultSet rs;
Try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con = DriverManager.getConnection("jdbc:odbc:std","scott","tiger");
cst=con.prepareCall("{call SearchStudent('AAAAA','Ashish')}");
cst.executeUpdate();
con.close();
System.out.println("\n \t Procedure Successfully compiled...");
}
catch (Exception e)
{
System.out.println(e);
}
}
}
 now run the java program
javac Pro34.java
java Pro34
O/P procedure successfully compiled

 and see the output in oracle table.


Select * from std;

Connect with mysql


 To connect with mysql data base first of all create table in mysql.
 And start apache server.
 Then create employee table and enter data in a table.
 Create table std(id number(3), name varchar2(30));
 Insert into table values(1,’AAAAA’);
 Insert into table values(1,’BBBBB’);
 Now, create a DSN for oracle
9) click on control panel
10) select administrative tools
11) select data sources(ODBC)
12) click on add button
13) select mysql odbc driver 3.51
14) click on finish button
15) give data source name and database name
16) click on ok button
 write the java program
import java.sql.*;
public class ex
{
public static void main(String[] args)
{
Sub : J2EE Chapter – 1 B.C.A. Sem -5

try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con= DriverManager.getConnection("jdbc:odbc:stud","root","");
try
{
Statement st = con.createStatement();
ResultSet rs = st.executeQuery("SELECT * FROM employee");
System.out.println("Emp_code: " + "\t" + "Emp_name: ");
while (rs.next())
{
int i = rs.getInt("id");
String s = rs.getString("name");
System.out.println(i + "\t\t" + s);
}
con.close();
}
catch (SQLException se)
{
System.out.println(se);
}
}
catch (Exception e)
{
System.out.println(e);
}
}
}
 now run the java program
javac ex.java
java ex
O/P
Emp_code Emp_name
1 AAAAA
2 BBBBB

You might also like