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.

Slides:



Advertisements
Similar presentations
Copyright  Oracle Corporation, All rights reserved. 2 Java and Databases: An Overview.
Advertisements

Murach’s Java SE 6, C21© 2007, Mike Murach & Associates, Inc.Slide 1.
Database programming in Java An introduction to Java Database Connectivity (JDBC)
EE448: Server-Side Development Slide 1 EE448: Server-Side Development Lecturer: David Molloy Time: Tuesdays 3pm-5pm Notes:
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.
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.
1 CSC 440 Database Management Systems JDBC This presentation uses slides and lecture notes available from
Java Database Connectivity Vijayan Sugumaran Department of DIS Oakland University.
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 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.
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.
VICTORIA UNIVERSITY OF WELLINGTON Te Whare Wananga o te Upoko o te Ika a Maui COMP 302 Database Systems Java Data Base Connectivity Lecturer Dr Pavle Mogin.
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.
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.
1 JDBC Aum Amriteshwaryai Namah. 2 2 JDBC – Java DataBase Connectivity.
JDBC – Java DataBase Connectivity. JDBC API Overview JDBC is Java API that allows the Java programmers to access database management system from Java.
JDBC. Preliminaries Database Database Collection of data Collection of data DBMS DBMS Database management system Database management system Stores and.
Chapter 8 Databases.
WEB/DB1 DATABASE PROGRAMMING 3JDBC by the ASU Scholars.
Copyright  Oracle Corporation, All rights reserved. 6 Accessing a Database Using the JDBC API.
JDBC Establish a connection with a database or access any tabular data source Send SQL statements Process the results Two major sets of interfaces: JDBC.
Работа с базами данных – JDBC Введение в JDBC. JDBC JDBC (Java DataBase Connectivity) – технология доступа к базам данных для Java Состоит из: API (java.sql.
Copyright © 2002 ProsoftTraining. All rights reserved. Building Database Client Applications Using JDBC 2.0.
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.
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.
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.
JDBC CS 260 Database Systems. Overview  Introduction  JDBC driver types  Eclipse project setup  Programming with JDBC  Prepared statements  SQL.
SQL.. AN OVERVIEW lecture3 1. Overview of SQL 2  Query: allow questions to be asked of the data and display only the information required. It can include.
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 - 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.
1 JDBC – Java Database Connectivity CS , Spring 2010.
Intro to JDBC Joseph Sant Applied Computing and Engineering Sciences Sheridan ITAL.
CS422 Principles of Database Systems JDBC and Embedded SQL Chengyu Sun California State University, Los Angeles.
Java and database. 3 Relational Databases A relational Database consists of a set of simple rectangular tables or relations The column headings are.
JDBC Statements The JDBC Statement, CallableStatement, and PreparedStatement interfaces define the methods and properties that enables to send SQL or PL/SQL.
JDBC. Database is used to store data permanently. These days almost all Applications needs database to store its data persistently. Below are the most.
EE557: Server-Side Development
Interacting with Database
Lec - 14.
JDBC – Java Database Connectivity
HW#4 Making Simple BBS Using JDBC
Prof: Dr. Shu-Ching Chen TA: Sheng Guan
Design and Implementation of Software for the Web
Interacting with Database
JDBC – ODBC DRIVERS.
مقدمة في قواعد البيانات
Java Database Connectivity
SQL .. An overview lecture3.
Bolat Azamat, Kim Dongmin
Java API for Database Connectivity
Presentation transcript:

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 be retrieved anytime. Java and database are used almost everywhere to store persistent data and retrieve it when required.

SQL Information stored in the database is in tables and the language used to query information from the database is SQL. Using SQL we can query a table based on the requirement.

CRUD OPERATIONS CRUD stands for create, read, update, delete. Create statement in SQL looks like – Create table mytab ( mynum number, name varchar2(25)); READ statement looks like – Select * from mytab where mynum=25; UPDATE statement looks as – Update mytab set mynum=88 where mynum=25; DELETE statement like – Delete from mytab where mynum=88;

DDL – DML – DCL - TCL DDL – Data Definition Language – These queries are used to create database objects such as tables, indexes, procedures, constraints – Create, drop, alter,truncate,comment,rename DML – Data Manipulation Language – These queries are used to manipulate the data in the database tables. – Insert, update, delete, select (more available) DCL – Data Control Language – These are data control queries like grant and revoke permissions TCL– Transaction Control Language – These are transaction control queries like commit, revoke, savepoint, set transaction

CRUD and Java Java can invoke CRUD operations using JDBC JDBC is Java Database Connectivity and there are 4 types of drivers which form a bridge between java and a database The Operations communicated by java will be translated in a form understood by the database by the drivers.

JDBC Drivers Type 1 Driver - JDBC-ODBC bridge – This is an ODBC driver, which is open source Type 2 Driver – Native API driver – This is more like the OCI (Oracle Call Interface) call interface is converted to native calls of the database. Type 3 Driver – Network protocol driver – This is achieved by using a Java Driver in a middleware Type 4 Driver – Native Protocol Driver – This is a driver written purely in Java Language We Usually prefer to depend on the Type 4 driver – Eg: Oracle thin driver

Java-Oracle Example public void ConnectToAndQueryDatabase (String username, String password) { Connection con = DriverManager.getConnection( "jdbc:myDriver:myDatabase", username, password); Statement stmt = con.createStatement(); ResultSet rs = stmt.executeQuery("SELECT a, b, c FROM Table1"); while (rs.next()) { int x = rs.getInt("a"); String s = rs.getString("b"); float f = rs.getFloat("c"); } In the example We have a method call ConnectToAndQueryDatabase which takes in a username, password The Connection Interface creates a connection. A connection is created by using the DriverManager Class with the connection string, username, password. The concrete implementation is OracleConnection. The statement Interface creates a statement. The concrete implementation is Oracle Statement. ResultSet stores the result after invoking execute Query on the statement object. We can iterate over the resultset to get the results from table.

Statement and Prepared Statement A Statement Interface is used to create a statement i.e. a query to be executed against a database. In this process, two calls are made to the database system, one to get the table metadata and other to get the data itself. The statement object is also compiled every time it executes. A Prepared statement comes in handy as it is compiled and cached and only makes on database call when invoked. The compilation would be skipped even if the values in the where clause change

Statement and Prepared Statement Statement Statement stmt= con.statment(“select * from mytab where num=22”); Prepared Statement PreparedStatement pstmt = con.preparestatement(select * from mytab where num=?”); pstmt.setInt(1,”11”); This sets the value of ? as 11 – If we run the statement in for loop every time changing the where clause, the statement is compiled every time before execution. – In the case of prepared statement, the prepared statement is cached and compiled only one time in the event of a for loop with changing where clause.

Stored Procedures Stored procedures are compiled sql programs which are stored in the database system and can be executed in the database. We use the call keyword in SQL to execute stored procedures Java can access the stored procedures in the database by using the Callable statements

Callable Statements Callable statements are used to call stored procedures in the database. CallableStatement cstmt = null; try { String SQL = "{call myprocedure(?, ?)}"; //? Can be set dynamically as in prepared statement cstmt = conn.prepareCall (SQL);... } catch (SQLException e) {... } finally {... }

Transactions Transactions imply a situation when we have one or more SQL statements to be committed only when everything goes right – Assume we have 10 databases which are always to be in sync. So we would have one sql query to be executed across all databases. So, the sql query should committed in all the databases only after successful execution in all 10 databases. This is called a transaction. The transaction is committed only after successful execution across all databases.

Closing Connections Always remember to close in the following sequence. – Resultset – Statement/Prepared Statement – Connections Do a null check always before closing the connections using close() method.