Package COM. itheima. util; import Java. SQL. connection; import Java. SQL. parametermetadata; import Java. SQL. preparedstatement; import Java. SQL. resultset; import Java. SQL. sqlexception; import javax. SQL. datasource;/*** this class is the auxiliary class of the SQL statement * @ author Simon **/public class dbassit {// When referencing this class, the private datasource data source will be required to be imported; public dbassit (datasource) {This. datasource = datasource;} // This method is suitable for adding, deleting, and modifying public Void Update (string SQL, object [] PARAM) {connection conn = NULL; preparedstatement PST = NULL; try {conn = datasource. getconnection (); PST = Conn. preparestatement (SQL); // obtain the parameter metadata of each parameter in the preparedstatement object. parametermetadata PM = PST. getparametermetadata (); // obtain the number of parameters in an SQL statement. Int pmcount = PM. getparametercount (); If (pmcount> 0) {// first, you must determine whether the passed parameter is null, the number of parameters must be the same as the number of placeholders in the SQL statement. If (Param = NULL & Param. length! = Pmcount) {Throw new runtimeexception ();} // If the SQL statement parameters are the same as the number of input parameters, use preparestatement to set the placeholder value for (INT I = 0; I <pmcount; I ++) {PST. setobject (I + 1, paraminii000000000000000000000000pst.exe cuteupdate ();} catch (sqlexception e) {e. printstacktrace () ;}finally {closeall (Conn, Pst, null) ;}/// Delete, resultsethandler RSH this is an interface public object query (string SQL, object [] Param, resultsethandler rsh) {connection conn = NULL; preparedstatement PST = NULL; Res Ultset rs = NULL; try {conn = datasource. getconnection (); PST = Conn. preparestatement (SQL); // obtain the parameter metadata of each parameter in the preparedstatement object. parametermetadata PM = PST. getparametermetadata (); // obtain the number of parameters in an SQL statement. Int pmcount = PM. getparametercount (); If (pmcount> 0) {// first, you must determine whether the passed parameter is null, the number of parameters must be the same as the number of placeholders in the SQL statement. If (Param = NULL & Param. length! = Pmcount) {Throw new runtimeexception ();} // If the SQL statement parameters are the same as the number of input parameters, use preparestatement to set the placeholder value for (INT I = 0; I <pmcount; I ++) {PST. setobject (I + 1, Param [I]) ;}} rs = pst.exe cutequery ();} catch (sqlexception e) {e. printstacktrace ();} // return the result set and return an object return RSH. handler (RS);} private void closeall (connection Conn, preparedstatement ps, resultset RS) {If (Conn! = NULL) {try {conn. Close () ;}catch (sqlexception e) {e. printstacktrace () ;}conn = NULL ;}if (PS! = NULL) {try {ps. Close () ;}catch (sqlexception e) {e. printstacktrace () ;}ps = NULL ;}if (RS! = NULL) {try {Rs. Close () ;}catch (sqlexception e) {e. printstacktrace () ;}rs = NULL ;}// this method is suitable for query operations}
package com.itheima.util;import java.lang.reflect.Field;import java.sql.ResultSet;import java.sql.ResultSetMetaData;import java.sql.SQLException;public class BeanHandler implements ResultSetHandler{public Class clazz;public BeanHandler(Class clazz){this.clazz = clazz;}public Object handler(ResultSet rs){try{if(rs.next()){Object instance = clazz.newInstance();ResultSetMetaData rsmd = rs.getMetaData();int count = rsmd.getColumnCount();for(int i=0;i<count;i++){String name = rsmd.getColumnName(i+1);Object obj = rs.getObject(i+1);Field field = clazz.getDeclaredField(name);field.setAccessible(true);field.set(instance, obj);}return instance;}else{return null;}}catch (Exception e){e.printStackTrace();}return null;}}
package com.itheima.util;import java.lang.reflect.Field;import java.sql.ResultSet;import java.sql.ResultSetMetaData;import java.util.ArrayList;import java.util.List;public class BeanListHandler implements ResultSetHandler{private Class clazz;public BeanListHandler(Class clazz){this.clazz = clazz;}public Object handler(ResultSet rs){List list = new ArrayList();try{while(rs.next()){Object instance = clazz.newInstance();ResultSetMetaData rsmd = rs.getMetaData();int count = rsmd.getColumnCount();for(int i=0;i<count;i++){String name = rsmd.getColumnName(i+1);Object obj = rs.getObject(i+1);Field field = clazz.getDeclaredField(name);field.setAccessible(true);field.set(instance, obj);}list.add(instance);}return list;}catch (Exception e){e.printStackTrace();}return null;}}package com.itheima.util;import java.sql.ResultSet;public interface ResultSetHandler{Object handler(ResultSet obj);}Below is the test class
package com.itheima.util;import java.util.List;import org.junit.Test;import com.itheima.util.Account;public class AccountDao{private DbAssit da = new DbAssit(new C3P0Util().getDatasource());@Testpublic void add(){String sql = "insert into account(id,name,money) values(?,?,?)";da.update(sql, new Object[]{8,"ggg",1000});}@Testpublic void update(){String sql = "update account set money = 1000";da.update(sql, null);}@Testpublic void delete(){String sql = "delete from account where id = ?";da.update(sql, new Object[]{8});}@Testpublic void query(){String sql = "select * from account";ResultSetHandler rsh = new BeanListHandler(Account.class);List<Account> as = (List<Account>) da.query(sql, null,rsh );for(Account ac :as){System.out.println(ac);}}}