Chapter 12 © 2001 by Addison Wesley Longman, Inc. 1 Chapter 12 Sebesta: Programming the World Wide Web.

Slides:



Advertisements
Similar presentations
Connecting to Databases. relational databases tables and relations accessed using SQL database -specific functionality –transaction processing commit.
Advertisements

Java Database Connectivity (JDBC). 2/24 JDBC (Java DataBase Connectivity) - provides access to relational database systems JDBC is a vendor independent.
Database programming in Java An introduction to Java Database Connectivity (JDBC)
JDBC - Java Database Connectivity The objectives of this chapter are: To describe the architecture of JDBC To outline the classes in the java.sql package.
Java and Databases CS-328 Dick Steflik. Database Drivers Think of a database as just another device connected to your computer like other devices it has.
Distributed Application Development B. Ramamurthy.
COS 381 Day Agenda  Capstone projects are DUE Wednesday, May 10 at 1PM  I will be covering ASP.NET from the handouts  There will be one lecture.
1 C. Shahabi Application Programming for Relational Databases Cyrus Shahabi Computer Science Department University of Southern California
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.
Advance Computer Programming Java Database Connectivity (JDBC) – In order to connect a Java application to a database, you need to use a JDBC driver. –
1 Section 6 Embedded SQL. 6-2 CA306 Embedded SQL Section Content 6.1Embedded SQL 6.2Java Database Connectivity 6.3Web Databases.
Getting connected.  Java application calls the JDBC library.  JDBC loads a driver which talks to the database.  We can change database engines without.
JDBC Vs. Java Blend Presentation by Gopal Manchikanti Shivakumar Balasubramanyam.
Chapter 14 Database Access through the Web. © 2006 Pearson Addison-Wesley. All rights reserved Relational Databases - A database is a collection.
Java Database Connectivity (JDBC). Introduction Database –Collection of data DBMS –Database management system –Storing and organizing data SQL –Relational.
1 Java Database Connection (JDBC) There are many industrial-strength DBMS's commercially available in the market. Oracle, DB2, and Sybase are just a few.
Think Possibility Integrating Web Applications With Databases.
Chapter 14 © 2005 by Addison Wesley Longman, Inc Relational Databases - A database is a collection of data organized to allow relatively easy access.
Chapter 13 © 2013 by Pearson Relational Databases - A database is a collection of data organized to allow relatively easy access for retrievals,
© Wang Bin 2004 JDBC ----Java Database Connectivity.
CSE470 Software Engineering Fall Database Access through Java.
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.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13 Database Access Through the Web.
Chapter 13 © 2010 by Addison Wesley Longman, Inc Relational Databases - A database is a collection of data organized to allow relatively easy access.
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,
Programming for WWW (ICE 1338) Lecture #11 Lecture #11 July 30, 2004 In-Young Ko iko.AT. icu.ac.kr Information and Communications University (ICU) iko.AT.
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.
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.
Chapter 8 Databases.
COMP201 Java Programming Topic 15: Database Connectivity JDBC Reading: Chapter 4, Volume 2.
WEB/DB1 DATABASE PROGRAMMING 3JDBC by the ASU Scholars.
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.
Java Database Connectivity. Java and the database Database is used to store data. It is also known as persistent storage as the data is stored and can.
Copyright © 2002 ProsoftTraining. All rights reserved. Building Database Client Applications Using JDBC 2.0.
Chapter 17 Accessing Databases with JDBC. JDBC JDBC provides a standard library for accessing relational databases. By using the JDBC API, you can access.
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.
JDBC CHAPTER-2. JDBC - Java Database Connectivity. JDBC from Sun Microsystems provides API or Protocol to interact with different databases. With the.
12/6/2015B.Ramamurthy1 Java Database Connectivity B.Ramamurthy.
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.
Chapter 13 © 2010 by Addison Wesley Longman, Inc Relational Databases - A database is a collection of data organized to allow relatively easy access.
JDBC CS 260 Database Systems. Overview  Introduction  JDBC driver types  Eclipse project setup  Programming with JDBC  Prepared statements  SQL.
CSI 3125, Preliminaries, page 1 JDBC. CSI 3125, Preliminaries, page 2 JDBC JDBC stands for Java Database Connectivity, which is a standard Java API (application.
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.
CP476 Internet Computing Perl CGI and MySql 1 Relational Databases –A database is a collection of data organized to allow relatively easy access for retrievals,
JDBC Chapter 1 JDBC Introduction
Ch. NoNameMarks 01AWT24 02Networking18 03JDBC20 04Swing18 05Servlet20 Advance Java Programming.
© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/3 Java Database Connectivity.
Intro to JDBC Joseph Sant Applied Computing and Engineering Sciences Sheridan ITAL.
6-1 JAVA DATABASE CONNECTOR Colorado Technical University IT420 Tim Peterson.
Chapter 13 © 2009 by Addison Wesley Longman, Inc Relational Databases - A database is a collection of data organized to allow relatively easy access.
13.1 Relational Databases - A database is a collection of data organized to allow relatively easy access for retrievals, additions, and deletions - A relational.
DEPTT. OF COMP. SC & APPLICATIONS
Note: To complete the examples in this section you need access to a database!! Most of the examples work for any database with JDBC drivers. However, connecting.
Interacting with Database
JDBC Database Management Database connectivity
JDBC.
Objectives In this lesson, you will learn about:
Interacting with Database
JDBC – ODBC DRIVERS.
Java Database Connectivity
Java API for Database Connectivity
Presentation transcript:

Chapter 12 © 2001 by Addison Wesley Longman, Inc. 1 Chapter 12 Sebesta: Programming the World Wide Web

Chapter 12 © 2001 by Addison Wesley Longman, Inc Relational Databases - A database is a collection of data organized to allow relatively easy access for retrievals, additions, and deletions - A relational database is a collection of tables of data, each of which has one special column that stores the primary keys of the table - An Example Relational Database: - A relational database for used Corvettes that are for sale - Could just put all data in a single table, whose primary key would be a simple sequence number - The table could have information about various equipment the cars could have - However, it is better to put the equipment in a different table and use a cross-reference table to relate cars to equipment - Use a separate table for state names, with only references in the main table

Chapter 12 © 2001 by Addison Wesley Longman, Inc Relational Databases (continued) - Logical model Corvettes States Corvettes_ Equipment Equipment - Implementation Vette_idBody_styleMiles YearState 1coupe hatchback convertible hatchback hatchback hardtop coupe convertible hardtop hatchback Figure 12.2 The Corvettes table

Chapter 12 © 2001 by Addison Wesley Longman, Inc Relational Databases (continued) State_idState 1Alabama 2Alaska 3Arizona 4Arkansas 5California 6Colorado 7Connecticut 8Delaware 9Florida 10Georgia Figure 12.3 The States table Equip_idEquip 1Automatic 24-speed 35-speed 46-speed 5CD 6leather Figure 12.4 The Equipment table

Chapter 12 © 2001 by Addison Wesley Longman, Inc Relational Databases (continued) Vette_idEquip Figure 12.5 The Corvettes_Equipment cross-reference table

Chapter 12 © 2001 by Addison Wesley Longman, Inc Intro to SQL - A standard language to create, query, and modify databases - Supported by all major database vendors - More similar to structured English than a programming language - We cover only five basic commands: CREATE TABLE, SELECT, INSERT, UPDATE, and DELETE - SQL reserved words are case insensitive - The CREATE TABLE command: CREATE TABLE table_name ( column_name 1 data_type constraints, … column_name n data_type constraints ) - There are many different data types ( INTEGER, FLOAT, CHAR( length ), …)

Chapter 12 © 2001 by Addison Wesley Longman, Inc Intro to SQL (continued) - There are several constraints possible e.g., NOT NULL, PRIMARY KEY CREATE TABLE States ( State_id INTEGER PRIMARY KEY NOT NULL, State CHAR(20)) - The SELECT Command - Used to specify queries - Three clauses: SELECT, FROM, and WHERE - General form: SELECT column names FROM table names WHERE condition SELECT Body_style FROM Corvettes WHERE Year > An asterisk for the column names means all columns

Chapter 12 © 2001 by Addison Wesley Longman, Inc Intro to SQL (continued) - The INSERT Command - General form: INSERT INTO table_name ( col_name 1, … col_name n ) VALUES ( value 1, …, value n ) - The correspondence between column names and values is positional INSERT INTO Corvettes(Vette_id, Body_style, Miles, Year, State) VALUES (37, 'convertible', 25.5, 1986, 17) - The UPDATE Command - To change one or more values of a row in a table - General form:

Chapter 12 © 2001 by Addison Wesley Longman, Inc Intro to SQL (continued) UPDATE table_name SET col_name 1 = value 1, … col_name n = value n WHERE col_name = value - The WHERE clause is the primary key of the row to be updated - Example: UPDATE Corvettes SET Year = 1996 WHERE Vette_id = 17 - The DELETE Command - Example: DELETE FROM Corvettes WHERE Vette_id = 27 - The WHERE clause could specify more than one row of the table

Chapter 12 © 2001 by Addison Wesley Longman, Inc Intro to SQL (continued) - Joins - If you want all cars that have CD players, you need information from two tables, Corvettes and Equipment - SELECT can build a temporary table with info from two tables, from which the desired results can be obtained - this is called a join of the two tables - A SELECT that does a join operation specifies two tables in its FROM clause and also has a compound WHERE clause - For our example, we must have three WHERE conditions 1. Vette_id s from Corvettes and Corvettes_Equipment must match 2. Equip from Corvette_Equipment must match the Equip_id from Equipment 3. The Equip from Equipment must be CD

Chapter 12 © 2001 by Addison Wesley Longman, Inc Intro to SQL (continued) SELECT Corvettes.Vette_id, Corvettes.Body_style, Corvettes.Miles, Corvettes.Year, Corvettes.State, Equipment.Equip FROM Corvettes, Corvettes_Equipment, Equipment, WHERE Corvettes.Vette_id = Corvettes_Equipment.Vette_id AND Corvettes_Equipment.Equip = Equipment.Equip_id AND Equipment.Equip = 'CD' This query produces VETTE_ID BODY_STYLE MILES YEAR STATE EQUIP. 1 coupe CD 2 hatchback CD 8 convertible CD 9 hardtop CD 10 hatchback CD

Chapter 12 © 2001 by Addison Wesley Longman, Inc Architectures for Database Access - Client-Server Database Architectures - Usual database client tasks: - Provide a way for users to submit queries - Run applications that use the results of queries - Display results of queries - Database server task: - Implement a data manipulation language, which can directly access and update the database - A two-tier system has clients that are connected directly to the server - Potential problems with a two-tier system: - Because the relative power of clients has grown considerably, we could shift processing to the client, but then maintaining data integrity is difficult

Chapter 12 © 2001 by Addison Wesley Longman, Inc Architectures for Database Access (continued) - One solution to the problems of two-tier systems is to add a component in the middle - create a three-tier system - For Web-based database access, the middle tier can run applications (client just gets results) Client Middle tier Database JDBC server - Database Access with Embedded SQL - SQL commands are embedded in programs written in a host programming language, whose compiler is extended to accept some form of SQL commands - Advantage: - One package has computational support of the programming language, as well as database access with SQL

Chapter 12 © 2001 by Addison Wesley Longman, Inc Architectures for Database Access (continued) - Disadvantage (of embedded SQL): - Portability among database systems - Microsoft Access Architecture - A tool to access any common database structure - Use either the Jet database engine, or go through the Open Database Connectivity (ODBC) standard - ODBC is an API for a set of objects and methods that are an interface to different databases - Database vendors provide ODBC drivers for their products – the drivers implement the ODBC objects and methods - An application can include SQL statements that work for any database for which a driver is available

Chapter 12 © 2001 by Addison Wesley Longman, Inc Architectures for Database Access (continued) - The Java JDBC Architecture: - Related to both embedded languages and to ODBC - JDBC is a standard protocol that can be implemented as a driver for any database system - JDBC allows SQL to be embedded in Java applications, applets, and servlets - JDBC has the advantage of portability over embedded SQL - A JDBC application will work with any database system for which there is a JDBC driver 12.4 Approaches to Using JDBC - SQL commands are issued against a database system through JDBC methods - JDBC interfaces are in the java.sql package

Chapter 12 © 2001 by Addison Wesley Longman, Inc Approaches to Using JDBC (continued) - The classes that implement the JDBC interfaces serve as the client - Configurations for using JDBC: 1. The JDBC-ODBC bridge converts JDBC database interactions to ODBC database interactions - Not meant for commercial applications 2. If the target database has a JDBC driver, a two-tier configuration can be used - The application talks to the JDBC driver, which talks to the database server - Disadvantage: Every database needs its own driver, and every driver that the client may use must be installed on the client machine 3. Use a third-tier computer to run middleware - Client runs as a client of the middleware server, which talks to the database server

Chapter 12 © 2001 by Addison Wesley Longman, Inc Approaches to Using JDBC (continued) 3. Use a third-tier computer to run middleware (continued) - Advantages: a. The middleware supports all databases, so even though vendors may implement JDBC interfaces differently (extensions), the application on the client is database independent b. The client never needs to connect directly to the database server, which allows applets to be used for JDBC database access 12.5 Java JDBC Applications - We use the PointBase system, from - Connecting the application to the driver - The getConnection method of DriverManager, which select the correct driver from those that are registered

Chapter 12 © 2001 by Addison Wesley Longman, Inc Java JDBC Applications (continued) - The general form of a reference to a database for the connection operation is: jdbc: subprotocol_name : more_info - The “subprotocol” specifies the driver - For the JDBC-ODBC bridge, it is odbc - For the PointBase, it is pointbase - The “more info” part depends on the specific database being used - If the database is local, it is just the name of the database - Otherwise, it is the URL of the database - Two ways to register a database driver: 1. The general way is to have the system property jdbc.drivers maintain a list of registered drivers - Add one with jdbc.drivers = com.pointbase.jdbc.jbdcDriver;

Chapter 12 © 2001 by Addison Wesley Longman, Inc Java JDBC Applications (continued) 2. Manual registration, using the forName method of the Class class, passing the name of the driver Class.forName( "com.pointbase.jdbc.jdbcDriver"); - The actual connection is made by creating a Connection object with the getConnection method of the DriverManager class DriverManager.getConnection( database_address, database_user_id, password ) - If the application owner owns the database, public can be used for both the user id and the password myCon = DriverManager.getConnection( "jdbc:pointbase:vette", "public", "public"); - SQL commands through JDBC - First, you need a Statement object Statement myStmt = myCon.createStatement();

Chapter 12 © 2001 by Addison Wesley Longman, Inc Java JDBC Applications (continued) - SQL commands are String objects final String sql_com = "UPDATE Corvettes " + "Year = 1991 WHERE Vette_id = 7"); - Categories of SQL commands - Action - INSERT, UPDATE, DELETE, CREATE TABLE, and DROP TABLE - Query - SELECT - The action commands are executed with the executeUpdate method of Statement myStmt.executeUpdate(sql_com); - Returns the number of affected rows - A SELECT is executed by sending it as the actual parameter to the executeQuery method of Statement - The executeQuery method returns an object of class ResultSet - Get rows from ResultSet with next iterator

Chapter 12 © 2001 by Addison Wesley Longman, Inc Java JDBC Applications (continued) ResultSet result; final String sql_com = "SELECT * FROM Corvettes WHERE Year <= 1990" result = myStmt.executeQuery(sql_com); while(result.next()) { // access and process the current element } - Information is extracted from the ResultSet object with an access method, for which there is one for each data type e.g., If an extracted row is 3, "convertible", 13.5, 2001, 1 String style; style = result.getString("Body_style"); or style = result.getString(2);

Chapter 12 © 2001 by Addison Wesley Longman, Inc Java JDBC Applications (continued) Corvettes For Sale Vette_id Body_style Miles Year State 1 coupe hatchback convertible hardtop convertible hardtop hatchback Metadata - to get table and column names from a database - Two kinds of metadata: 1. Metadata that describes the database (tables and columns) 2. Metadata that describes a ResultSet object - A Connection method, getMetaData, creates an object of class DatabaseMetaData DatabaseMetaData dbmd = myCon.getMetaData();

Chapter 12 © 2001 by Addison Wesley Longman, Inc Java JDBC Applications (continued) - The getTables method of DatabaseMetaData takes four parameters, only one of which is necessary (the string, "TABLE" ) String tbl[] = {"TABLE"}; DatabaseMetaData dbmd = myCon.getMetaData(); result = dbmd.getTables( null, null, null, tbl); System.out.println( "The tables in the database are: \n\n"); while (result.next()) { System.out.println(result.getString(3)); } - Output from this: The tables in this database are: CORVETTES CORVETTES_EQUIPMENT EQUIPMENT STATES - Metadata about query results has a different structure than general database metadata - ResultSetMetaData object

Chapter 12 © 2001 by Addison Wesley Longman, Inc Java JDBC Applications (continued) ResultSetMetaData resultMd = result.getMetaData(); - We can get the number of columns, their names, types, and sizes from the resultMd object, using its methods - getColumnCount returns the number of columns - getColumnLable(i) returns the column names // Create an object for the metadata ResultSetMetaData resultMd = result.getMetaData(); // Loop to fetch and display the column names for (int i = 1; i" <= resultMd.getColumnCount(); i++) { String columnName = resultMd.getColumnLabel(i); System.out.print(columnName + "\t"); } System.out.println("\n"); Output: Vette_id Body_style Miles Year State