Database Programming in Java Corresponds with Chapter 32, 33.

Slides:



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

Java Database Connectivity (JDBC). 2/24 JDBC (Java DataBase Connectivity) - provides access to relational database systems JDBC is a vendor independent.
Distributed Application Development B. Ramamurthy.
1 C. Shahabi Application Programming for Relational Databases Cyrus Shahabi Computer Science Department University of Southern California
DT228/3 Web Development Databases. Database Almost all web application on the net access a database e.g. shopping sites, message boards, search engines.
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.
1 Foundations of Software Design Lecture 27: Java Database Programming Marti Hearst Fall 2002.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 37 Java Database Programming.
JDBC / ODBC JDBC is the java API that facilitate interaction of a java application with the DBMS. FIRST APPROACH:
Dale Roberts 8/24/ Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Computer Science, IUPUI
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 (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.
JDBC Java Database Connectivity. What is an RDBMS? Relational database management system. There are other kinds of DBMS. Access is a GUI on a JET RBDMS.
Jaeki Song JAVA Lecture 11 Java Database Connectivity.
© 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.
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.
Java Utility Classes CS 21b. Some Java Utility Classes Vector Hashtable StringTokenizer * import java.util.*;
PHP Programming with MySQL Slide 8-1 CHAPTER 8 Working with Databases and MySQL.
A CCESSING D ATABASES WITH JDBC CH 24 C S 442: A DVANCED J AVA P ROGRAMMING.
CIS 270—Application Development II Chapter 25—Accessing Databases with JDBC.
Chapter 7 Working with Databases and MySQL PHP Programming with MySQL 2 nd Edition.
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,
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.
Chapter 14 - Designing Data Access Classes1 Chapter 14 Designing Data Access Classes.
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.
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.
Chapter 25 Databases. Chapter Scope Database concepts Tables and queries SQL statements Managing data in a database Java Foundations, 3rd Edition, Lewis/DePasquale/Chase25.
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.
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.
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.
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:
Database Access Using JDBC BCIS 3680 Enterprise Programming.
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.
Advanced Java Session 5 New York University School of Continuing and Professional Studies.
Ch. NoNameMarks 01AWT24 02Networking18 03JDBC20 04Swing18 05Servlet20 Advance Java Programming.
1 Principles of Database Systems With Internet and Java Applications Today’s Topic Chapter 8: Applications Programming for Relational Databases Instructor’s.
© 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.
CS320 Web and Internet Programming Database Access with JDBC Chengyu Sun California State University, Los Angeles.
DEPTT. OF COMP. SC & APPLICATIONS
JDBC Database Management Database connectivity
JDBC.
Mr. Harish Sharma Asst. Professor Dept. of CA & IT SGRRITS Dehradun
Interacting with Database
JDBC – ODBC DRIVERS.
MSIS 655 Advanced Business Applications Programming
Java Database Connectivity
Lecture 11 Database Connection
Presentation transcript:

Database Programming in Java Corresponds with Chapter 32, 33

The Entity-Relationship (ER) Model The most common way of representing the world for the purpose of database design and analysis. The most common way of representing the world for the purpose of database design and analysis. Graphical depiction of things in the world (entities) and the relationships between them (relationships). Graphical depiction of things in the world (entities) and the relationships between them (relationships).

Major Data Elements in ER Models ENTITY: Person, Place, Thing, Event about which data must be kept ENTITY: Person, Place, Thing, Event about which data must be kept represented by a RECORD (row) in a TABLE represented by a RECORD (row) in a TABLE ATTRIBUTE: Description of a Particular entity ATTRIBUTE: Description of a Particular entity represented by a FIELD (column) of the RECORD represented by a FIELD (column) of the RECORD RELATIONSHIPS: Description of how Entities interact with each other RELATIONSHIPS: Description of how Entities interact with each other 1:1, 1:N (one-to-many), N:M (many-to-many) 1:1, 1:N (one-to-many), N:M (many-to-many) Represented by associations between Primary and Foreign keys (identifiers) Represented by associations between Primary and Foreign keys (identifiers)

ER Diagram of Employee Database Entities in boxes Relationships in diamonds Employees Departments JobTypes Projects 1 | N M | N 1 | N

Entities in Employee Database Employees - data about people in the company Employees - data about people in the company Departments - data about the organizational units Departments - data about the organizational units JobTypes - data about the work classifications JobTypes - data about the work classifications Projects - data about the current projects underway Projects - data about the current projects underway

Relationships in Employee Database Each department has many employees, but each employee works for only one department (1:N) Each department has many employees, but each employee works for only one department (1:N) There are many employees of a given job type, but each employee has only one job title (1:N) There are many employees of a given job type, but each employee has only one job title (1:N) An employee who is a manager has many employees under her, but each employee has only one manager to report to (1:N) An employee who is a manager has many employees under her, but each employee has only one manager to report to (1:N) Each employee can be working on several projects, and each project may have several employees working on it (M:N) Each employee can be working on several projects, and each project may have several employees working on it (M:N)

What is a relational database? A database in which: A database in which: entity types are represented by TABLES (also called RELATIONS), entity types are represented by TABLES (also called RELATIONS), specific entities are represented by ROWS in the TABLE (records) specific entities are represented by ROWS in the TABLE (records) attributes are represented by COLUMNS in the TABLE (fields) attributes are represented by COLUMNS in the TABLE (fields) relationships between entities are represented by associations between primary and foreign keys (identifier fields) relationships between entities are represented by associations between primary and foreign keys (identifier fields)

ER-Diagram of the tables in the Employee Database 8 1 The relationships are implemented via associations between primary keys (shown here in boldface) and foreign keys of tables M:N relationships require an additional table, called an intersection (or junction) table. The M:N relationship between Employees and Projects is implemented via the EmployeeProject intersection table.

1:N Relationship Between Departments and Employees The DepartmentID field of the Employees table is a foreign key. It references the DepartmentID field of the Departments table (primary key). In this way, we can see that Sam Smith, Mike Mitri, Alice Friedman, and Brendan Mitri are all in the Payroll department. (DepartmentID = 2). A department has several employees, but each employee is in only one department.

M:N relationship between Employees and Projects The EmployeeProject table is an intersection table that implements the M:N relationship. The EmployeeID field of the EmployeeProject table is a foreign key that references the EmployeeID field of the Employees table. Likewise for the ProjectID fields. Here we see that James Smith is one of the four employees who works on Accounts Payable project. James Smith also works on the Accounts Receivable project. Each employee can have several projects and each project can have several employees.

The SELECT Statement SELECT is a keyword in the SQL language SELECT is a keyword in the SQL language First word in all SQL queries (i.e. SQL statements that display database information to the user) First word in all SQL queries (i.e. SQL statements that display database information to the user) The DBMS will respond to a SELECT statement by returning a result set. The DBMS will respond to a SELECT statement by returning a result set. Java has an interface that works with result sets, called ResultSet. Each database vendor has drivers that implement this interface. Java has an interface that works with result sets, called ResultSet. Each database vendor has drivers that implement this interface.

SQL Statements for Data Modification INSERT - adds a new row to a table INSERT - adds a new row to a table UPDATE - modifies one or more an existing row(s) UPDATE - modifies one or more an existing row(s) DELETE - removes one or more existing row(s) DELETE - removes one or more existing row(s) In Java, the Statement interface’s executeUpdate() method will be used to implement these operations. In Java, the Statement interface’s executeUpdate() method will be used to implement these operations.

JDBC Java Data Base Connectivity Java Data Base Connectivity Provides a connection between a Java application or applet and a database system Provides a connection between a Java application or applet and a database system JDBC classes and interfaces available from the java.sql package JDBC classes and interfaces available from the java.sql package JDBC is implemented by a JDBC driver JDBC is implemented by a JDBC driver

Java Database Connectivity (JDBC) An set of classes and drivers that allows applications to access data in database management systems (DBMS) using Structured Query Language (SQL) as a standard for accessing data. An set of classes and drivers that allows applications to access data in database management systems (DBMS) using Structured Query Language (SQL) as a standard for accessing data. Supports interoperability: a programmer can create an JDBC application without targeting a specific data source. Users can add drivers to the application after it is compiled and shipped. Supports interoperability: a programmer can create an JDBC application without targeting a specific data source. Users can add drivers to the application after it is compiled and shipped.

Components of JDBC Application - The program you develop. Calls JDBC functions to submit SQL statements and retrieve results. Application - The program you develop. Calls JDBC functions to submit SQL statements and retrieve results. Driver Manager - Provides access to JDBC drivers. Driver Manager - Provides access to JDBC drivers. JDBC Driver - Processes JDBC function calls, submits SQL requests to a data source, and returns results to the application. JDBC Driver - Processes JDBC function calls, submits SQL requests to a data source, and returns results to the application. Data source - A database and its associated DBMS. Data source - A database and its associated DBMS.

Application Driver Manager Driver DataSource (MS Access) Driver DataSource(Oracle)

The Application Requests connection to a data source. Requests connection to a data source. Sends SQL requests to the data source. Sends SQL requests to the data source. Contains storage areas and data formats for the results of SQL requests. Contains storage areas and data formats for the results of SQL requests. Requests results. Requests results. Processes errors. Processes errors. Displays data to user Displays data to user Terminates the connection to the data source. Terminates the connection to the data source.

JDBC Driver Manager A class in the java.sql package A class in the java.sql package Loads drivers for the application. Loads drivers for the application. Processes JDBC initialization calls. Processes JDBC initialization calls. Provides entry points to JDBC functions for a JDBC driver. Provides entry points to JDBC functions for a JDBC driver.

JDBC Driver Typically a DLL Typically a DLL Implements JDBC interfaces and their methods and interacts with a data source. Implements JDBC interfaces and their methods and interacts with a data source. Each DBMS has its own JDBC driver. Each DBMS has its own JDBC driver. Establishes a connection to a data source. Establishes a connection to a data source. Submits requests to the data source. Submits requests to the data source. Translates data to or from other formats, if requested by the application. Translates data to or from other formats, if requested by the application. Returns results to the application. Returns results to the application. Formats errors into standard error codes and returns them to the application. Formats errors into standard error codes and returns them to the application. Java JDK includes a JDBC driver called the JDBC-ODBC bridge Other drivers are available from other vendors See for full listing of available drivers from different vendors

Data Sources A database and its DBMS product (and any remote operating system and network necessary to access it) client- server capabilities A database and its DBMS product (and any remote operating system and network necessary to access it) client- server capabilities NOTE: Your system’s installed ODBC drivers and recognized data sources can be seen and modified through the ODBC Administrator Dialog Box available in the Control Panel. NOTE: Your system’s installed ODBC drivers and recognized data sources can be seen and modified through the ODBC Administrator Dialog Box available in the Control Panel.

JDBC Interfaces Connection Connection DatabaseMetaData DatabaseMetaData Driver Driver ResultSet ResultSet ResultSetMetaData ResultSetMetaData Statement Statement These are all interfaces (i.e. completely abstract) They are implemented within the loaded JDBC driver A JDBC-compliant driver implements all the functionality of these interfaces Boldfaced interfaces will be used in examples and assignment

JDBC Classes Date Date DriverManager DriverManager DriverProperyInfo DriverProperyInfo Time Time TimeStamp TimeStamp Types Types DriverManager will be used in the example and assignment

Creating JDBC Applications in Java Steps involved: Steps involved: Connect to a data source Connect to a data source Perform query and update operations on tables or record sets Perform query and update operations on tables or record sets Close the connection to the data source Close the connection to the data source Note: many JDBC operations throw SQLExceptions, so your application must throw or catch these also

Connecting to a Database 3 steps: 3 steps: load the driver load the driver use the Class.forName(drivername) method use the Class.forName(drivername) method specify the URL for the data source specify the URL for the data source JDBC URL format as follows: JDBC URL format as follows: jdbc: : jdbc: : note: jdbc is always the protocol -- the subprotocol depends on type of driver (e.g. odbc) -- the subname is the name of the data source note: jdbc is always the protocol -- the subprotocol depends on type of driver (e.g. odbc) -- the subname is the name of the data source connect to the data source connect to the data source using the DriverManager.getConnection(url, username,password) method using the DriverManager.getConnection(url, username,password) method

Performing Queries Create a Statement instance Create a Statement instance Set up an SQL query in a String (typically an SQL SELECT statement) Set up an SQL query in a String (typically an SQL SELECT statement) use the Statement class’s executeQuery() method to obtain the ResultSet of rows that match the query use the Statement class’s executeQuery() method to obtain the ResultSet of rows that match the query Use the ResultSet class’s next() method to read the records and the ResultSet class’s get****() methods for obtaining and processing data Use the ResultSet class’s next() method to read the records and the ResultSet class’s get****() methods for obtaining and processing data See the JDBC example

Performing Updates Create a Statement instance Create a Statement instance Set up an SQL statement in a String (typically an SQL UPDATE, INSERT, or DELETE statement) Set up an SQL statement in a String (typically an SQL UPDATE, INSERT, or DELETE statement) use the Statement class’s executeUpdate() method to invoke the update use the Statement class’s executeUpdate() method to invoke the update See the JDBC example

Connecting to data source

Load driver

Connecting to data source Identify Data source (this is if you want to use ODBC Administrator for DSN)

Connecting to data source Identify Data source (Alternatively, for Access database you can specify the location of the.mdb file)

Connecting to data source Connect to Data source

Note: in this example, I am asking the user to enter an sql statement, then passing it to some methods I declared.

Performing SQL query

To perform a query or update, you need to first obtain a Statement object from the database connection.

Performing SQL query Get resultset from query

Performing SQL query If you want metadata (e.g. field names, field count, etc.), get the metadata object for the resultset

Performing SQL query Columncount and columnname are information about the metadata. getColumnCount gives the number of fields and getColumnName gives the name of a particular field.

Performing SQL query getString gives the value (in string format) of the data from a particular field of the result set. The next() method of the ResultSet interface reads the next row…if it returns a null value this means that you are at the end of the resultset.

Performing SQL update/delete/insert The executeUpdate() method is used for an SQL update/delete/insert command. It returns an integer indicating how many rows were updated, added, or deleted.

Using DatabaseMetaData Many methods that can be useful for obtaining: Many methods that can be useful for obtaining: Driver version Driver version Database management system Database management system Database URL Database URL List of all table, queries, etc. List of all table, queries, etc. Many more things Many more things

This example shows how you can use DatabaseMetaData to get useful information about the database.