+ All Categories
Home > Documents > A CCESSING D ATABASES WITH JDBC CH 24 C S 442: A DVANCED J AVA P ROGRAMMING.

A CCESSING D ATABASES WITH JDBC CH 24 C S 442: A DVANCED J AVA P ROGRAMMING.

Date post: 11-Jan-2016
Category:
Upload: loraine-fletcher
View: 214 times
Download: 0 times
Share this document with a friend
35
ACCESSING DATABASES WITH JDBC CH24 CS442: ADVANCED JAVA PROGRAMMING
Transcript
Page 1: A CCESSING D ATABASES WITH JDBC CH 24 C S 442: A DVANCED J AVA P ROGRAMMING.

ACCESSING DATABASES WITH JDBC CH24

CS442: ADVANCED JAVA PROGRAMMING

Page 2: A CCESSING D ATABASES WITH JDBC CH 24 C S 442: A DVANCED J AVA P ROGRAMMING.

A database is an organized collection of data. A database management system (DBMS) provides mechanisms for

storing, organizing, retrieving and modifying data for many users. Database management systems allow for the access and storage of data without concern for the internal representation of data.

  A language called SQL is the international standard language used almost

universally with relational databases to perform queries and to manipulate data.

   Java programs communicate with databases and manipulate their data

using the Java Database Connectivity (JDBC™) API. A JDBC driver enables Java

applications to connect to a database in a particular DBMS and allows you to manipulate that

database using the JDBC API.

Page 3: A CCESSING D ATABASES WITH JDBC CH 24 C S 442: A DVANCED J AVA P ROGRAMMING.

Today’s most popular database systems are relational databases.

A relational database is a logical representation of data that allows the data to be accessed

A relational database stores data in tables. Tables are composed of rows, and rows are

composed of columns in which values are stored. primary key a column (or group of columns) with

a unique value that cannot be duplicated in other rows.

Good examples of primary key columns are a social security number and employee ID number.

Page 4: A CCESSING D ATABASES WITH JDBC CH 24 C S 442: A DVANCED J AVA P ROGRAMMING.

Different users of a database are often interested in different data and different relationships among the data. Most users require only subsets of the rows and columns

For example, you might select data from the Employee table to create a result that shows where each department is located, presenting the data sorted in increasing order by departmentnumber.

Some popular relational database management systems (RDBMSs) are Microsoft SQL Server, Oracle and MySQL.

Page 5: A CCESSING D ATABASES WITH JDBC CH 24 C S 442: A DVANCED J AVA P ROGRAMMING.

entity-relationship (ER) diagram is diagram shows the database tables and the relationships among them. The first compartment in each box contains the table’s name and the remaining compartments contain the table’s columns.

* There’s a one-to-many relationship between a primary key and a corresponding foreign key .

Page 6: A CCESSING D ATABASES WITH JDBC CH 24 C S 442: A DVANCED J AVA P ROGRAMMING.

TABLES WILL BE USED IN SQL QUERY-:

Page 7: A CCESSING D ATABASES WITH JDBC CH 24 C S 442: A DVANCED J AVA P ROGRAMMING.
Page 8: A CCESSING D ATABASES WITH JDBC CH 24 C S 442: A DVANCED J AVA P ROGRAMMING.

 STRUCTURED QUERY LANGUAGE(SQL)

Page 9: A CCESSING D ATABASES WITH JDBC CH 24 C S 442: A DVANCED J AVA P ROGRAMMING.

BASIC SELECT QUERY:The basic form of a SELECT query

the asterisk (*) wildcard character indicates that all columns from the tableName table should be retrieved. For example, to retrieve all the data in the Authors table, use

Example :

Page 10: A CCESSING D ATABASES WITH JDBC CH 24 C S 442: A DVANCED J AVA P ROGRAMMING.

WHERE CLAUSEThe basic form of a query with selection criteria

For example, to select the Title, EditionNumber and Copyright columns from table Titles for which the Copyright date is greater than 2010, use the query:

Page 11: A CCESSING D ATABASES WITH JDBC CH 24 C S 442: A DVANCED J AVA P ROGRAMMING.

PATTERN MATCHING: ZERO OR MORE CHARACTERSOperator LIKE is used for pattern matching with wildcard characters percent (%) and underscore(_).A pattern that contains a percent character (%) searches for strings that have zero ormore characters at the percent character’s position in the pattern. Example:

Page 12: A CCESSING D ATABASES WITH JDBC CH 24 C S 442: A DVANCED J AVA P ROGRAMMING.

PATTERN MATCHING: ANY CHARACTER

An underscore ( _ ) in the pattern string indicates a single wildcard character at that position in the pattern.

Page 13: A CCESSING D ATABASES WITH JDBC CH 24 C S 442: A DVANCED J AVA P ROGRAMMING.

ORDER BY CLAUSE

The basic form of a query with an ORDER BY clause

ASCENDING ORDER:

Page 14: A CCESSING D ATABASES WITH JDBC CH 24 C S 442: A DVANCED J AVA P ROGRAMMING.

DESCENDING ORDER:

Page 15: A CCESSING D ATABASES WITH JDBC CH 24 C S 442: A DVANCED J AVA P ROGRAMMING.

SORTING BY MULTIPLE COLUMNS

Page 16: A CCESSING D ATABASES WITH JDBC CH 24 C S 442: A DVANCED J AVA P ROGRAMMING.

COMBINING THE WHERE AND ORDER BY CLAUSES

Page 17: A CCESSING D ATABASES WITH JDBC CH 24 C S 442: A DVANCED J AVA P ROGRAMMING.

MERGING DATA FROM MULTIPLE TABLES: INNER JOIN

Page 18: A CCESSING D ATABASES WITH JDBC CH 24 C S 442: A DVANCED J AVA P ROGRAMMING.

INSERT STATEMENT

Page 19: A CCESSING D ATABASES WITH JDBC CH 24 C S 442: A DVANCED J AVA P ROGRAMMING.

UPDATE STATEMENT

the WHERE clause can be simplified as follows:

Page 20: A CCESSING D ATABASES WITH JDBC CH 24 C S 442: A DVANCED J AVA P ROGRAMMING.

DELETE STATEMENT

Page 21: A CCESSING D ATABASES WITH JDBC CH 24 C S 442: A DVANCED J AVA P ROGRAMMING.

MANIPULATING DATABASES WITH JDBC1 Connecting to and Querying a DatabaseThe example performs a simple query on the books database that retrieves the entire Authors table and displays the data. The program illustrates connecting to the database, querying the database and processing the result.

Page 22: A CCESSING D ATABASES WITH JDBC CH 24 C S 442: A DVANCED J AVA P ROGRAMMING.
Page 23: A CCESSING D ATABASES WITH JDBC CH 24 C S 442: A DVANCED J AVA P ROGRAMMING.

Output:

Page 24: A CCESSING D ATABASES WITH JDBC CH 24 C S 442: A DVANCED J AVA P ROGRAMMING.

2- Querying the books DatabaseThe example allows the user to enter any query into the program. The example displays the result of a query in a JTable, using a TableModel objectto provide the ResultSet data to the JTable.

Page 25: A CCESSING D ATABASES WITH JDBC CH 24 C S 442: A DVANCED J AVA P ROGRAMMING.
Page 26: A CCESSING D ATABASES WITH JDBC CH 24 C S 442: A DVANCED J AVA P ROGRAMMING.
Page 27: A CCESSING D ATABASES WITH JDBC CH 24 C S 442: A DVANCED J AVA P ROGRAMMING.
Page 28: A CCESSING D ATABASES WITH JDBC CH 24 C S 442: A DVANCED J AVA P ROGRAMMING.
Page 29: A CCESSING D ATABASES WITH JDBC CH 24 C S 442: A DVANCED J AVA P ROGRAMMING.

The result set type specifies whether the ResultSet’s cursor is able to scroll in both directions or forward only and whether the ResultSet is sensitive to changes made to the underlying data.

Page 30: A CCESSING D ATABASES WITH JDBC CH 24 C S 442: A DVANCED J AVA P ROGRAMMING.

The result set concurrency specifies whether the ResultSet can be updated with ResultSet’s update methods.

Page 31: A CCESSING D ATABASES WITH JDBC CH 24 C S 442: A DVANCED J AVA P ROGRAMMING.

DisplayQueryResults ClassClass DisplayQueryResults implements the application’s GUI and interacts with the ResultSetTableModel via a JTable object. This application also demonstrates the JTable sorting and filtering capabilities.

Page 32: A CCESSING D ATABASES WITH JDBC CH 24 C S 442: A DVANCED J AVA P ROGRAMMING.
Page 33: A CCESSING D ATABASES WITH JDBC CH 24 C S 442: A DVANCED J AVA P ROGRAMMING.
Page 34: A CCESSING D ATABASES WITH JDBC CH 24 C S 442: A DVANCED J AVA P ROGRAMMING.
Page 35: A CCESSING D ATABASES WITH JDBC CH 24 C S 442: A DVANCED J AVA P ROGRAMMING.

Recommended