Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 121 Adding Power to Database Access with JSP JavaServer Pages By Xue Bai.

Similar presentations


Presentation on theme: "Chapter 121 Adding Power to Database Access with JSP JavaServer Pages By Xue Bai."— Presentation transcript:

1 Chapter 121 Adding Power to Database Access with JSP JavaServer Pages By Xue Bai

2 Chapter 122 Objectives In this chapter, you will: Create more powerful query statements using the IN and BETWEEN Access database metadata from a JSP page Control transactions to ensure database integrity Create and use beans to access a database

3 Chapter 123 The IN Operator The IN operator is used to determine whether a field’s value falls within a specified range of values The use of the IN operator takes the following form: SELECT column_list FROM table_name WHERE a_column_name IN (value1, value2, …)

4 Chapter 124 The IN Operator The series values listed within the IN operator can be the result of another nested query SELECT * FROM purchase WHERE custnum IN ( SELECT custid FROM customer WHERE custname = ‘Weiei Co.’)

5 Chapter 125 The BETWEEN Operator The BETWEEN operator is used to determine whether a column’s value is between one value and another The use of the BETWEEN operator takes the following form: SELECT column_list FROM table_name WHERE a_column_name BETWEEN value1 AND value2

6 Chapter 126 Metadata Metadata in a database is data about the database itself, for example, information about the tables, the columns in a table, the column data type, and so on JDBC allows you to access information about the whole database as well as information about a table within a database

7 Chapter 127 Database Metadata Information about the database Use connection object to get a reference to DatabaseMetaData object, which provides methods to get information about the database DatabaseMetaData dbmd = conn.getMetaData();

8 Chapter 128 Database Metadata

9 Chapter 129 Get Information About Table Use ResultSetMetaData object to get table information: ResultSet rst = stm.executeQuery("select * from customer"); ResultSetMetaData rsmd = rst.getMetaData(); ResultSetMetaData object methods: –getColumnCount(), which returns the number of columns in the ResultSet object –getColumnName(int column), which returns the designated column's name, and takes a parameter which is the column number –getColumnDisplaySize(int column), which indicates the designated column's normal maximum width in characters

10 Chapter 1210 Figure 12-3: Column Data

11 Chapter 1211 Transactions A transaction is a set of one or more statements that are executed together as a unit, so either all of the statements are executed, or none of the statements are executed Within a transaction, you can actually control when each statement is executed, and when all actions take effect, or even abort the whole transaction (leaving the database unchanged)

12 Chapter 1212 Auto-Commit Mode Default mode To use transaction control, the auto- commit mode must be set to false: conn.setAutoCommit(false);

13 Chapter 1213 Committing a Transaction If auto-commit is set to false, all transactions are not complete until the SQL statements are either committed or aborted When a statement is committed, any change made by the SQL statement is reflected in the database; when a statement is aborted, the database remains unchanged To commit a SQL statement, use the following syntax: conn.commit();

14 Chapter 1214 Abort a Transaction conn.rollBack() Calling this method aborts a transaction and leaves the database unchanged within the current transaction Therefore, you can use the auto-commit mode combined with the commit and rollback methods to control transactions

15 Chapter 1215 Beans and Databases You can create various beans to facilitate database processing: –A connection bean to connect to a database –A database meta data bean to get database meta information –A table information bean that helps to get table information –A bean that helps to execute SQL statements and displays results


Download ppt "Chapter 121 Adding Power to Database Access with JSP JavaServer Pages By Xue Bai."

Similar presentations


Ads by Google