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.

Slides:



Advertisements
Similar presentations
Java Database Connectivity (JDBC). 2/24 JDBC (Java DataBase Connectivity) - provides access to relational database systems JDBC is a vendor independent.
Advertisements

JDBC. Java Database Connectivity (JDBC) Use the java.sql package to query and update the database. JDBC is an API that allows java to communicate with.
Distributed Application Development B. Ramamurthy.
15-Jun-15 JDBC. JDBC is a Sun trademark It is often taken to stand for Java Database Connectivity Java is very standardized, but there are many versions.
1 Lecture 05: Database Programming (JDBC). 2 Outline JDBC overview JDBC API Reading: Chapter 10.5 Pointbase Developer Manual.
JDBC Overview Autumn 2001 Lecturer: C. DeJong. Relational Databases widespread use used via SQL (Structured Query Language) freely available powerful.
1 C. Shahabi Application Programming for Relational Databases Cyrus Shahabi Computer Science Department University of Southern California
Java Database Connectivity (JDBC) java.sql package to query and update the database. JDBC is an API that allows java to communicate with a database server.
1 Lecture 29 More on JDBC Overview  Objectives of this lecture  JDBC and its Drivers  Connecting to Databases (Java’s Connection class)  Querying a.
CSE470 Software Engineering Fall Database Access through Java.
ODBC and JDBC What are they – libraries of function calls that support SQL statements Why do we need them –Provide a way for an application to communicate.
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:
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 (JDBC) Francisco Pajaro Saul Acosta Nahum Quezada Manuel Rubio.
1 Java Database Connection (JDBC) There are many industrial-strength DBMS's commercially available in the market. Oracle, DB2, and Sybase are just a few.
Helena Pomezná, ciz034 St. skupina: L392 FEI, VŠB-TUO Ak. rok. 2002/2003 Download:
Java Database Connectivity Vijayan Sugumaran Department of DIS Oakland University.
JDBC. What is JDBC JDBC is an acronym for –Java Data Base Connectivity. It allows java/jsp program to connect to any database.
© Wang Bin 2004 JDBC ----Java Database Connectivity.
CSE470 Software Engineering Fall Database Access through Java.
Beginning Databases with JDBC Mike Bradley Adapted from and notes by Kevin Parker, Ph.D.
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.
MySQL, Java, and JDBC CSE 3330 Southern Methodist University.
JDBC Tutorial MIE456 - Information Systems Infrastructure II Vinod Muthusamy November 4, 2004.
JDBC (Java Database Connectivity) SNU OOPSLA Lab. October 2005.
Designing and Developing WS B. Ramamurthy. Plans We will examine the resources available for development of JAX-WS based web services. We need an IDE,
Introduction to JDBC Michelle Lee, Ye Wu & Jeff Offutt SWE 432 Design and Implementation of Software for the Web.
JDBC. JDBC stands for Java Data Base Connectivity. JDBC is different from ODBC in that – JDBC is written in Java (hence is platform independent, object.
JDBC – Java DataBase Connectivity. JDBC API Overview JDBC is Java API that allows the Java programmers to access database management system from Java.
JDBC Enterprise Systems Programming. JDBC  Java Database Connectivity  Database Access Interface provides access to a relational database (by allowing.
Accessing Database using JDBC. JDBC Objectives Gain basic knowledge of Java JDBC Become familiar with the basics of interacting with a database using.
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.
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.
12/6/2015B.Ramamurthy1 Java Database Connectivity B.Ramamurthy.
JDBC
Java and Databases. JDBC Architecture Java Application JDBC API Data Base Drivers AccessSQL Server DB2InformixMySQLSybase.
COMP 321 Week 4. Overview Normalization Entity-Relationship Diagrams SQL JDBC/JDBC Drivers hsqldb Lab 4-1 Introduction.
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.
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.
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.
R EMAINING TOPICS IN JDBC Sagun Dhakhwa. O VERVIEW Introduction to JDBC API Types of JDBC Drivers Introduction to ODBC.
DEPTT. OF COMP. SC & APPLICATIONS
Interacting with Database
Lec - 14.
JDBC Database Management Database connectivity
Advanced Web Automation Using Selenium
Mr. Harish Sharma Asst. Professor Dept. of CA & IT SGRRITS Dehradun
Interacting with Database
JDBC – ODBC DRIVERS.
Java Database Connectivity
JAVA DATABaSE CONNECTIVITY
Java API for Database Connectivity
Presentation transcript:

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 program interface ) for database-independent connectivity between the Java programming language and a wide range of databases. The JDBC library includes APIs for each of the tasks commonly associated with database usage: Making a connection to a database Creating SQL or MySQL statements Executing that SQL or MySQL queries in the database Viewing & Modifying the resulting records

CSI 3125, Preliminaries, page 3 JDBC Architecture:

CSI 3125, Preliminaries, page 4 JDBC Architecture: The JDBC API uses a driver manager and database-specific drivers to provide connectivity to different databases. The JDBC driver manager ensures that the correct driver is used to access each data source. JDBC drivers are normally provided by specific data base vendors and they act as the interface to communicate between JDBC and a database

CSI 3125, Preliminaries, page 5 JDBC Architecture: JDBC DriverManager DriverManager manage the different types of JDBC database driver. DriverManager's responsibility to load all the drivers found in the system properly. Driver Manager's duty to choose the most appropriate driver from the previously loaded drivers. JDBC DriverManager is a class in JDBC API and objects of this class can connect Java applications to a JDBC driver

CSI 3125, Preliminaries, page 6 JDBC Architecture: JDBC Driver JDBC Drivers are written by vendors and must support the basic features of the JDBC specification It serves as the interface to communications between JDBC and a database. It processes JDBC methods invocations, sends SQL statements to a specific data source, and returns results back to the application.

CSI 3125, Preliminaries, page 7 JDBC Architecture: Driver Type JDBC DRIVERS TYPES: JDBC driver implementations vary because of the wide variety of operating systems and hardware platforms in which Java operates. Sun has divided the implementation types into four categories: Type 1 - JDBC-ODBC Bridge Driver Type 2 - JDBC-Native Bridge Driver Type 3 - JDBC-Net Bridge Driver (Net pure Java) Type 4 - Direct JDBC Driver

CSI 3125, Preliminaries, page 8 JDBC Architecture: Driver Type Type 1 - JDBC-ODBC Bridge Driver In a Type 1 driver, a JDBC bridge is used to access ODBC drivers installed on each client machine. Since there are different types of databases and each of them has its own way of dealing with SQL queries and to return the results, ODBC provides identical results regardless of the type of database ODBC driver is included in the Java 2 SDK within the sun.jdbc.odbc package

CSI 3125, Preliminaries, page 9 JDBC Architecture: Driver Type Type 1 - JDBC-ODBC Bridge Driver In order to use Type 1 drivers ODBC must be installed on the computer having the driver Type 1 drivers are not suitable for developing web applications as they need software to be installed on client machine Using ODBC requires configuring on the system a Data Source Name (DSN) that represents the target database. ODBC is developed by Microsoft that enables communication between databases clients running on Windows and DBMS

CSI 3125, Preliminaries, page 10 JDBC Architecture: Driver Type Type 1 - JDBC-ODBC Bridge Driver

CSI 3125, Preliminaries, page 11 JDBC Architecture: Driver Type Type 2 - JDBC-Native Bridge Driver Type 2 drivers use the Java Native Interface (JNI) to make calls to a local database library API. This driver converts the JDBC calls into a database specific call for databases such as SQL, ORACLE etc i.e. This driver is specific to a particular database.

CSI 3125, Preliminaries, page 12 JDBC Architecture: Driver Type Type 2 - JDBC-Native Bridge Driver This driver communicates directly with the database server and requires some native code to connect to the database. These drivers typically provided by the database vendors and used in the same manner as the JDBC- ODBC Bridge, driver must be installed on each client machine. If we change the Database we have to change the native API driver as it is specific to a database. Cannot be used for the Internet.

CSI 3125, Preliminaries, page 13 JDBC Architecture: Driver Type Type 3 - JDBC-Net Bridge Driver (Net pure Java) The JDBC type 3 driver, also known as the network- protocol driver The middle-tier (application server) converts JDBC calls directly or indirectly into the vendor-specific database protocol. Type 3 database requests are passed through the network to the middle-tier server and the middle-tier server can in turn use Type1, Type 2 or Type 4 drivers to translate the request to the database. Since the communication between client and the middleware server is database independent, there is no need for the vendor db library on the client machine

CSI 3125, Preliminaries, page 14 JDBC Architecture: Driver Type Type 3 - JDBC-Net Bridge Driver (Net pure Java)

CSI 3125, Preliminaries, page 15 JDBC Architecture: Driver Type Type 4 – Direct JDBC Driver Type 4 drivers are pure Java drivers to communicate directly with the database. Like Type 3 drivers, they do not require native database libraries and can be deployed over the Internet without client installation. One drawback to Type 4 drivers is that they are database specific. Unlike Type 3 drivers, if your back-end database changes, you may have to purchase and deploy a new Type 4 driver. However, because Type 4 drivers communicate directly with the database engine rather than through middleware or a native library, they are usually the fastest

CSI 3125, Preliminaries, page 16 JDBC Architecture: Driver Type Type 4 – Direct JDBC Driver In a Type 4 driver, a pure Java-based driver that communicates directly with vendor's database through socket connection. This is the highest performance driver available for the database and is usually provided by the vendor itself. This kind of driver is extremely flexible, you don't need to install special software on the client or server. Driver completely written in Java

CSI 3125, Preliminaries, page 17 Driver Usage When accessing one type of database, such as Oracle, Sybase, or IBM, the preferred driver type is 4. when Java application is accessing multiple types of databases at the same time, type 3 is the preferred driver. Type 2 drivers are useful in situations where a type 3 or type 4 driver is not available yet for your database. The type 1 driver is used for development and testing purposes only.

CSI 3125, Preliminaries, page 18 JDBC The java.sql package contains the entire set of interfaces and classes defined by JDBC API that sends SQL (Structured Query Language) statements to relational databases and retrieves the results of executing those SQL statements.

CSI 3125, Preliminaries, page 19 JDBC Program Steps Step 1: Create a table check(id:PK, u_name:Text, age:number) in the any database such as(MS Access, Oracle, MySQL,SQL..) Create Data Source Name, using ODBC Step 2: Import JDBC Packages (import java.sql.*;) Step 3: Register JDBC Driver: Load database specific driver by calling Class.forName("Name of the driver class file"). Syntax - Class.forName("sun.jdbc.odbc.jdbcOdbcDriver"); The Class class is avaliable in java.lang package. The database driver is different for each DBMS. The type1 driver is same for all the databases.

CSI 3125, Preliminaries, page 20 JDBC Program Steps Step 4: Once JDBC driver is loaded, The DriverManager is used to establish connection with database server using getConnection() method. The Connection type is the return type of getConnection(). The DriverManager and Connection are available in java.sql package. Syntax - Connection con=DriverManager.getConnection("URL","userid","password "); URL - jdbc:odbc: Userid : user Id of database (Optional) Password : Password of database (Optional)

CSI 3125, Preliminaries, page 21 JDBC Program Steps Step 5: Creating a jdbc Statement object Statement statement = dbConnection.createStatement(); eg: Statement stmt = con.createStatement() ; String query = "SELECT col1, col2, col3 FROM sometable”; Step 6: ResultSet resultSet = statement.executeQuery(query);

CSI 3125, Preliminaries, page 22 JDBC Prog Database: db.mdb, DSN: ds, table check(id:Pk,u_name:text,age:number) import java.sql.*; public class db6 {public static void main(String[] args) {try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection con = DriverManager.getConnection("jdbc:odbc:ds"); Statement stmt = con.createStatement(); ResultSet rs = stmt.executeQuery("select * from check"); while (rs.next()) System.out.println("Name " + rs.getString("u_name") + " Age " +rs.getString("age")); con.close(); } catch (SQLException e) {System.out.println(e);} catch (Exception e) {System.out.println(e);} }

CSI 3125, Preliminaries, page 23 JDBC Prog Database: db.mdb, DSN: ds, table check(id:Pk,u_name:text,age:number) Insert a row in the table Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection con = DriverManager.getConnection("jdbc:odbc:ds"); Statement stmt = con.createStatement(); stmt.executeUpdate("insert into check (u_name,age) values('appu',23)");

CSI 3125, Preliminaries, page 24 JDBC Prog insert & display Database: db.mdb, DSN: ds, table check(id:Pk,u_name:text,age:number) import java.sql.*; public class db7 {public static void main(String[] args) throws Exception { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection con = DriverManager.getConnection("jdbc:odbc:ds"); Statement stmt = con.createStatement(); stmt.executeUpdate("insert into check (u_name,age) values('appu',23)"); ResultSet rs=stmt.executeQuery("select *from check"); while (rs.next()) System.out.println("Name " + rs.getString("u_name") + " Age " +rs.getString("age")); con.close(); }

CSI 3125, Preliminaries, page 25 JDBC Prog update Database: db.mdb, DSN: ds, table check(id:Pk,u_name:text,age:number) Connection con = DriverManager.getConnection("jdbc:odbc:ds"); Statement stmt = con.createStatement(); stmt.executeUpdate("update check set u_name='zoo' where u_name='appu'");

CSI 3125, Preliminaries, page 26 JDBC Prog delete Database: db.mdb, DSN: ds, table check(id:Pk,u_name:text,age:number) Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection con = DriverManager.getConnection("jdbc:odbc:ds"); Statement stmt = con.createStatement(); stmt.executeUpdate("delete from check where u_name='zoo'");

CSI 3125, Preliminaries, page 27 JDBC Prog delete Database: db.mdb, DSN: ds, table check(id:Pk,u_name:text,age:number)

CSI 3125, Preliminaries, page 28 JDBC Prog delete Database: db.mdb, DSN: ds, table check(id:Pk,u_name:text,age:number)

CSI 3125, Preliminaries, page 29 JDBC Prog import java.sql.*; public class database {public static void main(String[] args) { Connection conn=null; try{ Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); } catch(ClassNotFoundException e){ System.out.println(e);} try{ conn = DriverManager.getConnection("jdbc:odbc:ds"); } catch(SQLException se) { System.out.println(se);} System.out.println("connection is successful!!!"); try{ String selectSQL = "select * from check"; Statement stmt = conn.createStatement(); ResultSet rset = stmt.executeQuery(selectSQL); while(rset.next()){ System.out.println("ID: " + rset.getString(1) + " NAME: " +rset.getString(2) + " Age: "+rset.getString(3) ); } stmt.close(); } catch(SQLException se) { System.out.println(se);} }