COMP201 Java Programming Topic 15: Database Connectivity JDBC Reading: Chapter 4, Volume 2.

Slides:



Advertisements
Similar presentations
Murach’s Java SE 6, C21© 2007, Mike Murach & Associates, Inc.Slide 1.
Advertisements

Java Database Connectivity (JDBC). 2/24 JDBC (Java DataBase Connectivity) - provides access to relational database systems JDBC is a vendor independent.
1 Lecture 05: Database Programming (JDBC). 2 Outline JDBC overview JDBC API Reading: Chapter 10.5 Pointbase Developer Manual.
JDBC Overview Autumn 2001 Lecturer: C. DeJong. Relational Databases widespread use used via SQL (Structured Query Language) freely available powerful.
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.
CSE470 Software Engineering Fall Database Access through Java.
JDBC. In This Class We Will Cover: What SQL is What ODBC is What JDBC is JDBC basics Introduction to advanced JDBC topics.
Advanced OOP MCS-3 OOP BSCS-3 Lecture # 11. DATABASE CONNECTIVITY IN JAVA JDBC Programming JDBC (Java Database Connectivity) is the specification of 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).
CIS 270—App Dev II Big Java Chapter 22 Relational Databases.
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.
Java Database Connectivity (JDBC). Introduction Database –Collection of data DBMS –Database management system –Storing and organizing data SQL –Relational.
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.
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.
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.
Java Database Connectivity ASE. Java Database Connectivity (JDBC) l JDBC – provides an interface to Relational Data Sources l JDBC library provides the.
Database Programming in Java Corresponds with Chapter 32, 33.
Java Utility Classes CS 21b. Some Java Utility Classes Vector Hashtable StringTokenizer * import java.util.*;
CIS 270—Application Development II Chapter 25—Accessing Databases with JDBC.
Georgia Institute of Technology Making Text for the Web part 5 Barb Ericson Georgia Institute of Technology March 2006.
JDBC (Java Database Connectivity) SNU OOPSLA Lab. October 2005.
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.
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.
WEB/DB1 DATABASE PROGRAMMING 3JDBC by the ASU Scholars.
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.
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.
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.
Presentation On How To Create Connection To A Database.
12/6/2015B.Ramamurthy1 Java Database Connectivity B.Ramamurthy.
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.
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)
Database Programming With Java & JDBC Reading: DD Ch. 18, pp al/jdbc/index.html, or anything covering JDBC.
Introduction to JDBC Instructor: Mohamed Eltabakh 1.
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.
JSP and DB.
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.
JDBC Database Management Database connectivity
JDBC.
Objectives In this lesson, you will learn about:
Interacting with Database
JDBC – ODBC DRIVERS.
MSIS 655 Advanced Business Applications Programming
Java Database Connectivity
Java API for Database Connectivity
Presentation transcript:

COMP201 Java Programming Topic 15: Database Connectivity JDBC Reading: Chapter 4, Volume 2

COMP201 Topic 15 / Slide 2 Objective & Outline l Objective: n Introduction: How to connect to and communicate with databases l Outline: n Database basics n Connecting to a database with JDBC n Querying and manipulating databases with JDBC

COMP201 Topic 15 / Slide 3 Database Basics l A (relational) database: a collection of tables n Attributes: names of columns n Records or entries: rows l Example: testdb.mdb n 4 tables

COMP201 Topic 15 / Slide 6 Database Basics l Database management systems (DBMS): allow user to manipulate data Many DBMS available. We will use Microsoft Access, which comes with Microsoft Office.

COMP201 Topic 15 / Slide 7 Database Basics l Task: n Find all books by “Fox, David”, list author name, title, publisher name and price l The query involves all four tables and

COMP201 Topic 15 / Slide 8 Database Basics l The query: Select2 in testmd.db

COMP201 Topic 15 / Slide 9 Database Basics l Task: Find all books published by McGraw-Hill n Select3 in testdb

COMP201 Topic 15 / Slide 10 Database Basics l Update: n Modify records n Insert new records n Delete records n Create new Table n ….

COMP201 Topic 15 / Slide 11 Database Basics l So far, interact with database manually n Usually interact with databases via application programs l The structured query language (SQL): n Standard language for accessing databases:. Query database using SELECT statements n Select all elements in the Books table: SELECT * FROM Books n Select only some columns SELECT ISBN, Price, Title FROM Books

COMP201 Topic 15 / Slide 12 Database Basics l Select from multiple tables n Find all books by “Fox, David”, list author name, title, publisher name and price SELECT Authors.Name, Books.Title, Publishers.Name, Books.Price FROM Books, Authors, Publishers, BooksAuthors WHERE Publishers.Publisher_Id = Books.Publisher_Id AND Books.ISBN = BooksAuthors.ISBN AND BooksAuthors.Author_Id = Authors.Author_Id AND Authors.Name= 'Fox, David' n When we select from multiple tables, a joint table is formed. The joint table consists of ALL possible combinations of rows from all the tables. n The first three conditions in the WHERE clause constrain the query to legal combinations.

COMP201 Topic 15 / Slide 13 Database Basics l Update: n Slash prices for McGraw-Hill books by 0.5 UPDATE Books SET Price = Price -0.5 WHERE Books.Publisher_Id = ‘ ’ n A more intuitive way UPDATE Books SET Price = Price -0.5 WHERE Books.Publisher_Id = ( SELECT Publisher_Id FROM Publishers WHERE Name = 'McGraw-Hill ‘ )

COMP201 Topic 15 / Slide 14 Objective & Outline l Outline: n Database basics n Connecting to a database with JDBC n Querying and manipulating databases with JDBC

COMP201 Topic 15 / Slide 15 l JDBC-to-database communication path Connecting to Databases with JDBC Will discuss this path using testdb.mdb Textbook discusses this path using testdb.mdb Java Application JDBC Driver Manager JDBC/ODBC bridge Vendor supplied JDBC drivers ODBC Database

COMP201 Topic 15 / Slide 16 Connecting to Databases with JDBC l JDBC -- Java Database Connectivity kit : Programs written according to the JDBC API would talk to the JDBC driver manager, which in turn, would use the drivers that were plugged into it at that moment to talk to the actual database. JDBC consists of two layers. 1.JDBC API, it communicates with the JDBC manager driver API, sending it the various SQL statements. 2.The manager should communicate with the various third-party drivers that actually connect to the database and return the information from the query or perform the action specified by the query. Package: java.sql DriverManager : class for making connection to databases Connection : interface for communicating with databases once connection has been established. Other interfaces: Statement, ResultSet, DatabaseMetaData, ResultSetMetaData, SQLException, ….

COMP201 Topic 15 / Slide 17 l To establish connection, DBMS must provide JDBC drivers PointBase Mobile Edition : DBMS in java by PointBase Inc. –JDBC driver: com.pointbase.jdbc.jdbcDriver Microsoft Access & many other DBMS –Has a C interface called ODBC (Open DataBase Connectivity) –The JDBC/ODBC bridge sun.jdbc.odbc.JdbcOdbcDriver provided by Sun See java.sun.com/products/jdbc/driverdesc.html for DBMS with JDBC drivers Connecting to Databases with JDBC

COMP201 Topic 15 / Slide 18 Next: Register testdb.mdb with the ODBC Data Source Administrator on your PC. Register JDBC driver ( sun.jdbc.odbc.JdbcOdbc Driver ) with DriverManager n Making connection Connecting to Databases with JDBC Java Application JDBC Driver Manager JDBC/ODBC bridge Vendor supplied JDBC drivers ODBC Database

COMP201 Topic 15 / Slide 19 Register testdb.mdb with the ODBC Datasource Administer on your PC. Instructions: Open Control Panel and select ODBC Data Sources [32bit] (under Administrative Tools ). You will get the window shown on the right. Click on Add. Connecting to Databases with JDBC

COMP201 Topic 15 / Slide 20 Select Microsoft Access Driver and then click on Finish. You will get the window shown on the right. Provide a name for the data source, say TestDB. Click and Select and select testdb.mdb from file system (picture on the next page) Connecting to Databases with JDBC

COMP201 Topic 15 / Slide 21 n Then you see the picture on the right. Simply click on OK and the registration is complete. Thereafter the database testdb.mdb is known as TestDB to the ODBC Datasource Adminstrator. For JDBC, URL for the database is: jdbc:odbc:TestDB Connecting to Databases with JDBC

COMP201 Topic 15 / Slide 22 Register JDBC driver ( sun.jdbc.odbc.JdbcOdbcDriver ) with DriverManager n Method 1: –set system property( ~/.hotjava/properties ) System.setProperty("jdbc.drivers", "sun.jdbc.odbc.JdbcOdbcDriver"); –At initialization, DriverManager loads all driver classes referenced in jdbc.drivers n Method 2: Load driver explicitly at any time Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); A call to forName("X") causes the class named X to be initialized. Connecting to Databases with JDBC Java Application JDBC Driver Manager JDBC/ODBC bridge Vendor supplied JDBC drivers ODBC Database

COMP201 Topic 15 / Slide 23 l Making connection Connection con= DriverManager.getConnection("jdbc:odbc:TestDB", "", ""); java.sql.Connection : represents a connection to a DB. l Several drivers might have been registered with the DriverManager. The DriverManager will find one that can use the protocol on the database URL. Connecting to Databases with JDBC URL for database DB password DB user name

COMP201 Topic 15 / Slide 24 l Connection examples ConnectMe1.java : register JDBC driver using the first method ConnectMe.java : register JDBC driver using the second method Note. Examples (including testdb.mdb ) in this topic are packaged in the zip. See the code page Connecting to Databases with JDBC

COMP201 Topic 15 / Slide 25 Objective & Outline l Outline: n Database basics n Connecting to a database with JDBC n Querying and manipulating databases with JDBC

COMP201 Topic 15 / Slide 26 Querying and Manipulating Databases with JDBC getConnection returns an object of java.sql.Connection Connection con = DriverManager.getConnection("jdbc:odbc:TestDB","", ""); To execute SQL queries and commands, we first need to get a java.sql.Statement (interface in java.sql) object Statement stmt = con.createStatement(); The object used for executing a static SQL statement and returning the results it produces. Next: n Queries n Commands (updates)

COMP201 Topic 15 / Slide 27 l Form query as a string String query = "SELECT ISBN, Title, Price " + "FROM Books"; Call the executeQuery method of the Statement interface ResultSet rs = stmt.executeQuery( query ); Which returns an object of the java.sql.ResultSet interface. n A table of data representing a database result set, which is usually generated by executing a statement that queries the database. l Analysis of result: while (rs.next()) { for (int i = 1; i <= 3; i++) System.out.print( rs.getString(i) + " | "); System.out.println(""); } Querying and Manipulating Databases with JDBC Select1.java Database column number starts at 1

COMP201 Topic 15 / Slide 28 l More complex queries Select2.java: Find all books by David Fox and print out author name, title, publisher name, and price. String query = "SELECT Authors.Name, Books.Title, Publishers.Name, Books.Price " + "FROM Books, Authors, Publishers, BooksAuthors " + "WHERE Publishers.Publisher_Id = Books.Publisher_Id " + "AND Books.ISBN = BooksAuthors.ISBN " + "AND BooksAuthors.Author_Id = Authors.Author_Id " + "AND Authors.Name= 'Fox, David' "; Querying and Manipulating Databases with JDBC

COMP201 Topic 15 / Slide 29 l More complex queries Select3.java: Find all books published by McGraw-Hill and print out author name, title, publisher name, and price String query = "SELECT Authors.Name, Books.Title, Publishers.Name, Books.Price " + "FROM Books, Authors, Publishers, BooksAuthors " + "WHERE Publishers.Publisher_Id = Books.Publisher_Id " + "AND Books.ISBN = BooksAuthors.ISBN " + "AND BooksAuthors.Author_Id = Authors.Author_Id " + "AND Publishers.Name= 'McGraw-Hill' "; Querying and Manipulating Databases with JDBC

COMP201 Topic 15 / Slide 30 l Commands: modify records, insert new records, delete records, …. l Form command as a string String command = "UPDATE Books " + "SET Price = Price - 0.5" + " WHERE Books.Publisher_Id = 'McGraw-Hill'"; Call the executeUpdate method of the Statement interface stmt.executeUpdate( command ); Querying and Manipulating Databases with JDBC Update.java