Chapter 111 Databases with JSP JavaServer Pages By Xue Bai.

Slides:



Advertisements
Similar presentations
PHP II Interacting with Database Data. The whole idea of a database-driven website is to enable the content of the site to reside in a database, and to.
Advertisements

Estimating Volatilities and Correlations
Murach’s Java SE 6, C21© 2007, Mike Murach & Associates, Inc.Slide 1.
Introduction to Structured Query Language (SQL)
1 C. Shahabi Application Programming for Relational Databases Cyrus Shahabi Computer Science Department University of Southern California
ASP.NET Database Connectivity I. 2 © UW Business School, University of Washington 2004 Outline Database Concepts SQL ASP.NET Database Connectivity.
CSE470 Software Engineering Fall Database Access through Java.
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”
Introduction to Structured Query Language (SQL)
Concepts of Database Management Sixth Edition
Advance Computer Programming Java Database Connectivity (JDBC) – In order to connect a Java application to a database, you need to use a JDBC driver. –
Getting connected.  Java application calls the JDBC library.  JDBC loads a driver which talks to the database.  We can change database engines without.
A Guide to SQL, Eighth Edition Chapter Three Creating Tables.
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.
CPS120: Introduction to Computer Science Information Systems: Database Management Nell Dale John Lewis.
ASP.NET Programming with C# and SQL Server First Edition
Advanced Database Management System Lab no. 11. SQL Commands (for MySQL) –Update –Replace –Delete.
© Wang Bin 2004 JDBC ----Java Database Connectivity.
CSE470 Software Engineering Fall Database Access through Java.
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.
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.
 SQL stands for Structured Query Language.  SQL lets you access and manipulate databases.  SQL is an ANSI (American National Standards Institute) standard.
CIS 270—Application Development II Chapter 25—Accessing Databases with JDBC.
Introduction to SQL Steve Perry
2440: 141 Web Site Administration Database Management Using SQL Professor: Enoch E. Damson.
Web Server Administration Chapter 7 Installing and Testing a Programming Environment.
Chapter 7 Working with Databases and MySQL PHP Programming with MySQL 2 nd Edition.
Chapter 7 Working with Databases and MySQL PHP Programming with MySQL 2 nd Edition.
CPS120: Introduction to Computer Science Lecture 19 Introduction to SQL.
SQL SQL Server : Overview SQL : Overview Types of SQL Database : Creation Tables : Creation & Manipulation Data : Creation & Manipulation Data : Retrieving.
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.
Using Special Operators (LIKE and IN)
Concepts of Database Management Seventh Edition
1 Structured Query Language (SQL). 2 Contents SQL – I SQL – II SQL – III SQL – IV.
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.
COMP201 Java Programming Topic 15: Database Connectivity JDBC Reading: Chapter 4, Volume 2.
WEB/DB1 DATABASE PROGRAMMING 3JDBC by the ASU Scholars.
Chapter 25 Databases. Chapter Scope Database concepts Tables and queries SQL statements Managing data in a database Java Foundations, 3rd Edition, Lewis/DePasquale/Chase25.
6 1 Lecture 8: Introduction to Structured Query Language (SQL) J. S. Chou, P.E., Ph.D.
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.
Topic 1: Introduction to SQL. SQL stands for Structured Query Language. SQL is a standard computer language for accessing and manipulating databases SQL.
Concepts of Database Management Eighth Edition Chapter 3 The Relational Model 2: SQL.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
Database Fundamental & Design by A.Surasit Samaisut Copyrights : All Rights Reserved.
SQL Jan 20,2014. DBMS Stores data as records, tables etc. Accepts data and stores that data for later use Uses query languages for searching, sorting,
JDBC CS 124. JDBC Java Database Connectivity Database Access Interface provides access to a relational database (by allowing SQL statements to be sent.
Li Tak Sing COMPS311F. Database programming JDBC (Java Database Connectivity) Java version of ODBC (Open Database Connectivity) ODBC provides a standard.
DATABASE CONNECTIVITY TO MYSQL. Introduction =>A real life application needs to manipulate data stored in a Database. =>A database is a collection of.
A CCESSING D ATABASES WITH JDBC CH 24 C S 442: A DVANCED J AVA P ROGRAMMING.
Database Access Using JDBC BCIS 3680 Enterprise Programming.
JDBC CS 260 Database Systems. Overview  Introduction  JDBC driver types  Eclipse project setup  Programming with JDBC  Prepared statements  SQL.
Web Programming MySql JDBC Web Programming.
Basics of JDBC Session 14.
ECMM6018 Enterprise Networking For Electronic Commerce Tutorial 6 CGI/Perl and databases.
Distribution of Marks For Second Semester Internal Sessional Evaluation External Evaluation Assignment /Project QuizzesClass Attendance Mid-Term Test Total.
1 A Very Brief Introduction to Relational Databases.
7 1 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel 7.6 Advanced Select Queries SQL provides useful functions that.
LM 5 Introduction to SQL MISM 4135 Instructor: Dr. Lei Li.
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.
Web Systems & Technologies
Chapter 5 Introduction to SQL.
JDBC.
Chapter 8 Working with Databases and MySQL
The University of Akron College of Applied Science & Technology Dept
Presentation transcript:

Chapter 111 Databases with JSP JavaServer Pages By Xue Bai

Chapter 112 Objectives In this chapter, you will: Learn basic database and DBMS concepts Use SQL to manipulate databases via your JSP Web applications Use the JDBC or JDBC-ODBC bridge to connect to databases from JSP pages Create tables and insert data into tables via JSP pages Get data from tables via JSP pages

Chapter 113 Database Concepts A database is a collection of data items related to some enterprise –For example, a database might contain checking account information in a bank, product items information in an on-line store, students records in a college, and so on Relational databases store information in simple structures called tables Most commercial databases today are relational databases

Chapter 114 Database Concepts The data in a table is organized into rows and columns The rows in the table are called records A record contains information about a given entity An entity is a distinct object, for example, a person's name, a product, or an event, to be presented in the table The columns in the table contain fields A field contains a single, specific piece of information within a record. So, a record is a group of related fields

Chapter 115 Table 11-1: Sample Database Table Item NumberItem NameUnit PriceInventory 100Camera$ Washer$ TV$

Chapter 116 Creating Tables Table name Fields and their data types –The table name identifies the table –A database can consist of many tables –You use the name of a table to reference the table and to manipulate data in that table

Chapter 117 Using Common Fields to Link Two Tables productIDproductNamemodelPricemanufacturerI D 100WasherD TVS manufacturerIDnameaddressphone 1Weiwei Co.Edward Rd XYZ Co.Central654321

Chapter 118 Primary Key Unique identifier Used to uniquely identify a record in a table

Chapter 119 An Introduction to SQL Computer language to process relational database Data definition language (DDL): –Create table, alter table, and so on –Used to define a tables column, add or delete columns, and delete unneeded tables Data manipulation language(DML): –Insert into, update, delete, and select –Used to insert, update, delete, and retrieve data in a table

Chapter 1110 Data Types Data typeSample dataDescription CHAR(length)Newcastle Dr.For nonnumeric data. Fixed length VARCHAR(length)Newcastle Dr.For nonnumeric data. Variable length (listed length indicates maximum) INTEGER123456For whole number data between –2 31 and SMALLINT31For whole number data between –2 15 and FLOAT2.6E+10Very large or vary small numbers DATE11/16/2001For date. Implementations vary in different databases

Chapter 1111 Creating and Dropping Tables CREATE TABLE tableName (field1 dataType, field2 dataType, …) CREATE TABLE tableName (field1 dataType PRIMARY KEY, field2 dataType, …) DROP TABLE tableName

Chapter 1112 Example: Student Table CREATE TABLE student ( id integer PRIMARY KEY, firstName varchar(15), lastName varchar(15));

Chapter 1113 Inserting Data Into a Table INSERT INTO TABLE tableName VALUES (value1, value2, …) or INSERT INTO TABLE tableName (field1, field2, …) VALUES ( value1, value2, …)

Chapter 1114 Updating Table Data UPDATE tableName SET field1=value1, fiedl2=value2, … WHERE conditions The WHERE clause gives the condition for selecting which rows (records) are to be updated in the table identified as tableName The SET keyword is followed by the field list to be updated If the WHERE clause is omitted, all rows in the table are updated

Chapter 1115 Updating Table Data Conditions in a WHERE clause are similar to conditional statements in JSP Conditions can be constructed with comparison operators and logical operators You can use the six comparison operators ( =, <> for not equal,, =) as well as the three logical operators (AND, OR, and NOT) to create compound conditions or to negate a condition

Chapter 1116 Deleting Records from a Table DELETE FROM tableName WHERE conditions –This deletes all rows that satisfy the WHERE clause in the statement –If there is no WHERE clause, then all rows in the table are deleted

Chapter 1117 Retrieving Data SELECT field1, field2, … FROM tableName WHERE conditions –The SELECT clause lists the fields retrieved in the query result, separated by commas –The FROM clause lists one or more table names to be used by the query –All fields listed in the SELECT or WHERE clauses must be found in one and only one of the tables listed in the FROM clause

Chapter 1118 Retrieving Data –The WHERE clause contains conditions for selecting rows from the tables listed in the FROM clause –The data retrieved are from the rows that satisfy the condition (and are therefore selected) –If the WHERE clause is omitted, all rows are selected

Chapter 1119 Wildcard Characters Special symbols that represent any character or combination of characters Make it easier to use inexact spelling in a query The percent symbol % represents any collection of characters, including zero characters The underscore _ represents any individual character

Chapter 1120 Sorting Retrieved Data SELECT field1, field2, … FROM tableName WHERE conditions ORDER BY sort_field1 DESC, sort_field2, … Sort data retrieved in a particular order

Chapter 1121 Database Access from JSP Via JDBC connection Via JDBC to ODBC bridge JDBC is a technology developed by Sun to allow to access virtually any database system from JSP pages ODBC is a technology developed by Microsoft to allow generic access to database systems

Chapter 1122 Steps to Access Database in JSP 1.Load the JDBC driver 2.Define the connection URL 3.Establish the connection 4.Create the statement object 5.Execute a query or update 6.Process the results 7.Close the connection

Chapter 1123 Loading the JDBC Driver The driver acts as the bridge between the JDBC classes (the classes used in JSP and JavaBeans to access database) and the database itself The driver is a piece of software that knows how to talk to the DBMS To load a driver, all you need to do is to load the appropriate class Class.forName(fully qualified class name);

Chapter 1124 Loading the JDBC Driver Example Class.forName(sun.jdbc.odbc.JdbcOdbc Driver); Class.forName(oracle.jdbc.driver.OracleD river); Class.forName(org.mysql.Driver); Class.forName(org.gjt.mm.mysql.Driver) ;

Chapter 1125 Defining the Connection URL Location of the database server URLs referring to databases use the jdbc: protocol followed by specific subprotocal and the name of the database For some subprotocals, you may need to specify the database server host name and port number, user name and password, etc The URLs are different in different databases For jdbc:odbc bridge connection, the URL has the following form: jdbc.odbc.data_source_name

Chapter 1126 Establishing the Connection Create a connection to the database server To make the actual network connection, you need to pass the URL, the database username, and the password to the getConnection method of the DriverManager class, as follows: Connection conn = DriverManager.getConnection(connURL,username, password);

Chapter 1127 Creating the Statement Object Used to send queries and commands to the database Statement stm = conn.createStatement();

Chapter 1128 Executing a Query or Command The statement object has two methods: the executeUpdate() method for table updating, creating, modifying, and so on The executeQuery() method for retrieving data from a table The executeUpdate() method returns an integer indicating how many rows were affected The executeQuery() method returns a ResultSet object containing the selected rows from a table in the database

Chapter 1129 Example: Create a Table String query = CREATE TABLE product + (productID char(5), name + varchar(15)); stm.executeUpdate(query);

Chapter 1130 Processing ResultSets Use getXXX() method, where XXX represents data type. For example: getString(name) The parameter passed can be column index, which starts at 1 (not 0) Use the next() method of a ResultSet to navigate to the next record

Chapter 1131 Closing the Connection Close connection: 1.Close ResultSet 2.Close Statement 3.Close Connection

Chapter 1132 Registering a Database as an ODBC Data Source To use JDBC-ODBC bridge to connect to Microsoft Access database, you need to register the database as an ODBC data source in advance

Chapter 1133 ODBC Data Source Administrator Dialog Box

Chapter 1134 Create New Data Source Dialog Box

Chapter 1135 ODBC Microsoft Access Setup Dialog Box