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 );
}
}
}