1. Write the dbassist class which simplifies the SQL query (package com. itheima. dbassist;
Import Java. SQL. connection; import Java. SQL. parametermetadata; import Java. SQL. preparedstatement; import Java. SQL. resultset; import javax. SQL. datasource; import COM. itheima. util. jdbcc3p0util; public class dbassist {private datasource Ds; Public dbassist (datasource DS) {This. DS = Ds;}/*** run the Add Delete update statement * @ Param SQL * @ Param Params parameter */Public void Update (string SQL, object [] Params) {connectio N conn = NULL; preparedstatement stmt = NULL; resultset rs = NULL; try {conn = jdbcc3p0util. getconnection (); stmt = Conn. preparestatement (SQL); // set the parameter parametermetadata MD = stmt. getparametermetadata (); int num = md. getparametercount (); // the number of parameters for (INT I = 0; I <num; I ++) {stmt. setobject (I + 1, Params [I]);} stmt.exe cuteupdate ();} catch (exception e) {Throw new runtimeexception (E);} finally {jdbcc3p0util. R Elasticsearch (RS, stmt, Conn) ;}} public object query (string SQL, object [] Params, resulthandler handler) {connection conn = NULL; preparedstatement stmt = NULL; resultset rs = NULL; try {conn = jdbcc3p0util. getconnection (); stmt = Conn. preparestatement (SQL); // set the parameter parametermetadata MD = stmt. getparametermetadata (); int num = md. getparametercount (); // the number of parameters for (INT I = 0; I <num; I ++) {stmt. setobject (I + 1, para MS [I]);} rs = stmt.exe cutequery (); // encapsulate the result set into the object o = handler. handle (RS); return O;} catch (exception e) {Throw new runtimeexception (E);} finally {jdbcc3p0util. release (RS, stmt, Conn) ;}} defines the add, delete, modify, and query method). It can be combined with resultsethandler to complete most database operations and greatly reduce the coding workload.
2. Compile the resulthandler interface. This interface is used to process java. SQL. resultset and convert the data to another form as required.
The resultsethandler interface provides a separate method: <t> T handle (resultset RS) makes a general method by using generics.
package com.itheima.dbassist; import java.sql.ResultSet; public interface ResultHandler { // Object handle(ResultSet rs); <T> T handle(ResultSet rs); }
3. Compile the implementation class of the resulthandler Interface
The following is a simple implementation class (like beanhandler in beanutils)
Package COM. itheima. dbassist. handler; import Java. lang. reflect. field; import Java. SQL. resultset; import Java. SQL. resultsetmetadata; import COM. itheima. dbassist. resulthandler;/*** query a record ** @ author a **/public class beanhandler <t> implements resulthandler {private class <t> clazz; Public beanhandler (class clazz) {This. clazz = clazz;} public t handle (resultset RS) {try {If (RS. next () {t o = clazz. newinstance (); resultsetmetadata MD = Rs. getmetadata (); int cloumnnum = md. getcolumncount (); For (INT I = 0; I <cloumnnum; I ++) {string columnname = md. getcolumnname (I + 1); object columnvalue = Rs. getObject (columnname); field = clazz. getdeclaredfield (columnname); field. setaccessible (true); field. set (O, columnvalue);} return O;} else return NULL;} catch (exception e) {Throw new runtimeexception (e );}}}
4. compress the compiled classes into jar packages.
Export --> JAR file --> remove. classpath,. Project, and other related tool files.
Files retained: only the classes written in step 1-3 can be retained.