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.

Slides:



Advertisements
Similar presentations
CE203 - Application Programming Autumn 2013CE203 Part 51 Part 5.
Advertisements

Java Database Connectivity (JDBC). 2/24 JDBC (Java DataBase Connectivity) - provides access to relational database systems JDBC is a vendor independent.
Basic JDBC Celsina Bignoli What is JDBC Industry standard for database- connectivity between the Java language and a wide range of.
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.
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.
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 JDBC Java Database Connectivity. 2 c.pdf
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.
JDBC / ODBC JDBC is the java API that facilitate interaction of a java application with the DBMS. FIRST APPROACH:
Introduction to JDBC (Java Database Connectivity).
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.
June 1, 2000 Object Oriented Programming in Java (95-707) Java Language Basics 1 Lecture 8 Object Oriented Programming in Java Advanced Topics Java Database.
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.
Database Programming in Java Corresponds with Chapter 32, 33.
Java Utility Classes CS 21b. Some Java Utility Classes Vector Hashtable StringTokenizer * import java.util.*;
JDBC (Java Database Connectivity) SNU OOPSLA Lab. October 2005.
CONTROLPANEL Java.sql package This package provides the APIs for accessing and processing data which is stored in the database especially relational.
JDBC Java and Databases, including Postgress. JDBC l Developed by Industry leaders l Three main goals: –JDBC should be an SQL-level API –JDBC should capitalize.
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.
1 JDBC Aum Amriteshwaryai Namah. 2 2 JDBC – Java DataBase Connectivity.
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.
JDBC – Java Database Concentricity
Chapter 25 Databases. Chapter Scope Database concepts Tables and queries SQL statements Managing data in a database Java Foundations, 3rd Edition, Lewis/DePasquale/Chase25.
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.
Copyright © 2002 ProsoftTraining. All rights reserved. Building Database Client Applications Using JDBC 2.0.
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.
JDBC CHAPTER-2. JDBC - Java Database Connectivity. JDBC from Sun Microsystems provides API or Protocol to interact with different databases. With the.
Presentation On How To Create Connection To A Database.
12/6/2015B.Ramamurthy1 Java Database Connectivity B.Ramamurthy.
Li Tak Sing COMPS311F. Database programming JDBC (Java Database Connectivity) Java version of ODBC (Open Database Connectivity) ODBC provides a standard.
DATABASE CONNECTIVITY TO MYSQL. Introduction =>A real life application needs to manipulate data stored in a Database. =>A database is a collection of.
JDBC and SQLJ CIS 612 Spring JDBC JDBC is an API that enables database access from Java programs JDBC for DB access provides ◦ Portability across.
Database Access Using JDBC BCIS 3680 Enterprise Programming.
JDBC CS 260 Database Systems. Overview  Introduction  JDBC driver types  Eclipse project setup  Programming with JDBC  Prepared statements  SQL.
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 Session 14.
JDBC - Java Database Connectivity. JDBC provides Java applications with access to most database systems via SQL The architecture and API closely resemble.
Database Programming With Java & JDBC Reading: DD Ch. 18, pp al/jdbc/index.html, or anything covering JDBC.
1 JDBC – Java Database Connectivity CS , Spring 2010.
Intro to JDBC Joseph Sant Applied Computing and Engineering Sciences Sheridan ITAL.
JDBC Statements The JDBC Statement, CallableStatement, and PreparedStatement interfaces define the methods and properties that enables to send SQL or PL/SQL.
CS320 Web and Internet Programming Database Access with JDBC Chengyu Sun California State University, Los Angeles.
Interacting with Database
Lec - 14.
JDBC 15-Apr-18.
JDBC Database Management Database connectivity
JDBC – Java Database Connectivity
CS320 Web and Internet Programming Database Access with JDBC
JDBC 21-Aug-18.
JDBC 15-Nov-18.
Objectives In this lesson, you will learn about:
Interacting with Database
JDBC – ODBC DRIVERS.
Java Database Connectivity
JDBC Example.
Presentation transcript:

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 oriented robust etc.), while – ODBC is written in C, is not object oriented. However, both JDBC and ODBC are based on the X/Open SQL Command Level Interface (CLI). Sun provides a JDBC ODBC bridge, which enables one to connect painlessly to ODBC data sources.

JDBC /ODBC Bridge We use MS Access, a 32 bit ODBC driver. First create an access database file e.g. filename.mdb. Create a 32-bit ODBC driver via which JAVA will communicate with the database tables.

Steps to create 32-bit Driver Select the ODBC data source from control panel; ODBC administrator interface will open up. This allows the creation of several types of Data Source Name (DSN). Select System DSN. Add Microsoft Access Driver. Name your DSN. Select the database.

DSN is aware of the existence of MS Access Database. Java cant communicate directly with ODBC driver as this driver has been created using different techniques other than Java. Hence, using appropriate java code and associated Java driver’s a bridge must be set up between 32-bit ODBC driver and JAVA UI. This is a JDBC:ODBC bridge.

Java Application UI 32-bit ODBC Access Driver Database and Tables Java Driver Manager Responsible for setting up JDBC:ODBC bridge Java Connectivity Model

Java application communicates with a driver manager using java code. The DriverManager is a class that belongs to Java.sql package. The driver manager communicates with 32-bit ODBC driver. Then 32-bit ODBC driver communicates with Access database.

Java Driver Manager ODBC System DSN Database and Tables Creates a Connection Object which communicates with ODBC driver and in turns spawns a Result Set object to hold the record set returned by the query made to database table. The ResultSet Object holds the recoreds retrieved from the database table. Data from this result set object used in java application.

Driver Manager Java’s Driver Manager controls interaction between UI and database. It can support multiple drivers to different DBMS systems. It loads the requested driver and provides support for managing database connections.

It can perform- Locate a driver for a particular database. Process JDBC initialization calls. Provides entry point to JDBC functions for each specific driver. Performs validation for JDBC calls.

Once an ODBC driver has been created and registered in Windows registry on the computer, the java DriverManager class can be used to invoke this driver. The DriverManager class works as the interface between UI and ODBC driver.

The DriverManager provides method to establish a connection to specified database- getConnection(JDBC:ODBC:DSN Name,, ) On success it spawns a Connection object which represent a physical connection to the database. After physical connection establishment, initialize the SQL queries. Recoreds retrieved from database which will be held in ResultSet object. To see the data held within this object its each row must be read. next() in loop fetches one row at a time. It returns false when there are no more rows in ResultSet object.

Java.sql package DriverManager class is in java.sql package. getConnection(URL,user,pwd) establish a connection. On success, it returns a Connection object. A connection object represents a connection with a database. Methods used with this objects are- void close()to release database resources. Statement CreateStatement()returns a new statement object that can be used to execute SQL statements. On error both throws SQLException.

Statement Interface Statement object execute SQL statements that are static and obtain results in ResultSet object. Some methods are- ResultSet executeQuery(String SQLquery) –executes a SQL statement that returns a single ResultSet object. On error SQLException is thrown. int executeUpdate(String SQLquery) –It executes SQL UPDATE, DELETE, or INSERT statement. A row count is returned by these statement. Zero returned if no row is returned. On error SQLException is thrown.

PreparedStatement Interface It extends Statement interface. If an application requires multiple executions of any SQL statement, it is pre-compiled and stored in a PreparedStatement object. This object can then be used by many times to execute the statement. Some methods are- executeQuery() executeUpdate() –SQL statements that don’t return anything such as DDL can also be executed. On Error SQLException is thrown

ResultSet Interface ResultSet object watches a single row of data at a time. This row of data is called its current row. When a SQL statement is re-executed the current ResultSet is closed and a new ResultSet object is created. Some methods are- void close() void getString() –Gets the value of a column in the current row as a Java String. next() –A ResultSet is initially positioned before its first row. The first call to next() makes the first row the current row. The second call makes the second row the current row, etc.

1. Choosing an appropriate driver For every specific database connection established, an appropriate driver is required to allow JDBC API to manage the connection. Java program code is used to force a particular driver to be loaded. Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”);

2. Create a connection getConnection() of DriverManager class is used. String url = “jdbc:odbc:DSNName”; Connection conn = DriverManager.getConnection(url);

3. Perform database query A connection object needs to be used to send SQL statement to database. After creating the connection, a SQL statement has to be created. Statement stmt = conn.createStatement(); The SQL statement will be sent to DBMS where it is optimized and executed. ResultSet rs = stmt.executeQery(“Select * From Emp”); int rows =stmt.executeUpdate(“Update Emp Set Ename=“Pk” Where Ecode=‘101’”);

The current row’s content are accessible via getXXX() methods that allow extraction of values from current row in ResultSet object. Accessing columns of current row is possible in 2 ways- By index number By name

By index String getString(int columnIndex); Boolean getBoolean(int columnIndex); byte getByte(int columnIndex); short getShort(int columnIndex); int getInt(int columnIndex); long getLong(int columnIndex); Float getFloat(int columnIndex); Double getDouble(int columnIndex);

By Name String getString(String columnName); Boolean getBoolean(String columnName); byte getByte(String columnName); short getShort(String columnName); int getInt(String columnName); long getLong(String columnName); Float getFloat(String columnName); Double getDouble(String columnName);

Example