JDBC.

Slides:



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

 2003 Prentice Hall, Inc. All rights reserved. Chapter 22 – Database: SQL, MySQL, DBI and ADO.NET Outline 22.1 Introduction 22.2 Relational Database Model.
 Pearson Education, Inc. All rights reserved Accessing Databases with JDBC.
Introduction to Structured Query Language (SQL)
30-Jun-15 SQL A Brief Introduction. SQL SQL is Structured Query Language Some people pronounce SQL as “sequel” Other people insist that only “ess-cue-ell”
Concepts of Database Management Sixth Edition
CSCI 3328 Object Oriented Programming in C# Chapter 12: Databases and LINQ 1 Xiang Lian The University of Texas – Pan American Edinburg, TX 78539
Exercise SELECT authorID, lastName FROM authors AuthorID FirstName
Java Database Connectivity (JDBC). Introduction Database –Collection of data DBMS –Database management system –Storing and organizing data SQL –Relational.
Chapter 5 Introduction to SQL. Structured Query Language = the “programming language” for relational databases SQL is a nonprocedural language = the user.
ASP.NET Programming with C# and SQL Server First Edition
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.
Database Programming in Java Corresponds with Chapter 32, 33.
 2008 Pearson Education, Inc. All rights reserved Database: SQL, MySQL, ADO.NET 2.0 and Java DB.
CHAPTER 7 Database: SQL, MySQL. Topics  Introduction  Relational Database Model  Relational Database Overview: Books.mdb Database  SQL (Structured.
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.
 2003 Prentice Hall, Inc. All rights reserved. 1 Java Database Connectivity with JDBC TM.
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.
CHAPTER 8 Database: SQL, MySQL. Topics  Introduction  Relational Database Model  Relational Database Overview: Books.mdb Database  SQL (Structured.
CSC 411/511: DBMS Design Dr. Nan WangCSC411_L12_JDBC_MySQL 1 MySQL and JDBC.
Sundara Ram Matta Apr 01 st, Sundara Ram Matta Apr 01 st, 2015
Database: SQL and MySQL
CPS120: Introduction to Computer Science Lecture 19 Introduction to SQL.
1 Databases November 15, 2005 Slides modified from Internet & World Wide Web: How to Program (3rd) edition. By Deitel, Deitel, and Goldberg. Published.
 2004 Prentice Hall, Inc. All rights reserved. 1 Segment – 6 Web Server & database.
Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting MySQL – Selecting Data.
Database, SQL, and ADO.NET- Part 1 Session 11 Mata kuliah: M0874 – Programming II Tahun: 2010.
Java How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Concepts of Database Management Seventh Edition
7 1 Chapter 7 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
JDBC. Preliminaries Database Database Collection of data Collection of data DBMS DBMS Database management system Database management system Stores and.
COMP201 Java Programming Topic 15: Database Connectivity JDBC Reading: Chapter 4, Volume 2.
Chapter 25 Databases. Chapter Scope Database concepts Tables and queries SQL statements Managing data in a database Java Foundations, 3rd Edition, Lewis/DePasquale/Chase25.
Visual C# 2012 How to Program © by Pearson Education, Inc. All Rights Reserved.
Java How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Database Fundamental & Design by A.Surasit Samaisut Copyrights : All Rights Reserved.
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.
Internet & World Wide Web How to Program, 5/e. © by Pearson Education, Inc. All Rights Reserved. 2 Revised by Dr. T. Tran for CSI3140.
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.
 2005 Pearson Education, Inc. All rights reserved Accessing Databases with JDBC.
A CCESSING D ATABASES WITH JDBC CH 24 C S 442: A DVANCED J AVA P ROGRAMMING.
 2008 Pearson Education, Inc. All rights reserved Database: SQL, MySQL, ADO.NET 2.0 and Java DB.
 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 22 - SQL, MySQL, DBI and ADO Outline 22.1 Introduction 22.2 Relational Database Model 22.3 Relational.
 2009 Pearson Education, Inc. All rights reserved Databases and LINQ to SQL.
Web Programming MySql JDBC Web Programming.
Database: SQL, MySQL, LINQ and Java DB © by Pearson Education, Inc. All Rights Reserved.
MySQL Tutorial. Databases A database is a container that groups together a series of tables within a single structure Each database can contain 1 or more.
1 Section 1 - Introduction to SQL u SQL is an abbreviation for Structured Query Language. u It is generally pronounced “Sequel” u SQL is a unified language.
COM621: Advanced Interactive Web Development Lecture 11 MySQL – Data Manipulation Language.
ORDER BY Clause The result of a query can be sorted in ascending or descending order using the optional ORDER BY clause. The simplest form of.
Database, SQL and ADO.NET
CHAPTER 7 DATABASE ACCESS THROUGH WEB
Chapter 5 Introduction to SQL.
 2012 Pearson Education, Inc. All rights reserved.
Accessing Databases with JDBC
Databases Intro (from Deitel)
Chapter 7 Working with Databases and MySQL
Chapter 8 Working with Databases and MySQL
Interacting with Database
Chapter 22 - SQL, MySQL, DBI and ADO
MSIS 655 Advanced Business Applications Programming
Java Database Connectivity
Introduction To Structured Query Language (SQL)
Lecture 11 Database Connection
Java Chapter 6 (Estifanos Tilahun Mihret--Tech with Estif)
Presentation transcript:

JDBC

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. It allows for the access and storage of data without concern for the internal representation of data. Today’s most popular database systems are relational databases, where the data is stored without consideration of its physical structure. A language called SQL—pronounced “sequel,” or as its individual letters—is the international standard language used almost universally with relational databases to perform queries and to manipulate data. Some popular relational database management systems (RDBMSs) are Microsoft SQL Server, Oracle, Sybase, IBM DB2, Informix, PostgreSQL and MySQL. Java programs communicate with databases and manipulate their data using the 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.

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. The Number column of each row in this table is the table’s primary key—a column (or group of columns) in a table with a unique value that cannot be duplicated in other rows. Each column represents a different data attribute.

The books Database

SQL Basic SELECT Query A SQL query “selects” rows and columns from one or more tables in a database. Such selections are performed by queries with the SELECT keyword. SELECT * FROM tableName in which the asterisk (*) indicates that all columns from the tableName table should be retrieved. For Example, to retrieve all the data in the authors table SELECT * FROM authors

WHERE Clause SQL uses the optional WHERE clause in a query to specify the selection criteria for the query. SELECT columnName1, columnName2, … FROM tableName WHERE criteria For example, to select the title, editionNumber and copyright columns from table titles for which the copyright date is greater than 2005, use the query SELECT title, editionNumber, copyright FROM titles WHERE copyright > '2005‘ The WHERE clause criteria can contain the operators <, >, <=, >=, =, <> and LIKE. Operator 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 or more characters at the percent character’s position in the pattern. For example, the next query locates the rows of all the authors whose last name starts with the letter D: SELECT authorID, firstName, lastName FROM authors WHERE lastName LIKE 'D%‘ The % in the WHERE clause’s LIKE pattern indicates that any number of characters can appear after the letter D in the lastName.

An underscore ( _ ) in the pattern string indicates a single wildcard character at that position in the pattern. For example, the following query locates the rows of all the authors whose last names start with any character (specified by _), followed by the letter o, followed by any number of additional characters (specified by %): SELECT authorID, firstName, lastName FROM authors WHERE lastName LIKE '_o%'

ORDER BY Clause The rows in the result of a query can be sorted into ascending or descending order by using the optional ORDER BY clause. SELECT columnName1, columnName2, … FROM tableName ORDER BY column ASC SELECT columnName1, columnName2, … FROM tableName ORDER BY column DESC where ASC specifies ascending order (lowest to highest), DESC specifies descending order (highest to lowest) and column specifies the column on which the sort is based. For example, to obtain the list of authors in ascending order by last name (Fig. 25.15), use the query SELECT authorID, firstName, lastName FROM authors ORDER BY lastName ASC To obtain the same list of authors in descending order by last name (Fig. 25.16), use the query ORDER BY lastName DESC

Multiple columns can be used for sorting with an ORDER BY clause of the form ORDER BY column1 sortingOrder, column2 sortingOrder, … where sortingOrder is either ASC or DESC. Note that the sortingOrder does not have to be identical for each column. The query SELECT authorID, firstName, lastName FROM authors ORDER BY lastName, firstName sorts all the rows in ascending order by last name, then by first name. The WHERE and ORDER BY clauses can be combined in one query, as in SELECT isbn, title, editionNumber, copyright FROM titles WHERE title LIKE '%How to Program' ORDER BY title ASC

Merging Data from Multiple Tables: INNER JOIN Often, it is necessary to merge data from multiple tables into a single result. An INNER JOIN merges rows from two tables by matching values in columns that are common to the tables. SELECT columnName1, columnName2, … FROM table1 INNER JOIN table2 ON table1.columnName = table2.columnName The ON clause of the INNER JOIN specifies the columns from each table that are compared to determine which rows are merged. For example, the following query produces a list of authors accompanied by the ISBNs for books written by each author: SELECT firstName, lastName, isbn FROM authors INNER JOIN authorISBN ON authors.authorID = authorISBN.authorID ORDER BY lastName, firstName The query merges the firstName and lastName columns from table authors with the isbn column from table authorISBN, sorting the result in ascending order by lastName and firstName.

INSERT Statement The INSERT statement inserts a row into a table. INSERT INTO tableName ( columnName1, columnName2, …, columnNameN ) VALUES ( value1, value2, …, valueN ) where tableName is the table in which to insert the row. The tableName is followed by a comma-separated list of column names in parentheses. The list of column names is followed by the SQL keyword VALUES and a comma- separated list of values in parentheses. INSERT INTO authors ( firstName, lastName ) VALUES ( 'Sue', 'Smith' )

UPDATE Statement An UPDATE statement modifies data in a table. UPDATE tableName SET columnName1 = value1, columnName2 = value2, …, columnNameN = valueN WHERE criteria The tableName is followed by keyword SET and a comma-separated list of column name/value pairs in the format columnName = value. UPDATE authors SET lastName = 'Jones' WHERE lastName = 'Smith' AND firstName = 'Sue‘

DELETE Statement A SQL DELETE statement removes rows from a table. DELETE FROM tableName WHERE criteria where tableName is the table from which to delete. The optional WHERE clause specifies the criteria used to determine which rows to delete. If this clause is omitted, all the table’s rows are deleted. DELETE FROM authors WHERE lastName = 'Jones' AND firstName = 'Sue‘

Instructions for installing MySQL and MySQL Connector/J MySQL 5.0 Community Edition is an open-source database management system that executes on many platforms, including Windows, Solaris, Linux, and Macintosh.

Instructions for Setting Up a MySQL User Account

Creating Database books in MySQL

Manipulating Databases with JDBC In this section, we present two examples. The first example introduces how to connect to a database and query the database. Connecting to and Querying a Database The 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.

Lines 3–8 import the JDBC interfaces and classes from package java.sql Line 13 declares a string constant for the database driver, and line 14 declares a string constant for the database URL. This identifies the name of the database to connect to, as well as information about the protocol used by the JDBC driver connects to the books database, queries the database, displays the result of the query and closes the database connection. The program must load the database driver before connecting to the database. Line 27 uses the static method forName of class Class to load the class for the database driver. This line throws a checked exception of type java.lang.ClassNotFoundException if the class loader cannot locate the driver class. Lines 30–31 of Fig. 25.23 create a Connection object (package java.sql) referenced by connection. An object that implements interface Connection manages the connection between the Java program and the database. Connection objects enable programs to create SQL statements that manipulate databases. The program initializes connection with the result of a call to static method getConnection of class DriverManager (package java.sql), which attempts to connect to the database specified by its URL. Method getConnection takes three arguments—a String that specifies the database URL, a String that specifies the username and a String that specifies the password.

The URL jdbc:mysql://localhost/books specifies the protocol for communication (jdbc), the subprotocol for communication (mysql) and the location of the database (//localhost/books, where localhost is the host running the MySQL server and books is the database name). If the DriverManager cannot connect to the database, method getConnection throws a SQLException. Figure 25.24 lists the JDBC driver names and database URL formats of several popular RDBMSs.

Line 34 invokes Connection method createStatement to obtain an object that implements interface Statement (package java.sql). Lines 37–38 use the Statement object’s executeQuery method to submit a query that selects all the author information from table authors. This method returns an object that implements interface ResultSet and contains the query results. Lines 41–54 process the ResultSet. Line 41 obtains the metadata for the ResultSet as a ResultSetMetaData (package java.sql) object. The metadata describes the ResultSet’s contents. Method next returns boolean value true if it is able to position to the next row; otherwise, the method returns false.