Some methods of DBCP connection pooling

Source: Internet
Author: User
Tags connection pooling stmt

Create a connection


Connection conn =null; =null; = dbcp.getconnection (); St=conn.createstatement ();

1. Batch processing Addbatch ()

You can centralize the strings of multiple operational databases

String sql1= "Select ..... ";

String sql2= "Select ..... ";

..............

St.addbatch (SQL1);

St.addbatch (SQL2);

..............

St.executebatch (); Centralized processing

St.clearbatch ();//better clean up in time, otherwise the string will remain in Addbatch

2. Object Management Setautocommit (Boolean);

Some logic must be in one transaction to complete: Through transaction management You can have multiple logic complete in one transaction.

Thing characteristics: ACID

Atomicty atomicity: The transaction must be an atomic unit of work, and the modification of the data is either fully executed or not executed.

Consistency consistency: When a transaction is complete, all data must be in a consistent state.

Isolation isolation: Modifications made by concurrent firms must be isolated from other concurrent transactions.
Durability persistence: After a transaction completes, its effect on the system is permanent.

Conn.setautocommit (FALSE); Turn off autocommit, default is True

St.executeupdate (SQL1);

St.executeupdate (SQL2);

Conn.commit (); Manual commit, Action SQL1 and SQL2 are executed, or both do not execute

3.preparedStatement: Pre-compiled to effectively prevent injection attacks

String sql= "SELECT * from emp_xx where ename = Upper (?) and Sal >?";

Connection conn=dbcp.getconnection ();
PreparedStatement p=conn.preparestatement (SQL);
P.setstring (1, "Scott");
P.setdouble (2, 1000.0);
ResultSet Rs=p.executequery ();

4. Result set

@Test Public voidTest8 () {String SQL= "SELECT * FROM Emp_xx"; Connection Conn=NULL; ResultSet RS=NULL; Try{conn=dbcp.getconnection (); PreparedStatement PS=conn.preparestatement (SQL); RS=Ps.executequery (); ResultSetMetaData Meta=rs.getmetadata ();//get the metadata set        intNumber= Meta.getcolumncount ();//get the number of columns in the result setSystem.out.println (Meta.getcolumntypename (1));//gets the ordinal number of the type column of a column starting from 1         for(inti = 1; I <=number; i++) {String columname=meta.getcolumnname (i);//traverse to get all column names in the result setSystem.out.println (columname); }            } Catch(SQLException e) {//TODO auto-generated Catch blockE.printstacktrace (); }}

5. Scrollable result set

@Test Public voidTest9 () {String SQL= "Select Ename,sal from Emp_xx order by Sal"; Connection Conn=NULL; ResultSet RS=NULL; Try{conn=dbcp.getconnection (); //If you want to create a scrollable result set, statement or preparestatement is created differently://Statement stmt = conn.createstatement (Type,concurrney);//PreparedStatement PS = conn.preparestatement (Sql,type,concurrney);            /*** Where type is value: * type_forward_only: Default, only read down * type_scroll_insenstive: Scrollable , not aware of data changes * type_scroll_senstive: scrollable, perceptual data changes * where Concurrency: * concur_read_only: Only Read, Cannot update * concur_updatable: Can update the result set*/Statement stmt=conn.createstatement (resultset.type_scroll_sensitive, resultset.concur_updatable); RS=stmt.executequery (SQL); /*** The usual way to move pointers: * First: pointer moves to number one * Last: pointer moves to final bar * Beforefirst:              * Afterlast: * IsFirst: Determine whether to point to the first * Islast: is the last bar * Isbeforefirst:               * Isafterlast: * Relative: Move to the relative position of the current pointer * Next: Move to the next * previous: Move to the previous bar * Absolute: Move to absolute position*/             //To update the result set data:Rs.absolute (3);//Move Pointer to 3rd recordRs.updatedouble (2, 2000);//Update the second column of data into 1555//Rs.updaterow ();//updating data source dataRs.first ();  while(Rs.next ()) {System.out.print (rs.getstring ("ename") + ""); System.out.println (Rs.getstring ("Sal")); }                    } Catch(SQLException e) {e.printstacktrace (); }finally {            if(conn!=NULL){                Try{conn.close (); } Catch(SQLException e) {//TODO auto-generated Catch blockE.printstacktrace (); }            }        }        }

Some methods of DBCP connection pooling

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.