Query for a List using Dbutils

Source: Internet
Author: User

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 ();
}





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.