Java universal Dao

Source: Internet
Author: User

 

Package Org. gaoyoubo. Dao;

ImportJava. SQL .*;
ImportJava. util .*;

ImportJavax. servlet. jsp. jstl. SQL .*;

Public Class Commandao {
Private Connection con;
Private String SQL;
Private LIST parameters;
// Set connection
Public Void Setcon (connection con ){
This . Con = con;
}
// Set SQL statements;
Public Void Setsql (string SQL ){
This . SQL = SQL;
}
// Set SQL statement parameters;
Public Void Setparameters (list aparametersrameters ){
This . Parameters = aparametersrameters;
}

/**
* Execute the query;
*
@ Return
*/
Public Result executequery (){
Result result = Null ;
Resultset rs = Null ;
Preparedstatement pstmt = Null ;


Try {
// If the SQL statement has parameters;
If (Parameters! = Null & Parameters. Size ()> 0 ){
Pstmt = con. preparestatement (SQL );
Setparameters (pstmt, parameters );
Rs = pstmt.exe cutequery ();
}
// If no parameter exists;
Else {
Pstmt = con. preparestatement (SQL );
Rs = pstmt.exe cutequery ();
}
} Catch (Sqlexception e ){
// Todo: handle exception
E. printstacktrace ();
}
// Convert resultset to result;
Result = resultsupport. toresult (RS );
Return Result;
}

/**
* Execute updates;
*
@ Return Returns the number of affected rows;
*/
Public Int Executeupdate (){
Int Noofrows = 0;
Resultset rs = Null ;
Preparedstatement pstmt = Null ;
Try {
// If the parameter is not blank;
If (Parameters! = Null & Parameters. Size ()> 0 ){
Pstmt = con. preparestatement (SQL );
Setparameters (pstmt, parameters );
Noofrows = pstmt.exe cuteupdate ();
}
Else {
Pstmt = con. preparestatement (SQL );
Noofrows = pstmt.exe cuteupdate ();
}
} Catch (Sqlexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
}
Return Noofrows;
}


/**
* Method: set parameters for SQL statements;
*
@ Param Pstmt command
*
@ Param Parameters parameter set;
*
@ Throws Sqlexception
*/
Private Void Setparameters (preparedstatement pstmt, LIST parameters) Throws Sqlexception {
For ( Int I = 0; I <parameters. Size (); I ++ ){
Object v = parameters. Get (I );
Pstmt. setobject (I + 1, V );
}
}

}

 

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.