Java Database Connectivity (JDBC). Introduction Database –Collection of data DBMS –Database management system –Storing and organizing data SQL –Relational.

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

 2003 Prentice Hall, Inc. All rights reserved. Chapter 22 – Database: SQL, MySQL, DBI and ADO.NET Outline 22.1 Introduction 22.2 Relational Database Model.
Database programming in Java An introduction to Java Database Connectivity (JDBC)
Java and Databases CS-328 Dick Steflik. Database Drivers Think of a database as just another device connected to your computer like other devices it has.
 Pearson Education, Inc. All rights reserved Accessing Databases with JDBC.
Distributed Application Development B. Ramamurthy.
1 C. Shahabi Application Programming for Relational Databases Cyrus Shahabi Computer Science Department University of Southern California
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.
Web Application Deployment & JDBC CSC 667, Spring 2004 Dr. Ilmi Yoon.
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.
© Wang Bin 2004 JDBC ----Java Database Connectivity.
CSE470 Software Engineering Fall Database Access through Java.
Intro to JDBC To effectively use Java Data Base Connectivity we must understand: 1.Relational Database Management Systems (RDBMS) 2.JDBC Drivers 3.SQL.
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.
 2008 Pearson Education, Inc. All rights reserved Database: SQL, MySQL, ADO.NET 2.0 and Java DB.
CHAPTER 7 Database: SQL, MySQL. Topics  Introduction  Relational Database Model  Relational Database Overview: Books.mdb Database  SQL (Structured.
 2003 Prentice Hall, Inc. All rights reserved. 1 Java Database Connectivity with JDBC TM.
CIS 270—Application Development II Chapter 25—Accessing Databases with JDBC.
CHAPTER 8 Database: SQL, MySQL. Topics  Introduction  Relational Database Model  Relational Database Overview: Books.mdb Database  SQL (Structured.
CSC 411/511: DBMS Design Dr. Nan WangCSC411_L12_JDBC_MySQL 1 MySQL and JDBC.
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,
Database: SQL and MySQL
Connecting to Oracle using Java November 4, 2009 David Goldschmidt, Ph.D. David Goldschmidt, Ph.D.
 2004 Prentice Hall, Inc. All rights reserved. 1 Segment – 6 Web Server & database.
JDBC. Preliminaries Database Database Collection of data Collection of data DBMS DBMS Database management system Database management system Stores and.
JDBC Enterprise Systems Programming. JDBC  Java Database Connectivity  Database Access Interface provides access to a relational database (by allowing.
Chapter 8 Databases.
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.
Visual C# 2012 How to Program © by Pearson Education, Inc. All Rights Reserved.
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.
12/6/2015B.Ramamurthy1 Java Database Connectivity B.Ramamurthy.
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–
 2005 Pearson Education, Inc. All rights reserved Accessing Databases with JDBC.
DataBases and SQL INFSY 547 Spring Course Wrap Up April 12: Complete Work on Servlets Review of Team Projects Close of Portfolio Work April 19:
 2008 Pearson Education, Inc. All rights reserved Database: SQL, MySQL, ADO.NET 2.0 and Java DB.
 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 22 - SQL, MySQL, DBI and ADO Outline 22.1 Introduction 22.2 Relational Database Model 22.3 Relational.
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.
Web Programming MySql JDBC Web Programming.
Access Databases from Java Programs via JDBC Tessema M. Mengistu Department of Computer Science Southern Illinois University Carbondale
Basics of JDBC Session 14.
Database: SQL, MySQL, LINQ and Java DB © by Pearson Education, Inc. All Rights Reserved.
Advanced Java Session 5 New York University School of Continuing and Professional Studies.
Ch. NoNameMarks 01AWT24 02Networking18 03JDBC20 04Swing18 05Servlet20 Advance Java Programming.
© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/3 Java Database Connectivity.
In the Name Of Almighty Allah. Java Application Connection To Mysql Created by Hasibullah (Sahibzada) Kabul Computer Science Faculty Afghanistan.
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.
ORDER BY Clause The result of a query can be sorted in ascending or descending order using the optional ORDER BY clause. The simplest form of.
DEPTT. OF COMP. SC & APPLICATIONS
JDBC Database Management Database connectivity
Database Programming in Java
 2012 Pearson Education, Inc. All rights reserved.
Accessing Databases with JDBC
JDBC.
Mr. Harish Sharma Asst. Professor Dept. of CA & IT SGRRITS Dehradun
Chapter 22 - SQL, MySQL, DBI and ADO
JDBC – ODBC DRIVERS.
Java Database Connectivity
Presentation transcript:

Java Database Connectivity (JDBC)

Introduction Database –Collection of data DBMS –Database management system –Storing and organizing data SQL –Relational database –Structured Query Language JDBC –Java Database Connectivity –JDBC driver

JDBC Programs developed with Java/JDBC are platform and vendor independent. “write once, compile once, run anywhere” Write apps in java to access any DB, using standard SQL statements – while still following Java conventions. JDBC driver manager and JDBC drivers provide the bridge between the database and java worlds.

Java application JDBC Driver Manager JDBC/ODBC Bridge vendor- supplied JDBC driver ODBC driver Database JDBC API JDBC Driver API

ODBC JDBC heavily influenced by ODBC ODBC provides a C interface for database access on Windows environment. ODBC has a few commands with lots of complex options. Java prefers simple methods but lots of them.

Type 1: Uses a bridging technology to access a database. JDBC-ODBC bridge is an example. It provides a gateway to the ODBC. Type 2: Native API drivers. Driver contains Java code that calls native C/C++ methods provided by the database vendors. Type 3: Generic network API that is then translated into database-specific access at the server level. The JDBC driver on the client uses sockets to call a middleware application on the server that translates the client requests into an API specific to the desired driver. Extremely flexible. Type 4: Using network protocols built into the database engine talk directly to the database using Java sockets. Almost always comes only from database vendors. Type 3 Type 1 Type 2 Type 4 3 rd Party API Native C/C++ API Database Local API Network API

Relational-Database Model Relational database –Table –Record –Field, column –Primary key Unique data SQL statement –Query –Record sets

Structured Query Language (SQL) SQL overview SQL keywords

Basic SELECT Query Simplest format of a SELECT query –SELECT * FROM tableName SELECT * FROM authors Select specific fields from a table –SELECT authorID, lastName FROM authors

WHERE Clause specify the selection criteria –SELECT fieldName1, fieldName2, … FROM tableName WHERE criteria SELECT title, editionNumber, copyright FROM titles WHERE copyright > 1999 WHERE clause condition operators –, =, =, <> –LIKE wildcard characters % and _

WHERE Clause (Cont.) SELECT authorID, firstName, lastName FROM authors WHERE lastName LIKE ‘D%’

WHERE Clause (Cont.) SELECT authorID, firstName, lastName FROM authors WHERE lastName LIKE ‘_i%’

ORDER BY Clause Optional ORDER BY clause –SELECT fieldName1, fieldName2, … FROM tableName ORDER BY field ASC –SELECT fieldName1, fieldName2, … FROM tableName ORDER BY field DESC ORDER BY multiple fields –ORDER BY field1 sortingOrder, field2 sortingOrder, … Combine the WHERE and ORDER BY clauses

ORDER BY Clause (Cont.) SELECT authorID, firstName, lastName FROM authors ORDER BY lastName ASC

ORDER BY Clause (Cont.) SELECT authorID, firstName, lastName FROM authors ORDER BY lastName DESC

ORDER BY Clause (Cont.) SELECT authorID, firstName, lastName FROM authors ORDER BY lastName, firstName

ORDER BY Clause (Cont.) SELECT isbn, title, editionNumber, copyright, price FROM titles WHERE title LIKE ‘%How to Program’ ORDER BY title ASC

Merging Data from Multiple Tables: Joining Join the tables –Merge data from multiple tables into a single view –SELECT fieldName1, fieldName2, … FROM table1, table2 WHERE table1.fieldName = table2.fieldName –SELECT firstName, lastName, isbn FROM authors, authorISBN WHERE authors.authorID = authorISBN.authorID ORDER BY lastName, firstName

INSERT INTO Statement Insert a new record into a table –INSERT INTO tableName ( fieldName1, …, fieldNameN ) VALUES ( value1, …, valueN ) INSERT INTO authors ( firstName, lastName ) VALUES ( ‘Sue’, ‘Smith’ )

UPDATE Statement Modify data in a table –UPDATE tableName SET fieldName1 = value1, …, fieldNameN = valueN WHERE criteria UPDATE authors SET lastName = ‘Jones’ WHERE lastName = ‘Smith’ AND firstName = ‘Sue’

DELETE FROM Statement Remove data from a table –DELETE FROM tableName WHERE criteria DELETE FROM authors WHERE lastName = ‘Jones’ AND firstName = ‘Sue’

Manipulating Databases with JDBC Connect to a database Query the database Display the results of the query

JDBC Classes for DB Connection java.sql.Driver –Unless creating custom JDBC implementation, never have to deal with it. It gives JDBC a launching point for DB connectivity by responding to DriverManager connection requests java.sql.DriverManager –Maintains a list of Driver implementations and presents an application with one that matches a requested URL. –getConnection(url, uid, password) –getDrivers(), registerDriver() java.sql.Connection –Represents a single logical DB connection; used for sending SQL statements

Database URLs Uses syntax similar to net URLs –jdbc:odbc:CoreJava –jdbc:pointbase:CATS General syntax –jdbc:subprotocol_name:other_stuff –Where subprotocol selects the specific driver –Format for other_stuff depends on subprotocol, but in general: –jdbc:subprotocol://hostname:port/other –jdbc:odbc://whitehouse.gov:5000/CATS;PWD =Rice would connect to the CATS DB on port 5000 of whitehouse.gov, using the ODBC attribute value of PWD set to “Rice”.

JDBC – Making the Connection Register the Driver implementation of the DB. JDBC requires a Driver class to register itself with DriverManager when it is instantiated. –Explicitly call new to load the driver (needs to be hardcoded) –Or, use Class.forName(“DriverClass”) Establish a connection with the DB Connection c = DriverManager.getConnection(url, uid, password); –DM searches the registered Drivers until it finds the match. –The Driver class then establishes the connection, returns a connection object to DM, which in turn returns it back.

JDBC – Database Access Classes java.sql.Statement –Most basic class. It performs all the SQL statements –executeQuery( String ), executeUpdate( String ), execute( String ) java.sql.ResultSet –One or more rows of data returned by a query Statement st = c.createStatement(); ResultSet rs = st.executeQuery(“…”); –Methods: next(),getString(column),getInt(..), last(), getRow() –Be careful about SQL NULL and Java NULL –Always use wasNull() –Always call close() on all ResultSet, Statement, and Connection objects. Some drivers (e.g., IBM’s native DB2) will not close the rs and st objects even when you close the connection.

JDBC – DB Access Classes (Cont.) java.sql.ResultSetMetaData –Provides extra information about (data about data) the ResultSet object –getColumnCount(), getColumnName(column) ResultSetMetaData md = rs.getMetaData(); java.sql.DatabaseMetaData –Provides extra information about the database for a given connection object –What tables exist, what username is being used, is the DB read-only, what are the primary keys for a table, etc. DatabaseMetaData dmd = c.getMetaData(); –Lets developers write apps that are DB-independent

Prepared SQL For each SQL statement received, the DB builds a query plan by –parsing the SQL statement –reading the SQL to determine what to do –formulating a plan for executing the SQL Repeatedly executing SQL with same query plan is very inefficient –UPDATE account SET balance = XXX WHERE id = YYY Statement st = c.createStatement(); for (int i=0; i<accounts.length; i++ ) st.executeUpdate(“UPDATE account “ + “SET balance = “ + accounts[i].getBalance() + “WHERE id = “ + accounts[i].getId());

Prepared SQL (contd.) DBs enable you to optimize repeated calls through prepared SQL. Create a Java instance of a prepared statement that notifies the DB of the kind of SQL call it represents. DB can then create a query plan for that SQL even before it is actually executed. If the same prepared statement is executed more than once, the DB uses the same query plan without rebuilding a new one.

Prepared SQL (contd.) // create SQL statement with parameters PreparedStatement st = c.prepareStatement( “UPDATE account “ + “SET balance = ? “ + “WHERE id = ?”); for (int i=0; i<accounts.length; i++) { // bind the parameters st.setFloat(1, accounts[i].getBalance()); st.setInt(2, accounts[i].getId()); st.execute(); st.clearParameters(); }