Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 111 Databases with JSP JavaServer Pages By Xue Bai.

Similar presentations

Presentation on theme: "Chapter 111 Databases with JSP JavaServer Pages By Xue Bai."— Presentation transcript:

1 Chapter 111 Databases with JSP JavaServer Pages By Xue Bai

2 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

3 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

4 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

5 Chapter 115 Table 11-1: Sample Database Table Item NumberItem NameUnit PriceInventory 100Camera$267.9913 101Washer$489.568 102TV$189.9929

6 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

7 Chapter 117 Using Common Fields to Link Two Tables productIDproductNamemodelPricemanufacturerI D 100WasherD1356.991 200TVS2255.682 manufacturerIDnameaddressphone 1Weiwei Co.Edward Rd 123456 2XYZ Co.Central654321

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

9 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

10 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 +2 31 -1 SMALLINT31For whole number data between –2 15 and +2 15 -1 FLOAT2.6E+10Very large or vary small numbers DATE11/16/2001For date. Implementations vary in different databases

11 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

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

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

14 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

15 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

16 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

17 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

18 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

19 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

20 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

21 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

22 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

23 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);

24 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( ;

25 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

26 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);

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

28 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

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

30 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

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

32 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

33 Chapter 1133 ODBC Data Source Administrator Dialog Box

34 Chapter 1134 Create New Data Source Dialog Box

35 Chapter 1135 ODBC Microsoft Access Setup Dialog Box

Download ppt "Chapter 111 Databases with JSP JavaServer Pages By Xue Bai."

Similar presentations

Ads by Google