Sometimes we are interested in only one column in a table, for example, we want to query the name of the <25-Year-old student in the student's table, and the result should be the List<string> object, at which point Columnlisthandler will come in handy, and the sample code is as follows:
Package com.ricky.java.junit;
Import java.sql.SQLException;
Import java.util.List;
Import Javax.sql.DataSource;
Import Org.apache.commons.dbutils.handlers.ColumnListHandler;
Import Com.ricky.java.common.dao.AbstractDAO;
Import Com.ricky.java.common.db.DataSourceManager;
public class Studentdao extends Abstractdao {public
list<string> querynamelist ()
throws Return Qr.query ("select name from Dict_student where age<25", New columnlisthandler<string> (1));
@Override
protected DataSource Getdatasource () {return
datasourcemanager.getinstance (). Getdatasource ();
}
}
Abstractdao.java
Package Com.ricky.java.common.dao;
Import Javax.sql.DataSource;
Import Org.apache.commons.dbutils.QueryRunner;
Import Org.apache.log4j.Logger;
/**
* Implement common Database CRUD operations Interface base class
* @author bingbing Feng
* *
* @version 2014-10-13 14:30
V1.1
*
* * * Public abstract class Abstractdao {
protected Logger Mlogger = Logger.getlogger ("Devlog");
protected Queryrunner QR = new Queryrunner (Getdatasource ());
Get DataSource
protected abstract DataSource getdatasource ();
}