Common steps for DAO Operations:
0. Write SQL statement 1. Gets the connection 2. Create stmt3. Execute sqla) update b) query 4. Close/exception
Code:
Basedao
/*** General DAO, all DAO written by himself inherits this class; * This class defines 2 common methods: * 1. Update * 2. Enquiry *@authorJie.yuan **/ Public classBasedao {//Initialize Parameters PrivateConnection con; PrivatePreparedStatement pstmt; PrivateResultSet rs; /*** General method of updating *@paramSQL Update for SQL statement (update/insert/delete) *@paramParamsvalue The value of the placeholder in the SQL statement (if there is no placeholder, pass in null)*/ Public voidupdate (String sql,object[] paramsvalue) {Try { //Get ConnectionsCon =jdbcutil.getconnection (); //to create a stmt object that executes a commandPSTMT =con.preparestatement (SQL); //parameter metadata: Gets the number of placeholder parameters intCount =pstmt.getparametermetadata (). GetParameterCount (); //set the value of a placeholder parameter if(Paramsvalue! =NULL&& paramsvalue.length > 0) { //loop to assign a value to a parameter for(inti=0;i<count;i++) {Pstmt.setobject (i+1, Paramsvalue[i]); } } //Perform the updatepstmt.executeupdate (); } Catch(Exception e) {Throw NewRuntimeException (e); } finally{Jdbcutil.closeall (con, pstmt,NULL); } } /*** General method of query *@paramSQL *@paramParamsvalue*/ Public<T> list<t> query (String sql, object[] paramsvalue,class<t>clazz) { Try { //the returned collectionlist<t> list =NewArraylist<t>(); //ObjectT t =NULL; //1. Get the connectionCon =jdbcutil.getconnection (); //2. Create a stmt objectPSTMT =con.preparestatement (SQL); //3. Get the number of placeholder parameters and set the value of each parameter intCount =pstmt.getparametermetadata (). GetParameterCount (); if(Paramsvalue! =NULL&& paramsvalue.length > 0) { for(inti=0; i<paramsvalue.length; i++) {Pstmt.setobject (i+1, Paramsvalue[i]); } } //4. Execute the queryrs =Pstmt.executequery (); //5. Get result set metadataResultSetMetaData RSMD =Rs.getmetadata (); //---> Get the number of columns intColumnCount =Rsmd.getcolumncount (); //6. Traverse RS while(Rs.next ()) {//the object to encapsulatet =clazz.newinstance (); //7. Traverse each column of each row to encapsulate the data for(inti=0; i<columncount; i++) { //get column names for each columnString columnName = rsmd.getcolumnname (i + 1); //gets the column name for each column, corresponding to the valueObject value =Rs.getobject (columnName); //encapsulation: Set to the properties of the T object "beanutils component"Beanutils.copyproperty (t, columnName, value); } //Add the Encapsulated object to the list collectionList.add (t); } returnlist; } Catch(Exception e) {Throw NewRuntimeException (e); } finally{Jdbcutil.closeall (Con, pstmt, RS); } }}
Admindao:
Public classAdmindaoextendsBasedao {//Delete Public voidDeleteintID) {String SQL= "Delete from admin where id=?"; Object[] Paramsvalue={ID}; Super. Update (SQL, paramsvalue); } //Insert Public voidSave (Admin admin) {String SQL= "INSERT into admin (username,pwd) VALUES (?,?)"; Object[] Paramsvalue={admin.getusername (), Admin.getpwd ()}; Super. Update (SQL, paramsvalue); } //Search All PublicList<admin>GetAll () {String SQL= "SELECT * from admin"; List<Admin> list =Super. query (SQL,NULL, Admin.class); returnlist; } //Query by criteria (primary key) PublicAdmin FindByID (intID) {String SQL= "SELECT * from admin where id=?"; List<Admin> list =Super. query (SQL,NewObject[]{id}, Admin.class); return(list!=NULL&&list.size () >0)? List.get (0):NULL; } }
Extraction of DAO operations, Basedao