Unit 3 - Java Data Base Connectivity

Similar documents
Enterprise Java Unit 1- Chapter 6 Prof. Sujata Rizal

You write standard JDBC API application and plug in the appropriate JDBC driver for the database the you want to use. Java applet, app or servlets

e-pg Pathshala Subject: Computer Science Paper: Web Technology Module: JDBC INTRODUCTION Module No: CS/WT/26 Quadrant 2 e-text

Java Database Connectivity (JDBC) 25.1 What is JDBC?

Marcin Luckner Warsaw University of Technology Faculty of Mathematics and Information Science

UNIT-3 Java Database Client/Server

Unit 2 JDBC Programming

Kyle Brown Knowledge Systems Corporation by Kyle Brown and Knowledge Systems Corporation

JDBC Drivers Type. JDBC drivers implement the defined interfaces in the JDBC API for interacting with your database server.

JDBC Architecture. JDBC API: This provides the application-to- JDBC Manager connection.

JDBC SHORT NOTES. Abstract This document contains short notes on JDBC, their types with diagrams. Rohit Deshbhratar [ address]

CSC System Development with Java. Database Connection. Department of Statistics and Computer Science. Budditha Hettige

The Design of JDBC The Structured Query Language Basic JDBC Programming Concepts Query Execution Scrollable and Updatable Result Sets

JDBC. Oracle ODBC SP API SP API. SQL server C function calls. SQL server ODBC SP API. Oracle DSN Oracle ODBC Oracle

Java and the Java DataBase Connectivity (JDBC) API. Todd Kaufman April 25, 2002

Cyrus Shahabi Computer Science Department University of Southern California C. Shahabi

Application Programming for Relational Databases

JDBC [Java DataBase Connectivity]

JDBC. Sun Microsystems has included JDBC API as a part of J2SDK to develop Java applications that can communicate with databases.

Lecture 2. Introduction to JDBC

JDBC - INTERVIEW QUESTIONS

Java Database Connectivity

Database Programming Overview. COSC 304 Introduction to Database Systems. Database Programming. JDBC Interfaces. JDBC Overview

About the Tutorial. Audience. Prerequisites. Copyright & Disclaimer

Topic 12: Database Programming using JDBC. Database & DBMS SQL JDBC

CSCI/CMPE Object-Oriented Programming in Java JDBC. Dongchul Kim. Department of Computer Science University of Texas Rio Grande Valley

PESIT Bangalore South Campus

MANTHLY TEST SEPTEMBER 2017 QUESTION BANK CLASS: XII SUBJECT: INFORMATICS PRACTICES (065)

DATABASE DESIGN I - 1DL300

UNIT III - JDBC Two Marks

Chapter 3 DB-Gateways

Chapter 3 DB-Gateways

JDBC 3.0. Java Database Connectivity. 1 Java

COP4540 TUTORIAL PROFESSOR: DR SHU-CHING CHEN TA: H S IN-YU HA

Persistency Patterns. Repository and DAO

C:/Users/zzaier/Documents/NetBeansProjects/WebApplication4/src/java/mainpackage/MainClass.java

3) execute() Usage: when you cannot determine whether SQL is an update or query return true if row is returned, use getresultset() to get the

Database connectivity (II)

1. PhP Project. Create a new PhP Project as shown below and click next

Databases and JDBC. by Vlad Costel Ungureanu for Learn Stuff

Discuss setting up JDBC connectivity. Demonstrate a JDBC program Discuss and demonstrate methods associated with JDBC connectivity

SQL: Programming. Announcements (September 25) Motivation. CPS 116 Introduction to Database Systems. Pros and cons of SQL.

Top 50 JDBC Interview Questions and Answers

Outline. Lecture 10: Database Connectivity -JDBC. Java Persistence. Persistence via Database

Programming in Java

JAVA AND DATABASES. Summer 2018

Part I: Stored Procedures. Introduction to SQL Programming Techniques. CSC 375, Fall 2017

Chapter 3 DB-Gateways

JDBC drivers are divided into four types or levels. The different types of jdbc drivers are:

Accessing databases in Java using JDBC

BUSINESS INTELLIGENCE LABORATORY. Data Access: Relational Data Bases. Business Informatics Degree

SQL: Programming Midterm in class next Thursday (October 5)

ERwin and JDBC. Mar. 6, 2007 Myoung Ho Kim

More Database Programming. CS157A Chris Pollett Nov. 2, 2005.

SQream Connector JDBC SQream Technologies Version 2.9.3

SQL in a Server Environment

Intoduction to JDBC (Java Database Connectivity-- Connecting to DBMS)

Questions and Answers. A. A DataSource is the basic service for managing a set of JDBC drivers.

Instructor: Jinze Liu. Fall 2008

Pieter van den Hombergh. March 25, 2018

SNS COLLEGE OF ENGINEERING, Coimbatore

Databases 2012 Embedded SQL

CSE 308. Database Issues. Goals. Separate the application code from the database

JDBC MOCK TEST JDBC MOCK TEST IV

O ne of the most important features of JavaServer

Database Access with JDBC. Dr. Jens Bennedsen, Aarhus University, School of Engineering Aarhus, Denmark

CMPUT 391 Database Management Systems. JDBC in Review. - Lab 2 -

JDBC BASIC 19/05/2012. Objectives. Java Database Connectivity. Definitions of JDBC. Part 1. JDBC basic Working with JDBC Adv anced JDBC programming

Copyright 2016 Ramez Elmasri and Shamkant B. Navathe

Introduction to SQL & Database Application Development Using Java

Database Application Development

Database connectivity (I) ()

Database Application Development

Chapter 13 Introduction to SQL Programming Techniques

DataBase Lab JAVA-DATABASE CONNECTION. Eng. Haneen El-masry

UNIT-VIII Introduction of Java Database Connectivity

Working with Databases and Java

CHAPTER 2 JDBC FUNDAMENTALS

Introduction to JDBC. JDBC: Java Database Connectivity. Why Access a Database with Java? Compilation. Six Steps. Packages to Import

Calling SQL from a host language (Java and Python) Kathleen Durant CS 3200

Why use a database? You can query the data (run searches) You can integrate with other business systems that use the same database You can store huge

Java Database Connectivity

Introduction to Databases

Preview from Notesale.co.uk Page 21 of 162

Introduction JDBC 4.1. Bok, Jong Soon

Table of Contents. Introduction... xxi

Types of Databases. Types of Databases. Types of Databases. Databases and Web. Databases and Web. Relational databases may also have indexes

Accessing a database from Java. Using JDBC

Database Programming. Week 9. *Some of the slides in this lecture are created by Prof. Ian Horrocks from University of Oxford

SQL and Java. Database Systems Lecture 20 Natasha Alechina

CS506 Web Programming and Development Solved Subjective Questions With Reference For Final Term Lecture No 1

Database Applications (15-415)

Databases and MySQL. COMP 342: Programming Methods. 16 September Databases and MySQL

This lecture. Databases - JDBC I. Application Programs. Database Access End Users

Java Database Connectivity

Chapter 16: Databases

JDBC, Transactions. Niklas Fors JDBC 1 / 38

Copyright 2012, Oracle and/or its affiliates. All rights reserved.

Database Application Development

INTRODUCTION TO JDBC - Revised Spring

Transcription:

Two-Tier Database Design The two-tier is based on Client-Server architecture. The direct communication takes place between client and server. There is no mediator between client and server. Because of tight coupling a 2 tiered application will run faster. Example, Railway reservation application software. In this application, both Database and Server are incorporated with each other, so this technology is called as Client-Server Technology. The Two-tier architecture is divided into two parts: 1) Client Application (Client Tier / presentation layer) 2) Database (Data Tier / data layer) On client application side the code is written for saving the data in the SQL server database. Client sends the request to server and it process the request & send back with data. High portability. Systems are accessible by multiple users from any part of the world Easy to maintain and modification is bit easy as compare to 3 - tier. Communication is faster. Due to their less complexity, easy to build and thereby less expensive. Performance is reduced when clients increase. But generally faster with less number of users due to tight coupling between client and server. 1 AJP(3360701) Prof. Vishal K. Makwana

More suitable to departmental applications with small scale groupware and simple for Web-based. Three-Tier Database Design Three-tier architecture typically comprises Presentation tier, Business tier or data access tier, and Data tier. Three layers in the three tier architecture are as follows: 1) Client layer (Presentation Layer) 2) Business layer (Application Layer / Middle Tier) 3) Data layer 1) Client Layer : o It is also called as Presentation Layer which contains UI part of our application. o This layer is used for the design purpose where data is presented to the user or input is taken from the user. o For this, in web communication HTML is used. 2) Business Layer : o In this layer all business logic written like validation of data, calculations, data insertion etc. o It acts as an interface between Client Layer and Data Layer. o It is also called the mediator, helps to make communication faster between client and data layer. 3) Data Layer : o In this layer actual database is comes in the picture. o It contains methods to connect with database and to perform insert, update, delete, get data from database based on our input data. 2 AJP(3360701) Prof. Vishal K. Makwana

Improved Security Client is not direct access to database. Scalability Each layer is run on different systems. Performance is higher than 2-tier. High degree of flexibility as an additional tier for integration logic. It is fast in communication than 2-tier. Improve Data Integrity. Easy to maintain and modification is bit easy, won t affect other modules. Proved the possibility of integration of servers where one server can communicate with other servers. Increase Complexity/Effort JDBC Drivers ** JDBC Driver is a software component that enables java application to interact with the database. There are four types of JDBC drivers : 1) Type - 1 : JDBC-ODBC Bridge driver (Bridge) 2) Type - 2 : Native-API/partly Java driver (Native) 3) Type - 3 : All Java/Net-protocol driver (Middleware) 4) Type - 4 : All Java/Native-protocol driver (Pure) 1) JDBC-ODBC Bridge driver o In Type - 1 driver, a JDBC bridge is used to access ODBC drivers installed on each client machine. o Using ODBC, requires configuring on your system a Data Source Name (DSN) that represents the target database. o The Type 1 driver translates all JDBC calls into ODBC calls and sends them to the ODBC driver. o ODBC is a generic API. o The JDBC-ODBC Bridge driver is recommended only for experimental use or when no other alternative is available. 3 AJP(3360701) Prof. Vishal K. Makwana

o Easy to use. o Can be easily connected to any database. o The Bridge driver is not written fully in Java, Type 1 drivers are not portable. o Performance degraded because JDBC method call is converted into the ODBC function calls. o The ODBC driver needs to be installed on the client machine. o Not good for the Web. o It is the slowest of all driver types. 2) Native-API/partly Java driver o Type 2 drivers convert JDBC calls into database-specific calls. o This driver is specific to a particular database. o The vendor-specific driver must be installed on each client machine. o If we change the Database, we have to change the native API because it is database specific. o It is not written entirely in java. o Performance increases with a Type 2 driver as compare to Type 1 driver, because it eliminates ODBC's overhead. o The Native driver needs to be installed on the each client machine. o The Vendor client library needs to be installed on client machine. o If we change the Database we have to change the native API as it is specific to a database. o It is platform dependent. 4 AJP(3360701) Prof. Vishal K. Makwana

3) Pure Java/Network-protocol/Middleware driver o Type 3 driver is used three-tier approach to access databases. o It uses middleware (application server) that converts JDBC calls directly or indirectly into the vendorspecific database protocol. o It is fully written in java. o This driver is server-based, so there is no need for any vendor database library to be present on client machines. o It is portable because it is fully written in java. o It is very flexible allows access to multiple databases using one driver. o They are the most efficient amongst all driver types. o The middleware server can provide typical middleware services like caching, load balancing, logging and auditing. o Network support is required on client machine. o Maintenance of Network Protocol driver becomes costly because it requires database-specific coding to be done in the middle tier. o It requires another server application to install and maintain. 4) Database-protocol/Pure Java driver o Type 4 driver is an all Java driver which connects directly to the database. It is also known as Thin Driver. o It is a database driver implementation that converts JDBC calls directly into a vendor-specific database protocol. o The database protocol is vendor specific, the JDBC client requires separate drivers, usually vendor supplied, to connect to different types of databases. 5 AJP(3360701) Prof. Vishal K. Makwana

o It is fully written in Java language. o It is platform dependent. o The client application connects directly to the database server. No translation or middleware layers are used, performance is quite good. o No software is required at client side or server side. o Drivers depend on the Database. JDBC-ODBC Bridge The JDBC-ODBC Bridge is a JDBC driver which implements JDBC operations by translating them into ODBC operations. To ODBC it appears as a normal application program. The bridge implements JDBC for any database for which an ODBC driver is available. The bridge is implemented as the sun.jdbc.odbc Java package and contains a native library used to access ODBC. The bridge is a joint development of Intervolve and Java Soft. The bridge is implemented in Java and uses Java native methods to call ODBC. The bridge is installed automatically with the JDK as package sun.jdbc.odbc. No special configuration is required for the bridge. The bridge is used by opening a JDBC connection using a URL with the odbc subprotocol. Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Before a connection can be established, the bridge driver class, sun.jdbc.odbc.jdbcodbcdriver must be explicitly loaded using the Java class loader. When loaded, the ODBC driver creates an instance of itself and registers this with the JDBC driver manager. 6 AJP(3360701) Prof. Vishal K. Makwana

JDBC used with a Pure Java JDBC driver works well with applets. The bridge driver does not work well with applets. The bridge driver uses the odbc subprotocol. URLs for this subprotocol are of the form: jdbc:odbc:<data-source-name>[<attribute-name>=<attribute-value>]* Example: jdbc:odbc:sybase jdbc:odbc:mydb;uid=me;pwd=secret JDBC API * JDBC API provides the application to JDBC Manager connection. The JDBC API uses a driver manager and database-specific drivers to provide transparent connectivity to heterogeneous databases. Some of the important classes and interface defined in JDBC API are as follows. Class / Interface Driver Manager Connection Statement PreparedStatement Description The DriverManager class loads and configures a database driver on the client side. The Connection class performs connection and authentication to a database server. Syntax: Connection con = DriverManager.getConnection (url, username, password ); Useful when you are using static SQL statement at runtime and return the results by using ResultSet object. The Statement interface cannot accept parameter. The object is created using the createstatement() method of the Connection interface. Syntax: Connection con = DriverManager.getConnection (url, username, password ); Statement stmt = con.createstatement(); The PreparedStatement interface accepts input parameter at runtime. It is subclass of the Statement interface. It is precompiled query which can be executed multiple times. The object is created using the preparestatement() method of Connection interface. Syntax: Connection con = DriverManager.getConnection (url, u_name, password ); String query = insert into emp values(?,?) ; PreparedStatement ps = con.preparestatement(query); ps.setint(1,5); 7 AJP(3360701) Prof. Vishal K. Makwana

CallableStatement ResultSet DatabaseMetaData ResultSetMetaData ps.setstring(2, New Employee ); int n = ps.executeupdate(); The CallableStatement interface can also accept input parameter at runtime. It is used to call the stored procedures and functions. The object is created using the preparecall() method of connection interface. Syntax: Connection cn = DriverManager.getConnection("jdbc:odbc:exm"); CallableStatement cs = cn.preparecall("{call proc1()}"); ResultSet rs= cs.executequery(); The object of ResultSet maintains a cursor pointing to a particular row of data. Initially, cursor points to before the first row. You can use the next()method of ResultSet to move from row to row. DatabaseMetaData interface provides methods to get meta data of a database such as database product name, database product version, driver name, driver version etc. ResultSetMetaData interface provides methods to get meta data of a Resultset such as table name, column name, column type, total number of column etc. JDBC Advantages and Disadvantages Can read any database if proper drivers are installed. No content conversion required. Query and Stored procedure supported. Can be used for both Synchronous and Asynchronous processing. Zero Configurations for Network Computers. Database Connection Identified by URL. Correct drivers need to be deployed for each type of database. Cannot update or insert multiple tables with sequence. 8 AJP(3360701) Prof. Vishal K. Makwana

Develop java program using JDBC * import java.sql.*; public class DemoDatabase { // JDBC driver name and database URL static final String JDBC_DRIVER = "com.mysql.jdbc.driver"; static final String DB_URL = "jdbc:mysql://localhost:3306/student"; // Database credentials static final String USER = "root"; static final String PASS = "root"; public static void main(string[] args) throws Exception { Connection conn = null; Statement stmt = null; //STEP 2: Register JDBC driver Class.forName("com.mysql.jdbc.Driver").newInstance(); //STEP 3: Open a connection System.out.println("Connecting to database..."); conn = DriverManager.getConnection(DB_URL,USER,PASS); //STEP 4: Execute a query System.out.println("Creating statement..."); stmt = conn.createstatement(); String sqlquery; sqlquery = "SELECT Id, Name, Branch FROM demo"; ResultSet rs = stmt.executequery(sqlquery); //STEP 5: Extract data from result set while(rs.next()) { //Retrieve by column name int id = rs.getint("id"); String Name = rs.getstring("name"); String Branch = rs.getstring("branch"); //Display values 9 AJP(3360701) Prof. Vishal K. Makwana

System.out.print("ID: " + id +"\n"); System.out.print("Name: " + Name + "\n"); System.out.print("Branch: " + Branch +"\n"); } //STEP 6: Clean-up environment rs.close(); stmt.close(); conn.close(); } } 10 AJP(3360701) Prof. Vishal K. Makwana