Interacting with Database

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

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 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.
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
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:
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.
Accessing Databases with JDBC. Introduction to JDBC JDBC provides a standard library for accessing databases by using the JDBC API. JDBC standardizes.
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.
Java Database Connectivity Vijayan Sugumaran Department of DIS Oakland University.
© Wang Bin 2004 JDBC ----Java Database Connectivity.
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.
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.
CONTROLPANEL Java.sql package This package provides the APIs for accessing and processing data which is stored in the database especially relational.
Connecting to Oracle using Java November 4, 2009 David Goldschmidt, Ph.D. David Goldschmidt, Ph.D.
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  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.
Accessing Database using JDBC. JDBC Objectives Gain basic knowledge of Java JDBC Become familiar with the basics of interacting with a database using.
WEB/DB1 DATABASE PROGRAMMING 3JDBC by the ASU Scholars.
JDBC – Java Database Concentricity
Copyright  Oracle Corporation, All rights reserved. 6 Accessing a Database Using the JDBC API.
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.
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.
JDBC CHAPTER-2. JDBC - Java Database Connectivity. JDBC from Sun Microsystems provides API or Protocol to interact with different databases. With the.
Java and Databases. JDBC Architecture Java Application JDBC API Data Base Drivers AccessSQL Server DB2InformixMySQLSybase.
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.
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.
1 JDBC – Java Database Connectivity CS , Spring 2010.
Intro to JDBC Joseph Sant Applied Computing and Engineering Sciences Sheridan ITAL.
6-1 JAVA DATABASE CONNECTOR Colorado Technical University IT420 Tim Peterson.
Java and database. 3 Relational Databases A relational Database consists of a set of simple rectangular tables or relations The column headings are.
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.
Java JDBC.
Interacting with Database
JDBC 15-Apr-18.
JDBC Database Management Database connectivity
JDBC – Java Database Connectivity
Web Technologies IT230 Dr Mohamed Habib.
JDBC 21-Aug-18.
UNIT-2 Java Database Programming
JDBC(Java Database Connectivity
Introduction to Programming with Java
Chapter 16 JAVA DATABASE CONNECTIVITY
Introduction to Server-Side Web Development using JSP and Databases
JDBC 15-Nov-18.
Objectives In this lesson, you will learn about:
Mr. Harish Sharma Asst. Professor Dept. of CA & IT SGRRITS Dehradun
JDBC – ODBC DRIVERS.
Java Database Connectivity
Bolat Azamat, Kim Dongmin
JAVA DATABaSE CONNECTIVITY
JDBC Example.
CS3220 Web and Internet Programming Database Access with JDBC
Presentation transcript:

Interacting with Database

ODBC ODBC stands for Open Database Connectivity A standard or open application programming interface (API) for accessing a database. ODBC provides a C interface for database access on Windows environment.

JDBC JDBC stands for Java Database Connectivity. It is a standard Java API for connecting programs written in Java to the data in relational databases. JDBC works with Java on a variety of platforms, such as Windows, Mac OS, and the various versions of UNIX.

JDBC Driver JDBC Driver is a software component that enables java application to interact with the database. There are 4 types of JDBC drivers: JDBC-ODBC bridge driver Native-API driver (partially java driver) JDBC-Net pure Java/ Network-Protocol driver (fully java driver) Pure Java Driver /Thin driver / Database-Protocol driver(fully java driver)

JDBC-ODBC bridge driver The JDBC type 1 driver, also known as the JDBC-ODBC bridge driver. The JDBC-ODBC bridge driver uses ODBC driver to connect to the database. The JDBC-ODBC bridge driver converts JDBC method calls into the ODBC function calls.

Native API driver The JDBC type 2 driver, also known as the Native-API driver The Native API driver uses the client-side libraries of the database. The driver converts JDBC method calls into native calls of the database API. It is not written entirely in java.

JDBC-Net pure Java Driver The JDBC type 3 driver, also known as the Pure Java driver for database middleware. It is a database driver implementation which makes use of a middle tier between the calling program and the database. The middle-tier (application server) converts JDBC calls directly or indirectly into a vendor-specific database protocol. It is fully written in java.

Thin 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. That is why it is known as thin driver. It is fully written in Java language.

JDBC two tier model In a two-tier model, a Java application communicates directly with the database, via the JDBC driver.

JDBC three tier model In a three-tier model, a Java application communicates with a middle tier component that functions as an application server. The application server talks to a given database using JDBC.

Common JDBC Components The JDBC API provides the following interfaces and classes − DriverManager Class Driver Interface Connection Interface Statement Interface ResultSet Interface

Common JDBC Components DriverManager Class The DriverManager class acts as an interface between user and drivers. It keeps track of the drivers that are available and handles establishing a connection between a database and the appropriate driver. The DriverManager class maintains a list of Driver classes that have registered themselves by calling the method DriverManager.registerDriver().

Commonly used methods of DriverManager class Description public static void registerDriver(Driver driver); is used to register the given driver with DriverManager. public static void deregisterDriver(Driver driver); is used to deregister the given driver (drop the driver from the list) with DriverManager. public static Connection getConnection(String url); is used to establish the connection with the specified url. public static Connection getConnection(String url,String userName,String password); is used to establish the connection with the specified url, username and password.

Common JDBC Components (cont’d..) Driver Interface This interface handles the communications with the database server. You will very rarely interact directly with Driver objects. Instead, you use DriverManager objects, which manages objects of this type. It also abstracts the details associated with working with Driver objects.

Common JDBC Components (cont’d..) Connection Interface A Connection is the session between java application and database. The Connection interface is a factory of Statement, PreparedStatement, and DatabaseMetaData. The Connection interface provide many methods for transaction management like commit(),rollback() etc.

Commonly used methods of Connection interface Description public Statement createStatement(); creates a statement object that can be used to execute SQL queries. public void setAutoCommit(boolean status); It is used to set the commit status. By default it is true. public void commit(); It saves the changes made since the previous commit/rollback permanent. public void rollback(); Drops all changes made since the previous commit/rollback. public void close(); closes the connection and Releases a JDBC resources immediately.

Common JDBC Components (cont’d..) Statement Interface The Statement interface provides methods to execute queries with the database. It provides factory method to get the object of ResultSet.

Commonly used methods of Statement interface Description public ResultSet executeQuery(String sql); used to execute SELECT query. It returns the object of ResultSet. public int executeUpdate(String sql); used to execute specified query, it may be create, drop, insert, update, delete etc. public boolean execute(String sql); used to execute queries that may return multiple results. public int[] executeBatch(); used to execute batch of commands.

Common JDBC Components (cont’d..) ResultSet Interface The object of ResultSet maintains a cursor pointing to a particular row of data. Initially, cursor points to before the first row.

Commonly used methods of ResultSet interface Description public boolean next(); is used to move the cursor to the one row next from the current position. public boolean previous(); is used to move the cursor to the one row previous from the current position. public boolean first(); is used to move the cursor to the first row in result set object. public boolean last(); is used to move the cursor to the last row in result set object. public boolean absolute(int row); is used to move the cursor to the specified row number in the ResultSet object. public int getInt(int columnIndex); is used to return the data of specified column index of the current row as int. public int getInt(String columnName); is used to return the data of specified column name of the current row as int. public String getString(int columnIndex); is used to return the data of specified column index of the current row as String. public String getString(String columnName); is used to return the data of specified column name of the current row as String.

Connecting to Database There are 5 steps to connect any java application with the database in java using JDBC. They are as follows: Register the driver class Creating connection Creating statement Executing queries Closing connection

1.Register the driver class The Class.forName() method is used to register the driver class. This method is used to dynamically load the driver class. Syntax of forName() method public static void forName(String className)throws ClassNotFoundException Example to register with JDBC-ODBC Driver Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");  

2. Creating connection The DriverManager .getConnection() method is used to establish connection with the database. Syntax of getConnection() method public static Connection getConnection(String url)throws SQLException   public static Connection getConnection(String url,String name,String password)   throws SQLException   Example establish connection with Oracle Driver Connection con = DriverManager.getConnection ("jdbc:odbc:DemoDB","username","password");

3. Creating statement The createStatement() method of Connection interface is used to create statement. The object of statement is responsible to execute queries with the database. Syntax of createStatement() method public Statement createStatement()throws SQLException    Example to create the statement object Statement stmt=con.createStatement();  

4. Executing queries The executeQuery() method of Statement interface is used to execute queries to the database. This method returns the object of ResultSet that can be used to get all the records of a table. Syntax of executeQuery() method public ResultSet executeQuery(String sql)throws SQLException Example to execute query ResultSet rs=stmt.executeQuery("select * from emp");   while(rs.next()) {   System.out.println(rs.getInt(1)+" "+rs.getString(2));   }  

5. Closing connection By closing connection object statement and ResultSet will be closed automatically. The close() method of Connection interface is used to close the connection. Syntax of close() method public void close()throws SQLException   Example to close connection con.close();  

Example to Connect Java Application with mysql database

import java.sql.*; class MysqlCon{ public static void main(String args[]) { Try { Class.forName("com.mysql.jdbc.Driver"); Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/sun","root","root"); //here sun is database name, root is username and password Statement stmt=con.createStatement(); ResultSet rs=stmt.executeQuery("select * from emp"); while(rs.next()) System.out.println(rs.getInt(1)+" "+rs.getString(2)+" "+rs.getString(3)); con.close(); } catch(Exception e) { System.out.println(e); }