java--dbutil
package com.dbutil;
import java.sql.Connection;
import java.sql.ResultSet;
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;
import org.apache.commons.dbutils.ResultSetHandler;
import org.junit.Test;
import com.mchange.v2.c3p0.ComboPooledDataSource;
public class TestDButil {
@Test
public void TestDButil() throws Exception {
ComboPooledDataSource dataSource =new ComboPooledDataSource();
dataSource.setDriverClass("com.mysql.jdbc.Driver");
dataSource.setJdbcUrl("jdbc:mysql://192.168.195.122/test?useSSL=false");
dataSource.setUser("root");
dataSource.setPassword("123456");
QueryRunner queryRunner =new QueryRunner(dataSource);
//String sql ="insert into s_stu(name,age) values(?,?)";
//String sql ="select * from s_stu where id=?";
//queryRunner.update(sql,"dbutil",24);
/*
* Accout accout= queryRunner.query(sql, new ResultSetHandler<Accout>() {
*
* @Override public Accout handle(ResultSet rs) throws SQLException { // TODO
* Auto-generated method stub Accout accout =new Accout(); while(rs.next()) {
* String name =rs.getString("name"); int age = rs.getInt("age");
* //System.out.println(name+"---"+age); accout.setName(name);
* accout.setAge(age);
*
*
* }
*
* return accout; }
*
* },20); System.out.println(accout.toString());
*/
/*
* //单行查询
* ResultSetHandler<Accout> rs= new BeanHandler(Accout.class); Accout
* accout= queryRunner.query(sql,rs,23); System.out.println(accout);
*/
//多行查询
String sql ="select * from s_stu ";
ResultSetHandler<Accout> rs= new BeanListHandler(Accout.class);
List<Accout> accout= (List<Accout>) queryRunner.query(sql,rs);
for (Accout accout2 : accout) {
System.out.println(accout2);
}
}
}
******************************人因为有理想、梦想而变得伟大,而真正伟大就是不断努力实现理想、梦想*****************************