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

Similar documents
Unit 3 - Java Data Base Connectivity

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

JDBC 3.0. Java Database Connectivity. 1 Java

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

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

Unit 2 JDBC Programming

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

Logging and Recovery. 444 Section, April 23, 2009

Persistency Patterns. Repository and DAO

UNIT-3 Java Database Client/Server

Java Database Connectivity

Introduction to Databases

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

DATABASE DESIGN I - 1DL300

Databases 2012 Embedded SQL

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

JAVA AND DATABASES. Summer 2018

Java Database Connectivity

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

Top 50 JDBC Interview Questions and Answers

Database Application Development

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

Three-Tier Architecture

SQL in a Server Environment

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

SQL DML and DB Applications, JDBC

Database connectivity (II)

UNIT III - JDBC Two Marks

JDBC - INTERVIEW QUESTIONS

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

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

Accessing databases in Java using JDBC

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

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

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

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

Application Programming for Relational Databases

Instructor: Jinze Liu. Fall 2008

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

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

JDBC MOCK TEST JDBC MOCK TEST IV

Lecture 2. Introduction to JDBC

JDBC [Java DataBase Connectivity]

Introduction to JDBC. Lecture 12

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

INTRODUCTION TO JDBC - Revised Spring

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

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

INTRODUCTION TO JDBC - Revised spring

O ne of the most important features of JavaServer

PERSİSTENCE OBJECT RELATİON MAPPİNG

The Web Application Developer s. Red Hat Database. View. October 30, Webcast. Patrick Macdonald, Fernando Nasser. Red Hat Database Engineering

Java Database Connectivity (JDBC) 25.1 What is JDBC?

Transactions & Concurrency Control

Databases and JDBC. by Vlad Costel Ungureanu for Learn Stuff

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

Java Database Connectivity

Enterprise Java Unit 1- Chapter 6 Prof. Sujata Rizal

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

Database Application Development

Java E-Commerce Martin Cooke,

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

SNS COLLEGE OF ENGINEERING, Coimbatore

PESIT Bangalore South Campus Hosur road, 1km before Electronic City, Bengaluru -100 Department of Information Science and Engineering

Database Application Development

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

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

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

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

Database Access. Rick Cattell JavaSoft

Database Application Development Part 2 - Chapter

Working with Databases and Java

Database in Applica.on Development. Debapriyo Majumdar DBMS Fall 2016 Indian Statistical Institute Kolkata

Introduction to SQL & Database Application Development Using Java

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

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

Accessing a database from Java. Using JDBC

JDBC, Transactions. Niklas Fors JDBC 1 / 38

Distributed Systems Project 5 Assigned: Friday, April 6, 2012 Due: Friday, April 20, 11:59:59 PM

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

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

Allenhouse Institute of Technology (UPTU Code : 505) OOT Notes By Hammad Lari for B.Tech CSE V th Sem

PESIT Bangalore South Campus

Real SQL Programming 1

Server-side Web Programming

Programming in Java

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

Visit for more.

Database Application Programs PL/SQL, Java and the Web

Socket Programming(TCP & UDP) Sanjay Chakraborty

Copyright 2016 Ramez Elmasri and Shamkant B. Navathe

SQream Connector JDBC SQream Technologies Version 2.9.3

Chapter 3 DB-Gateways

Pieter van den Hombergh. March 25, 2018

JDBC Installation Transactions Metadata

Oracle Exam 1z0-809 Java SE 8 Programmer II Version: 6.0 [ Total Questions: 128 ]

Overview. Database Application Development. SQL in Application Code. SQL in Application Code (cont.)

Database Application Development

Database Application Development

Chapter 3 DB-Gateways

Transcription:

JDBC Stands for Java Database Connectivity, is an API specification that defines the following: 1. How to interact with database/data-source from Java applets, apps, servlets 2. How to use JDBC drivers 3. How to write JDBC drivers The main point of JDBC is database interoperability: By using JDBC API to database access you can change the underlaying database driver(or engine) without modifying the application. 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 JDBC API JDBC Driver Manager JDBC JDBC JDBC Driver Driver Driver ODBC Oracle Access JDBC Driver Types : Four types of JDBC drivers. Type 1: The JDBC-ODBC Bridge Java App JDBC-ODBC Bridge is provided as a part of JDK higher than 1.1 JDBC-ODBC Bridge bridge is part of sun.jdbc.odbc package JDBC-ODBC Library ODBC Driver ODBC Database Uses: 1. Quick system prototype

2. Three-tier database design 3. Database systems that provide only ODBC driver and not JDBC 4. Low-cost database solution where you have an ODBC driver Limitations: 1. Bridge was developed as a prototyping and marketing tool and one should not use it for mission-critical application if native JDBC driver is available. 2. Bridge uses C language code so can not be used in untrusted applets. So this depends on operating systems. 3.Since Bridge uses existing ODBC driver any bug in the driver would be carried-forward 4.If a thing that is not possible in ODBC driver that features will not be available using bridge Type 2: Java to NATIVE API It uses local native library provided by a vendor to communicate directly to the database. This driver has many same restrictions as bridge because it uses native library but they are faster than bridge. It can not be used in untrusted applets. This native library must be installed on each machine which uses the driver. Java applet, apps JDBC Driver Native Database Library Database Low-cost database solution where we are already using a major database system that provides a type 2 driver with their database. Type 3: Java to Proprietary Network Protocol Java application, applet, servlets

Type 3 JDBC driver Internet/Proprietary Network protocol Java Middleware JDBC Driver DATABASE This type of JDBC driver is by far most flexible. Used in n-tier application and can be deployed over Internet. Type 3 driver are pure Java and communicate with some type of middle tier via a proprietary network protocol created by the driver vendor. This middle tier will most likely reside on a Web or database server and, in turn, communicate with the database. Uses: Web-based applet that do not require any pre-installation or configuration Secure systems where the database producer will be protected behind middle tier Flexible solution where many different database products in uses Clients require a small footprint-size of type 3 drivers is much smaller than all other types. Type 4: Java to Native database protocol Type 4 drivers are pure Java drivers that communicate directly wih database engine via its native protocol. This driver can be deployed over Internet. This driver can give higher performance as compared to first two. Java Apps, applets, Servlets Uses: Performance-critical apps Type 4 JDBC Driver In environments where only one database product is in use Database Engine Web-deployed applets DATABASE

The JDBC program flow 1. Establish a connection to the database 2. Execute a SQL statement 3. Process the result 4. Disconnect from the database Loading Drivers Loading the driver or drivers you want to use is very simple and involves just one line of code. If, for example, you want to use the JDBC-ODBC Bridge driver, the following code will load it: Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Your driver documentation will give you the class name to use. For instance, if the class name is jdbc.driverxyz, you would load the driver with the following line of code: Class.forName("jdbc.DriverXYZ"); 1. Establish a connection to the database JDBC connections are specified by URL string which has format: Jdbc:<subprotocol>:URL e.g. jdbc:odbc:dsnname Connection con = DriverManager.getConnection( jdbc:odbc:msaccess ); If we are using the JDBC-ODBC Bridge driver, the JDBC URL will start with jdbc:odbc:. The rest of the URL is generally your data source name or database system. Connection returned by the method DriverManager.getConnection is an open connection you can use to create JDBC statements that pass your SQL statements to the DBMS.

2. Execute SQL Statement Statement class Prepared Statement to create object using SQL Statement Callable Statement to call backend procedure Public class simple query pubic static void main(string args[]) String drivername = sun.jdbc.odbc.jdbcodbcdriver ; String connectionurl = jdbc:odbc:accessdb ; Class.forName(driverName).newInstance(); Connection con = DriverManager.getConnection(connectionURL); Establishing a Database Connection: java.sql package Public static synchronized Connection getconnection(string url, String user, String password) throws SQLException Con = DriverManager.getConnection( jdbc:odbc:accessdsn, uid, pwd ) Interacting with Database using SQL: To execute SQL command using a JDBC connection, we must create a JDBC Statement object. By Connnection.createStatement() method. Table : Stud_id integer, Stud_Name; Import java.sql.*; public class FirstSQLApp public static void main(string args[]) Connection con = null; try Class.forName( sun.jdbc.odbc.jdbcodbcdriver ); Con= DriverManager.getConnection( jdbc.odbc.accdsn,, );

Statement s1 = con.createstatement(); Statement.excuteUpdate( Create Table Stud + (stud_id INTEGER, stud_name CHAR(30)) ); catch(sqlexception e1) System.err.println(e1.getMessage()); catch(exception e2) System.err.print(e1.getMessage()); finally try if(con!=null) con.close(); catch (Exception e) Inserting Data into Table: Insert SQL Statement s1.executeupdate( insert into Stud + values (1, stud1 ) ); Selecting Data from Table: SQL Select JDBC ResultSet object is used as client-side cursor to select data. try ResultSet rs = statement.executequery( Select * from STUD ); while(rs.next()) System.out.println( Stud Id = + rs.getstring( stud_id )); System.out.println( Stud Id = + rs.getstring( name )); rs.close(); getxxx methods

rs.getfloat(), rs.getint() Updating Tables Statement.executeUpdate( UPDate stud + SET name = stud2 + Where stud_id = 1 ); Deleting from Table Statement.executeUpdate( DELETE from stud + Where stud_id = 2 ); Types of Statements: 1. PreparedStatements - to create parameterised query 2. Callable statements to execute stored procedure Prepared Statements PreparedStatement object contains not just an SQL statement, but an SQL statement that has been precompiled. We can use them for, which take parameters and the same SQL statement is being executed again and again. Creating a PreparedStatement Object PreparedStatement s2 = con.preparestatement( "UPDATE Stud SET name =? Where stud_id =?"); updatesales.setstring(1, stud3 ); updatesales.setint(2, 1); s2.executeupdate(); Return Value for PreparedStatement Object int n = s2.executeupdate();

Stored Procedures Calling a Stored Procedure from JDBC CallableStatement cs = con.preparecall("call SHOW_STUDLIST"); ResultSet rs = cs.executequery(); CallableStatement is subclass of PreparedStatement so callable statement can also take input parameter in the same way.

Transactions A transaction is a set of one or more statements that are executed together as a unit, so either all of the statements are executed, or none of the statements is executed. Disabling Auto-commit Mode When a connection is created, it is in auto-commit mode. This means that each individual SQL statement is treated as a transaction and will be automatically committed right after it is executed. con.setautocommit(false); Committing a Transaction con.setautocommit(false);. PreparedStatement/Statement/CallableStatement executeupdate. con.commit(); con.setautocommit(true); RollingBack a Transaction Con.RollBack(); Transaction Isolation Level: To avoid conflicts during a transaction, a DBMS will use locks, mechanisms for blocking access by others to the data that is being accessed by the transaction. Default Isolation Level: TRANSACTION_READ_COMMITTED: DBMS will not allow dirty reads to occur Method: GetTransactionIsolation - to get Isolation Level of DBMS SetTransactionIsolation - to set Isolation Level of DBMS