Introduction to JSP Database Advanced Operations

Source: Internet
Author: User

This section briefly introduces three tips for JSP database operations: data sorting, Master/Slave table processing, and transaction processing.

1. Data Sorting

In JSP database technology, you can use the ORDER clause to sort query results [order by {order_by_exdivssion [ASC | DESC]} [limit... N], where, order_by_exdivssion is used to specify the columns to be sorted; ASC is specified to sort in ascending order; DESC is specified to sort in descending order.

 
  1. Class.forName( "com.microsoft.jdbc.sqlserver.SQLServerDriver"
    ).new Instance( );  
  2. String url="jdbc:microsoft:sqlserver://localhost:1433;
    DatabaseName=pubs"
    ;  
  3. String user="dxaw";  
  4. String password="123";  
  5. Connection conn=DriverManager.getConnection( url, user, password );  
  6. Statement st=conn.createStatement( ResultSet.TYPE_SCROLL_SENSITIVE,  
  7.  ResultSet.CONCUR_UPDATABLE );  
  8. String sql="select * from tbl order by id desc";  
  9. ResultSet rs=st.executeQuery( sql );  
  10. while( rs.next )...{  
  11.     out.println( rs.getString( "..." ) );  
  12. }  
  13. rs.close( );  
  14. st.close( );  
  15. conn.close( ); 

2. Master/Slave table Processing

The IN clause and EXISTS clause are used to implement master-slave table queries.

An IN clause is a data retrieval method connected by IN or not in. The returned results can contain zero or multiple values.

The EXISTS clause is a data retrieval method connected by EXISTS or not exists. Its function is to determine whether data rows are returned in the subquery result set, and return TRUE or FALSE, instead of returning other actual data. Because you do not need to return specific values in this seed query, the selection list of this seed query often uses the "SELECT *" format, and the WHERE clause of its outer layer does not need to specify the column name.

 
  1. select au_id, au_name form authors where au_id IN (   
  2. select au_id form titleauthor where title_id IN (   
  3. select title_id form titles where type="business" 
  4. )  
  5. ) 

3. Transaction Processing

Transactions are one of the core concepts in JSP database theory. If a group of processing steps either occur or are not executed, we call the group processing steps as a transaction. When all the steps are fully executed like an operation, we say the transaction is committed. The transaction must be rolled back to the initial system status because some or more of the steps fail to be committed ). Transactions must follow the ACID principles established by ISO/IEC. ACID is short for atomicity), consistency), isolation and persistent durability. The atomicity of a transaction indicates that any failure during the transaction execution will invalidate any modification made by the transaction. Consistency indicates that when the transaction fails, all data affected by the transaction should be restored to the status before the transaction is executed. Isolation indicates the modification of data during transaction execution, which is invisible to other transactions before the transaction is committed. Durability indicates that the data status should be correct when the transaction fails.

In JDBC, how does one combine multiple SQL statements into one transaction? In JDBC, when a Connection object is opened, the default mode is auto-commit. Each SQL statement is treated as a transaction, that is, each statement is automatically confirmed by the transaction. In order to combine multiple SQL statements into a transaction, We need to block the auto-commit mode. If the commit () method is not called after the auto-commit mode is disabled, the SQL statement will not be confirmed by the transaction. All SQL statements after the last call of the commit () method are confirmed when the method commit () is called.

 
  1. Public int delete (int sID ){
  2.  Dbc=NewDataBaseConnection ();
  3. ConnectionCon=Dbc. GetConnection ();
  4. Try {
  5. Con. setAutoCommit (false); // change the default commit method of JDBC transactions
  6. Dbc.exe cuteUpdate ("delete from tab1 whereID= "+ SID );
  7. Dbc.exe cuteUpdate ("delete from tab2 whereID= "+ SID );
  8. Dbc.exe cuteUpdate ("delete from tab3 whereBylawid= "+ SID );
  9. Con. commit (); // submit a JDBC transaction
  10. Con. setAutoCommit (true); // restore the default commit Method for JDBC transactions
  11. Dbc. close ();
  12. Return 1;
  13. }
  14. Catch (Exception exc ){
  15. Con. rollBack (); // roll back JDBC transactions
  16. Exc. printStackTrace ();
  17. Dbc. close ();
  18. Return-1;
  19. } View the source of this Article

  1. Notes for connecting JSP to ORACLE databases
  2. Configuration of tomcat SQL Server2000 Database Connection Pool in JSP
  3. Differences between Servlets and JSP
  4. Differences between JSP include commands and include actions
  5. Solutions to JSP output of excel documents and Chinese garbled characters

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.