Dbutils class
ResultsethandlerInterface
MaplisthandlerClass (implementing the resultsethandler Interface) puts all records queried from the database into the list set. Each object in the list set is of the map type, you can read the corresponding value based on the field name of the record.
BeanlisthandlerClass (implementing the resultsethandler Interface) puts records from the database into the list set. Each object in the list set is a JavaBean object, and the value can be obtained according to the get method.
QreryrunnerClass execution SQL language name, one of which is the above two types of objects
Import Java. SQL. connection; import Java. SQL. sqlexception; import Java. util. list; import javax. naming. context; import javax. naming. initialcontext; import javax. naming. namingexception; import javax. SQL. datasource; import Org. apache. commons. dbutils. queryrunner; import Org. apache. commons. dbutils. resultsethandler;/*** this class mainly performs Database Operations **/public class sqlutil {Private Static string datasoucename = "Java: COMP/ENV/jdbc/exam "; private datasource da; Private Static sqlutil = NULL; public static sqlutil newinstance () {If (sqlutil = NULL) {sqlutil = new sqlutil ();} return sqlutil ;} public connection getconnection () throws sqlexception {synchronized (DA) {return da. getconnection () ;}}/*** constructor. Set the data source * @ Param datasoucename */private sqlutil () {Init ();} /*** Method for initializing the data source */private void Init () {try {context Ct = new initialcontext (); this. DA = (datasource) CT. lookup (datasoucename); Ct. close ();} catch (namingexception e) {e. printstacktrace () ;}}/*** to add, delete, modify, and delete data, an array of objects is required, data is a placeholder * @ Param SQL * @ Param Pring * @ return */Public Boolean Update (string SQL, object Pring []) {queryrunner Qu = new queryrunner (); int I = 0; Boolean flag = false; try {I = Qu. update (getconnection (), SQL, Pring); if (I> = 0) {flag = true ;}} catch (sqlexception e) {e. printstacktrace ();} return flag;}/*** A objece array is required for the data query method. The data is a placeholder for SQL statements, the resultsethandler parameter is the type of data conversion you want to query * It Can Be A JavaBean (such as: New beanlisthandler (JavaBean. class) or: New maplisthandler) * @ Param SQL * @ Param Pring parameter array can be empty * @ Param RSH * @ return */public list query (string SQL, object Pring [], resultsethandler rsh) {queryrunner Qu = new queryrunner (); List result = NULL; try {result = (list) Qu. query (getconnection (), SQL, Pring, rsh);} catch (sqlexception e) {// todo auto-generated catch blocke. printstacktrace ();} return result ;}}
Call:
/*** Delete * @ return Boolean * @ Param itempool ID */Public Boolean deteitempool (string ipid) {string sql1 = "delete from itempool where ipid =? "; // Specify the required parameter array object [] Pring = {ipid}; // execute the update boolean result = SQL. Update (sql1, Pring); return result ;}
/*** Method for searching question bank by question bank Id */Public itempool searchitempoolbyid (string ipid) {string sql1 = "select * From itempool where ipid =? "; // Specify the parameter array object [] Pring = {ipid}; List list = SQL. query (sql1, Pring, new beanlisthandler (itempool. class); itempool = (itempool) list. get (0); Return itempool ;}