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.

Slides:



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

Basic JDBC Celsina Bignoli What is JDBC Industry standard for database- connectivity between the Java language and a wide range of.
Database programming in Java An introduction to Java Database Connectivity (JDBC)
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.
Distributed Application Development B. Ramamurthy.
1 JDBC Java Database Connectivity. 2 c.pdf
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 32 Java Database.
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.
JDBC Java API for Database Connectivity. Layout of this recitation Introduction to JDBC API JDBC Architecture Understanding the design of JDBC API –Classes.
JAVA JDBC JAVA JDBC Java Database Programming Lamiaa Said.
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.
Overview of JDBC and Pro*C 1 Overview of JDBC,Pro*C and Oracle connectivity on Omega CSE 5330 – Database Systems.
CS178 Database Management “JDBC”. What is JDBC ? JDBC stands for “Java DataBase Connectivity” The standard interface for communication between a Java.
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.
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.
Java Database Connectivity (JDBC) Introduction to JDBC JDBC is a simple API for connecting from Java applications to multiple databases. Lets you smoothly.
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.
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.
Web Design & Development 1 Lec Web Design & Development 2 More on JDBC.
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 Tutorial MIE456 - Information Systems Infrastructure II Vinod Muthusamy November 4, 2004.
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 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. JDBC stands for Java Data Base Connectivity. JDBC is different from ODBC in that – JDBC is written in Java (hence is platform independent, object.
MIS 3023 Business Programming II Professor: Akhilesh Bajaj Introduction to JDBC © Akhilesh Bajaj, All Rights Reserved.
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.
Copyright  Oracle Corporation, All rights reserved. 7 Accessing a Database Using SQLJ.
Chapter 17 Accessing Databases with JDBC. JDBC JDBC provides a standard library for accessing relational databases. By using the JDBC API, you can access.
JDBC - Connection The programming involved to establish a JDBC connection is fairly simple. Here are these simple four steps − Import JDBC Packages: Add.
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.
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.
Java and Databases. JDBC Architecture Java Application JDBC API Data Base Drivers AccessSQL Server DB2InformixMySQLSybase.
16 Java Database Connectivity. 2 Understand the JDBC Understand the steps of the JDBC: 1.) Importing packages 2.) Opening a connection to a database 3.)
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.
Access Databases from Java Programs via JDBC Tessema M. Mengistu Department of Computer Science Southern Illinois University Carbondale
Basics of JDBC.
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.
Umair Javed©2005 Enterprise Application Development Java Database Connectivity (JDBC) JDBC1.
Introduction to JDBC Instructor: Mohamed Eltabakh 1.
1 JDBC – Java Database Connectivity CS , Spring 2010.
Chapter 7 Chapter 7 Java Database Connectivity using JSP 1 (IS 203) WebProgramming (IS 203) Web Programming.
JDBC Statements The JDBC Statement, CallableStatement, and PreparedStatement interfaces define the methods and properties that enables to send SQL or PL/SQL.
JDBC. What is JDBC JDBC is an acronym for –Java Data Base Connectivity. It allows java program to connect to any database.
DB Apps Introduction SoftUni Team Technical Trainers
Interacting with Database
Lec - 14.
JDBC Database Management Database connectivity
JDBC – Java Database Connectivity
How to connect natively?
Objectives In this lesson, you will learn about:
Interacting with Database
Java Database Connectivity
JDBC API.
Java API for Database Connectivity
JDBC Example.
Presentation transcript:

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 database vendors how to write a driver program to interface Java programs with their database

JDBC  A Driver written according to this standard is called the JDBC Driver  All related classes and interfaces are present in the java.sql package  All JDBC Drivers implement the interfaces of java.sql

Database interaction  The steps involved in a database interaction are: – Loading the specific driver – Making a connection to the database – Sending SQL statements to the database – Processing the results

Statement  A statement object is used to send SQL statements to a database.  Three kinds : Statement Execute simple SQL without parameters PreparedStatement Used for pre-compiled SQL statements with or without parameters CallableStatement Execute a call to a database stored procedure or function

JDBC - classes and interfaces  DriverManager class - Manages all the JDBC Drivers that are loaded in the memory Helps in dynamic loading of Drivers

 Methods in DriverManager class - getConnection() : to establish a connection to a database. ○ Connection getConnection(String url, Properties info) ○ Connection getConnection(String url) ○ Connection getConnection(String url, String userID, String password ) registerDriver(java.sql.Driver)

 The different methods of Connection interface are: close() - closes the database connection createStatement() - creates an SQL Statement object prepareStatement() - creates an SQL PreparedStatement object. (PreparedStatement objects are precompiled SQL statements) prepareCall() - creates an SQL CallableStatement object using an SQL string. (CallableStatement objects are SQL stored procedure call statements )

 Statement interface - defines methods that are used to interact with database via the execution of SQL statements.  The different methods are: executeQuery(String sql) - executes an SQL statement (SELECT) that queries a database and returns a ResultSet object. executeUpdate(String sql) - executes an SQL statement (INSERT,UPDATE,or DELETE) that updates the database and returns an int, the row count associated with the SQL statement execute(String sql) - executes an SQL statement that is written as String object getResultSet() - used to retrieve the ResultSet object

 ResultSet Interface - maintains a pointer to a row within the tabular results. The next() method is used to successively step through the rows of the tabular results.  The different methods are: getBoolean(int) - Get the value of a column in the current row as a Java boolean. getByte(int) - Get the value of a column in the current row as a Java byte. getDouble(int) - Get the value of a column in the current row as a Java double. getInt(int) - Get the value of a column in the current row as a Java int.

import java.sql.*; class JDBCTest{ public static void main(String args[]) { try{ Class.forName ("oracle.jdbc.driver.OracleDriver"); Connection connection = IPaddress:port_no:host string",“uid",“password"); Statement statement = connection.createStatement(); ResultSet resultSet = statement.executeQuery ("select * from Student"); while (resultSet.next()) { System.out.println ( resultSet.getInt ("ClassNo")); } catch (Exception exception) { System.out.println (exception) }

 PreparedStatement interface -- helps us to work with precompiled SQL statements  Precompiled SQL statements are faster than normal statements  So, if a SQL statement is to be repeated, it is better to use PreparedStatement  Some values of the statement can be represented by a ? character which can be replaced later using setXXX method

import java.sql.*; class PreparedStatementTest{ public static void main(String args[]) throws Exception{ try{ Class.forName("oracle.jdbc.driver.OracleDriver"); Connection connection = DriverManager.getConnection(“url“, “UID", “password "); PreparedStatement preparedStatement = connection.prepareStatement("select * from Emp where ename=?"); preparedStatement.setString(1, 7521); ResultSet resultSet = preparedStatement.executeQuery(); while(resultSet.next()){ System.out.println(resultSet.getString("ename")); } catch(Exception exception){ System.out.println(exception); }

 CallableStatement interface -- helps us to call stored procedures and functions CallableStatement callableStatement = connection.prepareCall(“execute proc ?”); callableStatement.setInt(50); callableStatement.execute();

 The out parameters are to be registered callableStatement.registerOutParameter(int parameterIndex, int SQLType);  To get the value stored in the out parameter-- callableStatement.getXXX(int parameterIndex);

Example - Calling a stored procedure named GetSalary. The procedure queries on the Employee table and returns the salary of an employee. It has one input parameter that takes the EmpCode and an out parameter that returns the salary CallableStatement callableStatement = connection.("begin GetSalary(?,?); end;"); callableStatement.(1,29418); // OUT parameters must be registered. callableStatement.(2,Types.DOUBLE); callableStatement.execute(); System.out.println("Salary : “ +callableStatement.getDouble(2));

 ResultSetMetaData Interface - holds information on the types and properties of the columns in a ResultSet. Provides information about the database as a whole. Constructed from the Connection object  The different methods are: getColumnName() getColumnType() getColumnLabel(count)