Introduction to Programming with Java

Slides:



Advertisements
Similar presentations
Connecting to Databases. relational databases tables and relations accessed using SQL database -specific functionality –transaction processing commit.
Advertisements

Copyright  Oracle Corporation, All rights reserved. 2 Java and Databases: An Overview.
Java Database Connectivity (JDBC). 2/24 JDBC (Java DataBase Connectivity) - provides access to relational database systems JDBC is a vendor independent.
Database programming in Java An introduction to Java Database Connectivity (JDBC)
JDBC - Java Database Connectivity The objectives of this chapter are: To describe the architecture of JDBC To outline the classes in the java.sql package.
Distributed Application Development B. Ramamurthy.
1 C. Shahabi Application Programming for Relational Databases Cyrus Shahabi Computer Science Department University of Southern California
JDBC Java API for Database Connectivity. Layout of this recitation Introduction to JDBC API JDBC Architecture Understanding the design of JDBC API –Classes.
UFCE4Y UFCE4Y-20-3 Components and Services Julia Dawson.
JDBC / ODBC JDBC is the java API that facilitate interaction of a java application with the DBMS. FIRST APPROACH:
Dale Roberts 8/24/ Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Computer Science, IUPUI
Advance Computer Programming Java Database Connectivity (JDBC) – In order to connect a Java application to a database, you need to use a JDBC driver. –
Getting connected.  Java application calls the JDBC library.  JDBC loads a driver which talks to the database.  We can change database engines without.
Java Database Connectivity Vijayan Sugumaran Department of DIS Oakland University.
© Wang Bin 2004 JDBC ----Java Database Connectivity.
CSE470 Software Engineering Fall Database Access through Java.
Java Database Connectivity (JDBC) Introduction to JDBC JDBC is a simple API for connecting from Java applications to multiple databases. Lets you smoothly.
Java Database Connectivity ASE. Java Database Connectivity (JDBC) l JDBC – provides an interface to Relational Data Sources l JDBC library provides the.
What is JDBC? Java Database Connectivity (JDBC) is an API for the Java programming language that defines how a client may access a database. provides.
Java Utility Classes CS 21b. Some Java Utility Classes Vector Hashtable StringTokenizer * import java.util.*;
1 Java DataBase Connectivity JDBC java.sql.*. 2 Java DataBase Connectivity Draft release of JDBC spec (3/96) Java API for executing SQL statements (Since.
Dr R R DOCSIT, Dr BAMU. Basic Java : Introduction to JDBC 2 Objectives of This Session State what is Java Database Connectivity State different.
JDBC (Java Database Connectivity) SNU OOPSLA Lab. October 2005.
Connecting to Oracle using Java November 4, 2009 David Goldschmidt, Ph.D. David Goldschmidt, Ph.D.
JDBC  The JDBC (Java Database Connectivity) API helps a Java program to access a database in a standard way  JDBC is a specification that tells the.
JDBC. Preliminaries Database Database Collection of data Collection of data DBMS DBMS Database management system Database management system Stores and.
JDBC Enterprise Systems Programming. JDBC  Java Database Connectivity  Database Access Interface provides access to a relational database (by allowing.
COMP201 Java Programming Topic 15: Database Connectivity JDBC Reading: Chapter 4, Volume 2.
WEB/DB1 DATABASE PROGRAMMING 3JDBC by the ASU Scholars.
Java Database Connectivity (JDBC). Topics 1. The Vendor Variation Problem 2. SQL and Versions of JDBC 3. Creating an ODBC Data Source 4. Simple Database.
Java Database Connectivity. Java and the database Database is used to store data. It is also known as persistent storage as the data is stored and can.
JDBC Database Programming in Java Prepared by., Mrs.S.Amudha AP/SWE.
JDBC. Java.sql.package The java.sql package contains various interfaces and classes used by the JDBC API. This collection of interfaces and classes enable.
JDBC CS 124. JDBC Java Database Connectivity Database Access Interface provides access to a relational database (by allowing SQL statements to be sent.
Session 30 Basics of JDBC. Java Simplified / Session 30 / 2 of 33 Review A Swing menu consists of a menubar, menuitems and menus. Trees are used to depict.
JDBC CHAPTER-2. JDBC - Java Database Connectivity. JDBC from Sun Microsystems provides API or Protocol to interact with different databases. With the.
12/6/2015B.Ramamurthy1 Java Database Connectivity B.Ramamurthy.
Java and Databases. JDBC Architecture Java Application JDBC API Data Base Drivers AccessSQL Server DB2InformixMySQLSybase.
UNIT III - JDBC JDBC Overview – JDBC implementation – Connection class – Statements - Catching Database Results, handling database Queries. Networking–
DATABASE CONNECTIVITY TO MYSQL. Introduction =>A real life application needs to manipulate data stored in a Database. =>A database is a collection of.
CSI 3125, Preliminaries, page 1 JDBC. CSI 3125, Preliminaries, page 2 JDBC JDBC stands for Java Database Connectivity, which is a standard Java API (application.
Access Databases from Java Programs via JDBC Tessema M. Mengistu Department of Computer Science Southern Illinois University Carbondale
Basics of JDBC.
Basics of JDBC Session 14.
JDBC (Java Database Connectivity)
JDBC Chapter 1 JDBC Introduction
JDBC - Java Database Connectivity. JDBC provides Java applications with access to most database systems via SQL The architecture and API closely resemble.
Advanced Java Session 5 New York University School of Continuing and Professional Studies.
Ch. NoNameMarks 01AWT24 02Networking18 03JDBC20 04Swing18 05Servlet20 Advance Java Programming.
Database Programming With Java & JDBC Reading: DD Ch. 18, pp al/jdbc/index.html, or anything covering JDBC.
Intro to JDBC Joseph Sant Applied Computing and Engineering Sciences Sheridan ITAL.
Java and database. 3 Relational Databases A relational Database consists of a set of simple rectangular tables or relations The column headings are.
JDBC.
DEPTT. OF COMP. SC & APPLICATIONS
Note: To complete the examples in this section you need access to a database!! Most of the examples work for any database with JDBC drivers. However, connecting.
Interacting with Database
JDBC 15-Apr-18.
JDBC Database Management Database connectivity
Web Technologies IT230 Dr Mohamed Habib.
JDBC 21-Aug-18.
JDBC 15-Nov-18.
Objectives In this lesson, you will learn about:
Mr. Harish Sharma Asst. Professor Dept. of CA & IT SGRRITS Dehradun
Interacting with Database
JDBC – ODBC DRIVERS.
Java Database Connectivity
JAVA DATABaSE CONNECTIVITY
ODBC and JDBC.
Java API for Database Connectivity
Presentation transcript:

Introduction to Programming with Java 1

Chapter 3 Database Programming 2

JDBC Introduction Java Data Base Connectivity(JDBC) is a set of Java APIs used for executing SQL statements. JDBC is much similar to Microsoft’s Open Database Connectivity(ODBC) interface to SQL (Structure Query Language). JDBC API is a set of interfaces and classes to be used for communicating with a database. These interfaces and classes are found in the java.sql package. JDBC allows developers to write client/server properties in Java. JDBC is an interface that stands between the specific database and the Java applications, applets and Servlets. Why JDBC is advantageous with Java? The reason behind that is programs developed with the java programming language and JDBC are platform independent.

Role of JDBC Establishes connection with the database. Sends SQL statements. Process the results Close the connection.

JDBC Characteristics It is call-level SQL interface for Java. It does not restrict the type of queries of passed to an underlying DBMS driver. JDBC mechanisms are simple to understand and use. It provides Java Interface that stays consistent with the rest of the Java system.

The Design of the JDBC At the initial, when the Sun team wants to built the database they extends the standard Java Library to deal with the SQL access to database. Because of this it is impossible task it didn’t take them very long to realize. Afterwards with the help of networking protocols Sun works on the pure java API for the SQL access. Then much of time later the API for the database access become the JDBC API, and the rules for writing drivers were encapsulated in the JDBC driver API.

The Design of the JDBC Simple Design JDBC consists of two layers: the top layer is the JDBC API and the bottom layer is the JDBC driver API Java Application JDBC Driver Manager JDBC/ODBC Bridge ODBC driver Vender supplied JDBC driver Data Base Server

The Design of the JDBC Simple Design The top layered API communicates with the JDBC manager driver API sending it the various SQL statements. Afterwards the manager communicates with the various third party drivers that actually connect to the database and return the information from the query or perform the action specified by the query. The Driver Manager also helps to select the most appropriate driver from the previously loaded drivers when a new open database is connected.

The Design of the JDBC Two – tier and Three – tier Models Two – tier Model Java applications/applets interact directly with the database. This model is referred to as client/server model where user is the client and the machine that has the database is called the server. First the is the JDBC API layer and second is the various third party drivers. Java Application JDBC DBMS Client Machine (GUI) DBMS proprietary protocol Database Server

The Design of the JDBC Two – tier and Three – tier Models A middle tier is introduced in this model. Uses of the middle tier: It collects the SQL statement from the client and hands it over to the database. Receives results from the database to the client. Controls accessing and updating of data. This three tier architecture introduces the Java API calls the middleware layer that in turn accesses the data. Java Applet or HTML browser Client Machine (GUI) HTTP, RMI or COBRA calls Remote Method Invocation Application Server(Java) Server machine (Business Logic) JDBC DBMS proprietary protocol DBMS Database Server eg. SQL Server Oracle

The Design of the JDBC Two – tier and Three – tier Models

JDBC Drivers The JDBC driver translates standard JDBC calls into a network or database protocol or into a database library API call that facilitates communication with the database. This translation layer provides JDBC application with database independence. If the back-end database changes, only the JDBC driver need to be replaced with few code modifications required.

Type 1: JDBC ODBC Bridge Driver Prepared by Allaudin “Hemat”

Type 1: JDBC ODBC Bridge Driver Type 1 drivers act as a “bridge” between JDBC and another database connectivity mechanism such as ODBC. Sun provided a JDBC-ODBC Bridge driver: sun.jdbc.odbc.JdbcOdbcDriver. In this driver the java statements are converted to jdbc statement. JDBC statements call the ODBC by using the JDBC-ODBC Bridge. And finally the query is executed by the database. This bridge is helpful for testing but it cannot recommend for the production used. Type 1 only runs on platforms where ODBC is available. This driver is native code and not Java i.e it is written in C or C++ language not in Java

Type 2: Native-API Driver

Type 2: Native-API Driver Type 2 driver converts JDBC API calls to the native C or C++ API calls. This driver converts the JDBC calls into a database specific call for databases such as SQL, ORACLE etc. This driver communicates directly with the database server. It require some native code to connect to the database. Type 2 drivers are usually faster than type 1 drivers.

Type 3: Network Protocol Driver

Type 3: Network Protocol Driver Or All-Java Driver Type 3 drivers are pure Java drivers that use a proprietary network protocol to communicate with JDBC middleware on the server. The middleware then translates the network protocol to database-specific function call. Type 3 drivers can be deployed over the Internet without client installation. Java----> JDBC statements-----> SQL statements -- --->databases.

Type 4: Database Protocol Driver

Type 4: Database Protocol Driver The JDBC type 4 driver, also known as the Direct to Database Pure Java Driver, is a database driver implementation that converts JDBC calls directly into a vendor- specific database protocol. Written completely in Java, type 4 drivers are thus platform independent. They install inside the Java Virtual Machine of the client.

JDBC API Interfaces in java.sql package Interfaces Methods Array getArray(), getBaseType, getResultSet Blob getBinaryStream, getBytes, length, position CallableStatement registerOutParameter, wasNull Clob getAsciiStream, getCharacterStrea, getSubString, length, position Connection commit, createStatement, isClosed, isReadOnly, rollback DatabaseMetadata getColumns, getConnection, ResultSet absolute, afterLast, beforeFirst, close, next, previous, deleteRow, insertRow Statement cancel, close, executeUpdate, getConnection, getMaxRows Prepared by Allaudin “Hemat”

JDBC API Classes in java.sql package Classes Methods Date getHours, getMinutes, setTime, toString, valueOf DriverManager getConnection, println, registerDriver, getDriver Time getDate, getYear, getMonth, setTime, setYear Prepared by Allaudin “Hemat”

Essential JDBC progam Import the java.sql package import java.sql.*; Steps for using JDBC to access Database Import the java.sql package import java.sql.*; Load a JDBC driver All drivers are required to register themselves at load time. try { Class.forName("com.mysql.jdbc.Driver"); } catch(Exception e) System.out.println("Unable to load the driver..."); Prepared by Allaudin “Hemat”

Essential JDBC progam Connect to the database Steps for using JDBC to access Database Connect to the database The JDBC DriverManager Class defines objects which can connect java applications to a JDBC driver, which is backbone of JDBC architecture. getConnection() method used to establish connection. Connection con = DriverManager.getConnection("jdbc:mysql://localhost/db_hms","root",""); Create a statement To execute SQL statements, we need to initiate a statement object form our connection object by using createstatement() method. con.createstatement(); Prepared by Allaudin “Hemat”

Essential JDBC progam Steps for using JDBC to access Database There are 3 types of statement Statement: Simple SQL queries without parameters. Prepared Statement: Executed precompiled SQL queries with or without parameters. PrepareStatement objects are precompile SQL statement. Callable Statement: Execute a call to a database stored procedure. CallableStatement objects are SQL stored procedure call statements Execute the statement stmt.exeuteUpdate(String str); To exeute query executeQuery() methods comes in picture. The executeQuery() method takes SQL query as a parameter and returns the result of the query as the ResultSet Object.

Essential JDBC progam Retrieve the result Steps for using JDBC to access Database Result rs1=stmt.executeQuery("Select id, name,salary from teacher where department="Computer" "); Retrieve the result while(rs1.next()) { int x=rs1.getInt(1); String s=rs1.getString("b"); } The above is just an example where rs1 is a ResutlSet object. The results of the SQL statements are stored in a ResultSet object. To retrieve the data from this object, we use the getXXX methods. To move to the next row, we make use of rs1.next() method.

Essential JDBC progam Close the statement and the connection: Steps for using JDBC to access Database Close the statement and the connection: stmt.close(); con.close(); con.commit(); Prepared by Allaudin “Hemat”

Essential JDBC progam Steps for using JDBC to access Database Write JDBC program to accept employee information from database and update first tuple and display after updating. Write JDBC program that insert the following details to a teacher table having the following structure. teacher_name, department, salary