* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Download JAVA DATABASE CONNECTIVITY (JDBC)
Survey
Document related concepts
Serializability wikipedia , lookup
Relational algebra wikipedia , lookup
Microsoft Access wikipedia , lookup
Microsoft SQL Server wikipedia , lookup
Oracle Database wikipedia , lookup
Entity–attribute–value model wikipedia , lookup
Extensible Storage Engine wikipedia , lookup
Functional Database Model wikipedia , lookup
Ingres (database) wikipedia , lookup
Concurrency control wikipedia , lookup
Microsoft Jet Database Engine wikipedia , lookup
Open Database Connectivity wikipedia , lookup
Clusterpoint wikipedia , lookup
ContactPoint wikipedia , lookup
Transcript
JAVA DATABASE CONNECTIVITY (JDBC) 1 Overview Introduction JDBC in Use Relational databases SQL Manipulating databases with JDBC 2 Introduction 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. 3 Introduction Today's most popular database systems are relational databases. A language called SQL is the international standard language used almost universally with relational databases to perform queries (i.e., to request information that satisfies given criteria) and to manipulate data. 4 Introduction Some popular relational database management systems (RDBMSs) are Microsoft SQL Server, Oracle, IBM DB2, Informix and MySQL. Java programs communicate with databases and manipulate their data using the JDBC™API. 5 Introduction A JDBC driver enables Java applications to connect to a database in a particular DBMS and allows programmers to manipulate that database using the JDBC API. JDBC is almost always used with a relational database. However, it can be used with any table-based data source. 6 JDBC in Use Java program JDBC connectivity driver for Oracle data processing utilities jdbc-odbc bridge driver for Sybase odbc driver 7 The JDBC-ODBC Bridge ODBC (Open Database Connectivity) is a Microsoft standard from the mid 1990’s. It is an API that allows C/C++ programs to execute SQL inside databases ODBC is supported by many products. 8 The JDBC-ODBC Bridge The JDBC-ODBC bridge allows Java code to use the C/C++ interface of ODBC it means that JDBC can access many different database products The layers of translation (Java --> C --> SQL) can slow down execution. 9 The JDBC-ODBC Bridge The JDBC-ODBC bridge comes free with the JDK: called sun.jdbc.odbc.JdbcOdbcDriver The ODBC driver for Microsoft Access comes with MS Office 10 JDBC Pseudo-Code 1) load the JDBC driver 2) Specify the name and location of the database being used 3) Connect to the database with a Connection object 11 JDBC Pseudo-Code 4) Execute a SQL query using a Statement object 5) Get the results in a ResultSet object 6) Finish by closing the ResultSet, Statement and Connection objects 12 Pseudo-code as a Diagram DriveManager creates Connection creates Statement creates SQL ResultSet data Driver make link to driver SQL data 13 RELATIONAL DATABASES 14 Relational Databases A relational database is a logical representation of data that allows the data to be accessed without consideration of its physical structure. A relational database stores data in tables. +----+-----------+------------+ | id | firstName | lastName | +----+-----------+------------+ | 1 | lama | nadem | | 2 | noor | salem | | 3 | heba | nasem | +----+-----------+------------+ 15 Relational Database The books database Authors table from books database: Column Description authorID Author's ID number in the database. In the books database, this integer column is defined as autoincremented. For each row inserted in this table, the authorID value is increased by 1 automatically to ensure that each row has a unique authorID. This column represents the table's primary key. firstName Author's first name (a string). lastName Author's last name (a string). 16 Relational Database The books database Sample data from the authors table authorID firstName lastName 1 Harvey Deitel 2 Paul Deitel 3 Tem Nieto 4 Sean Santry 17 Relational Database The books database publishers table from books database: Column Description publisherID The publisher's ID number in the database. This autoincremented integer is the table's primary key. publisherName The name of the publisher (a string). 18 Relational Database The books database Data from the publishers table publisherID publisherName 1 Prentice Hall 2 Prentice Hall PTG 19 Relational Database The books database titles table from books database: Column isbn title Description ISBN of the book (a string). The table's primary key. ISBN is an abbreviation for "International Standard Book Number" Title of the book (a string). editionNumber Edition number of the book (an integer). copyright Copyright year of the book (a string). publisherID Publisher's ID number (an integer). A foreign key that relates this table to the publishers table. imageFile Name of the file containing the book's cover image (a string). price Suggested retail price of the book (a real number). 20 Relational Database The books database Sample data from the titles table of books: isbn title 0131426443 C How to Program edition Number 4 copyright 2004 publisher ID image File 1 chtp4.jpg price 85.00 0130384747 C++ How to Program 4 2003 1 cpphtp4.jpg 85.00 0130461342 Java Web Services for Experienced Programmers 1 2003 1 jwsfep1.jpg 54.99 0131483986 Java How to Program 6 2005 1 jhtp6.jpg 85.00 013100252X The Complete C++ Training Course 4 2003 2 cppctc4.jpg 109.99 0130895601 Advanced Java 2 Platform 1 How to Program 2002 1 advjhtp1.jpg 69.95 21 Relational Database The books database authorISBN table from books Column Description authorID The author's ID number, a foreign key to the authors table. isbn The ISBN for a book, a foreign key to the titles table. 22 Relational Database The books database Sample data from the authorISBN table of books: authorID isbn authorID isbn 1 0130895725 2 0139163050 2 0130895725 3 0130829293 2 0132261197 3 0130284173 2 0130895717 3 0130284181 2 0135289106 4 0130895601 23 Relational Database The books database Table relationships in books 24 SQL 25 SQL The SQL keywords are listed below: • SELECT • FROM • WHERE • GROUP BY • ORDER BY • INNER JOIN • INSERT • UPDATE • DELETE To learn other keywords, you could refer to the SQL reference guide supplied by the vendor of the RDBMS you are using. 26 Basic SELECT Query A SQL query "selects" rows and columns from one or more tables in a database. Example : SELECT authorID, lastName FROM authors authorID lastName 1 Deitel 2 Deitel 3 Nieto 4 Santry 27 WHERE Clause SELECT title, editionNumber, copyright FROM titles WHERE copyright > 2002 title Edition Number copyright The Complete C++ Training Course 4 2003 Java How to Program 5 2003 C How to Program 4 2004 Internet and World Wide Web How to 3 Program 2004 Java How to Program 6 2005 C# How to Program 1 2003 28 WHERE Clause SELECT authorID, firstName, lastName FROM authors WHERE lastName LIKE '_i%' authorID firstName lastName 3 Tem Nieto 29 ORDER BY Clause SELECT authorID, firstName, lastName FROM authors ORDER BY lastName ASC authorID firstName lastName 1 Harvey Deitel 2 Paul Deitel 3 Tem Nieto 4 Sean Santry 30 Back to our design Database designers often split related data into separate tables to ensure that a database does not store data redundantly. For example, the books database has tables authors and titles.We use an authorISBN table to store the relationship data between authors and their corresponding titles. If we did not separate this information into individual tables, we would need to include author information with each entry in the titles table. This would result in the database storing duplicate author information for authors who wrote multiple books. 31 INNER JOIN SELECT firstName, lastName, isbn FROM authors INNER JOIN authorISBN ON authors.authorID = authorISBN.authorID ORDER BY lastName, firstName firstName lastName isbn firstName lastName isbn Harvey Deitel 0130895601 Paul Deitel 0130895717 Harvey Deitel 0130284181 Paul Deitel 0132261197 Harvey Deitel 0134569555 Paul Deitel 0130895725 Harvey Deitel 0139163050 Paul Deitel 0130829293 Harvey Deitel 0135289106 Paul Deitel 0134569555 Harvey Deitel 0130895717 Paul Deitel 0130829277 Harvey Deitel 0130284173 Tem Nieto 0130161438 Harvey Deitel 0130829293 Tem Nieto 013028419x Paul Deitel 0130852473 Sean Santry 0130895601 32 INSERT Statement INSERT INTO authors (firstName, lastName) VALUES ( 'Sue', 'Smith' ) authorID firstName lastName 1 Harvey Deitel 2 Paul Deitel 3 Tem Nieto 4 Sean Santry 5 Sue Smith 33 UPDATE Statement UPDATE authors SET lastName = 'Jones' WHERE lastName = 'Smith' AND firstName = 'Sue' authorID firstName lastName 1 Harvey Deitel 2 Paul Deitel 3 Tem Nieto 4 Sean Santry 5 Sue Jones 34 DELETE Statement DELETE FROM authors WHERE lastName = 'Jones' AND firstName = 'Sue' authorID firstName lastName 1 Harvey Deitel 2 Paul Deitel 3 Tem Nieto 4 Sean Santry 35 MANIPULATING DATABASES WITH JDBC 36 Displaying the authors table from the books database 37 38 39 Output: 40