1、DBUtils下载:http://commons.apache.org/proper/commons-dbutils/download_dbutils.cgi
2、API
3、简单实例,参考:http://www.open-open.com/lib/view/open1342664230401.html
(1)经过加工的DBHelper
package cn.tan.dbutils;
import java.io.File;
import java.io.FileInputStream;
import java.util.Properties;
import javax.sql.DataSource;
import org.apache.commons.dbcp.BasicDataSource;
import org.apache.commons.dbutils.QueryRunner;
public class DBHelper {
private static String JDBCPROPERTIES = "jdbc.properties";
private static String PATH = "../DBUtilsTest/src/cn/tan/dbutils/";
private static DataSource dataSource;
private DBHelper() {
}
public static QueryRunner getQueryRunner() {
if (DBHelper.dataSource == null) {
// 配置dbcp数据源
Properties prop = getProperties();
BasicDataSource dbcpDataSource = new BasicDataSource();
dbcpDataSource.setUrl(prop.getProperty("jdbc.url"));
dbcpDataSource.setDriverClassName(prop.getProperty("jdbc.driverClassName"));
dbcpDataSource.setUsername(prop.getProperty("jdbc.username"));
dbcpDataSource.setPassword(prop.getProperty("jdbc.password"));
dbcpDataSource.setDefaultAutoCommit(Boolean.valueOf(prop.getProperty("jdbc.DefaultAutoCommit")));
dbcpDataSource.setMaxIdle(Integer.valueOf(prop.getProperty("jdbc.maxidle")));
dbcpDataSource.setMaxWait(Integer.valueOf(prop.getProperty("jdbc.maxwaitmillis")));
DBHelper.dataSource = (DataSource) dbcpDataSource;
}
return new QueryRunner(DBHelper.dataSource);
}
/**
* 返回Properties
*
* @return
*/
private static Properties getProperties() {
Properties prop = new Properties();
try {
FileInputStream fis = new FileInputStream(new File(PATH + JDBCPROPERTIES));
prop.load(fis);
} catch (Exception e) {
e.printStackTrace();
}
return prop;
}
public static void main(String[] args) {
getProperties();
}
}
(2)JDBC配置文件
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/book?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull
jdbc.username=root
jdbc.password=123456
jdbc.maxidle=100
jdbc.maxwaitmillis=500
jdbc.DefaultAutoCommit=true
(3)简单操作:
package cn.tan.dbutils;
import java.sql.SQLException;
import java.util.List;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
public class BookDao {
private QueryRunner qrunner;
public int add(Book book) {
String sql = "insert into t_book(name,author,price,date) values(?,?,?,?)";
qrunner = DBHelper.getQueryRunner();
try {
return qrunner.update(sql, book.getName(), book.getAuthor(), book.getPrice(), book.getDate());
} catch (SQLException e) {
e.printStackTrace();
return 0;
}
}
@SuppressWarnings({ "unchecked", "rawtypes" })
public List<Book> getallbooks() {
String sql = "select * from t_book";
qrunner = DBHelper.getQueryRunner();
try {
return qrunner.query(sql, new BeanListHandler(Book.class));
} catch (SQLException e) {
e.printStackTrace();
return null;
}
}
public Book getbookbyid(int id){
String sql = "select * from t_book where id=?";
qrunner = DBHelper.getQueryRunner();
try {
return qrunner.query(sql,new BeanHandler<Book>(Book.class),id);
} catch (SQLException e) {
e.printStackTrace();
return null;
}
}
public static void main(String[] args) {
Book book = new Book();
book.setAuthor("红薯");
book.setName("开源中国");
book.setPrice(50);
book.setDate("2013-11-14");
BookDao bookDao = new BookDao();
//System.out.println("insert num:" + bookDao.add(book));
List<Book> booklist=bookDao.getallbooks();
System.out.println("books num:"+booklist.size());
Book book1=bookDao.getbookbyid(1);
System.out.println("result:"+book1.toString());
}
}