Presentation is loading. Please wait.

Presentation is loading. Please wait.

Database Programming in Java Corresponds with Chapter 32, 33.

Similar presentations


Presentation on theme: "Database Programming in Java Corresponds with Chapter 32, 33."— Presentation transcript:

1 Database Programming in Java Corresponds with Chapter 32, 33

2

3 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).

4 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)

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

6 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

7 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)

8 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)

9 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.

10 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.

11 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.

12 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.

13 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.

14

15 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

16 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.

17 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.

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

19 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.

20 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.

21 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 http://developers.sun.com/product/jdbc/drivers for full listing of available drivers from different vendors http://developers.sun.com/product/jdbc/drivers

22 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.

23 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

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

25 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

26 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

27 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

28 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

29 Connecting to data source

30 Load driver

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

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

33 Connecting to data source Connect to Data source

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

35 Performing SQL query

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

37 Performing SQL query Get resultset from query

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

39 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.

40 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.

41 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.

42 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

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


Download ppt "Database Programming in Java Corresponds with Chapter 32, 33."

Similar presentations


Ads by Google