150 likes | 284 Views
Adding Power to Database Access with JSP. JavaServer Pages By Xue Bai. 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
E N D
Adding Power to Database Access with JSP JavaServer Pages By Xue Bai Chapter 12
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 Chapter 12
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, …) Chapter 12
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.’) Chapter 12
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 Chapter 12
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 Chapter 12
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(); Chapter 12
Database Metadata Chapter 12
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 Chapter 12
Figure 12-3: Column Data Chapter 12
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) Chapter 12
Auto-Commit Mode • Default mode • To use transaction control, the auto-commit mode must be set to false: conn.setAutoCommit(false); Chapter 12
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(); Chapter 12
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 Chapter 12
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 Chapter 12