Apache DbUtils详解

1,连接类ConnectDb

import java.sql.SQLException;
import java.sql.Connection; 

public class ConnectDb {
	private static String driveClassName = "com.mysql.jdbc.Driver";
	private static String url = "jdbc:mysql://192.168.1.161:3306/test?useUnicode=true&characterEncoding=utf8"; 
	
	private static String user = "root";
	private static String password = "e-playnow";
	
	public static Connection Connect(){
		Connection conn = null;
		
		//load driver
		try {
			Class.forName(driveClassName);
		} catch (ClassNotFoundException  e) {
			System.out.println("load driver failed!");
			e.printStackTrace();
		}
		
		//connect db
		try {
			conn = DriverManager.getConnection(url, user, password);
		} catch (SQLException e) {
			System.out.println("connect failed!");
			e.printStackTrace();
		}		
		
		return conn;
	}
}

数据库表:

CREATE TABLE `user` (
  `id` int(11) NOT NULL auto_increment,
  `name` varchar(50) NOT NULL,
  `age` tinyint(10) NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=4 DEFAULT CHARSET=utf8

Bean:

package Beans;

public class UserBean {
	private int id; 
    private String name; 
    private int age;
    
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
}

Demo:

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
import org.apache.commons.dbutils.DbUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import Beans.UserBean;

public class main {

	public static void main(String[] args) throws SQLException {
		insert_test();
		del_test();
	}
	
	static void insert_test() throws SQLException{
		Connection conn = ConnectDb.Connect();
        
        //创建SQL执行工具 
        QueryRunner qRunner = new QueryRunner(); 
        
        //执行SQL插入 
        int n = qRunner.update(conn, "insert into user(name,age) values('xxx',22)"); 
        System.out.println("成功插入" + n + "条数据!"); 
        
        //关闭数据库连接 
        DbUtils.closeQuietly(conn); 	
	} 
	
	static void select_test() throws SQLException{
		Connection conn = ConnectDb.Connect();
        
        //创建SQL执行工具 
        QueryRunner qRunner = new QueryRunner(); 
        
        @SuppressWarnings("unchecked")
		List<UserBean> list = (List<UserBean>) qRunner.query(conn, "select id,name,age from user", new BeanListHandler(UserBean.class)); 
        //输出查询结果 
        for (UserBean user : list) { 
                System.out.println(user.getAge()); 
        } 
        
        //关闭数据库连接 
        DbUtils.closeQuietly(conn); 
	} 

	static void update_test() throws SQLException{
		Connection conn = ConnectDb.Connect();
        
        //创建SQL执行工具 
        QueryRunner qRunner = new QueryRunner(); 
        //执行SQL插入 
        int n = qRunner.update(conn, "update user set name = 'xxx',age=28"); 
        System.out.println("成功更新" + n + "条数据!"); 
        
        //关闭数据库连接 
        DbUtils.closeQuietly(conn); 
	} 
	
	static void del_test() throws SQLException{
		Connection conn = ConnectDb.Connect();
        
        //创建SQL执行工具 
        QueryRunner qRunner = new QueryRunner(); 
        //执行SQL插入 
        int n = qRunner.update(conn, "DELETE from user WHERE name='xxx';"); 
        System.out.println("删除成功" + n + "条数据!"); 
        
        //关闭数据库连接 
        DbUtils.closeQuietly(conn); 
	} 
}

补充个map的例子:

static void map_query(){
 Connection conn = ConnectDb.Connect();
 try {
    QueryRunner qr = new QueryRunner() ;
    ResultSetHandler rsh = new MapListHandler();
    String strsql = "select * from user";
    ArrayList result = (ArrayList)qr.query(conn ,strsql ,rsh);
    for(int i = 0 ; i < result.size() ; i++) {
        Map map = (Map)result.get(i);
    }
 } catch(Exception ex) {
    ex.printStackTrace(System.out);
 }
}

转自【http://blog.csdn.net/luxiaoyu_sdc/article/details/7361714】

你可能感兴趣的:(Apache DbUtils详解)