DBUtils原理及基本使用

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/c1523456/article/details/70185647

首先我们通过一个基本的演示来了解一下DBUtils的基本原理

public class UseDButils {

	public static void main(String[] args) {
		
		/*addUser(new User("weijinhao","123456"));
		quary("weijinhao");*/
	}

	public static  void addUser(User user) {
		ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource();
		QR qr = new QR(comboPooledDataSource);
		String sql = "insert into users values(?,?)";
		Object[] para = {user.getUsername(),user.getPassword()};
		qr.update(sql, para);
	}
	public static void quary(String username) {
		ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource();
		QR<User> qr = new QR<User>(comboPooledDataSource);
		String sql = "select * from users where username = ?";
		Object[] para = {username};
		User user = (User) qr.quary(new QRHandler<User>() {  //我们在这使用的是匿名内部类
			public User handler(ResultSet rs) {
				User user = new User();
				try {
					rs.next();
					user.setUsername(rs.getString(1));
					user.setPassword(rs.getString(2));
				} catch (SQLException e) {
					e.printStackTrace();
				}
				return user;
			}
		}, sql,para);
		System.out.println(user);
	}

}


/**
 * 
 * 我们用这个类来模拟一下DBUtils类的结构,麻雀虽小五脏俱全
 * 
 */
public class QR<T> {
	private DataSource ds;
	public QR() {
		super();
	}
	public QR(DataSource ds) {   
		super();
		this.ds = ds;
	}
	/*
	 * 给出sql和object[]来实现增删改查
	 */
	public void update(String sql ,Object...objects)   {  
		Connection con = null;
		PreparedStatement ps = null;
		try{
			con = ds.getConnection();
			ps = con.prepareStatement(sql);
			init(ps,objects);  //我们调用该函数来进行prepareStatement的初始化
			ps.executeUpdate();
		}catch(SQLException s){
			s.printStackTrace();
		}finally{
			try {
				ps.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			try {
				con.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	/*
	 * QRHandler<T>用该对象来进行表数据到对像属性的映射,其实在DBUtils中不用我们自己写该接口的实现类,有写好的可以直接使用
	 * sql为要查询的语句
	 * 
	 */
	public T quary(QRHandler<T> hd, String sql ,Object...objects) {
		Connection con = null;
		PreparedStatement ps = null;
		T user = null;
		try{
			con = ds.getConnection();
			ps = con.prepareStatement(sql);
			init(ps,objects);
			ResultSet rs = ps.executeQuery();
			user =  hd.handler(rs);  //用该对象来进行表数据到对像属性的映射
		}catch (SQLException e) {
			e.printStackTrace();
		}finally {
			try {
				ps.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
			try {
				con.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
			return user;
		}
	}
	
	void init(PreparedStatement ps,Object...objects) {
		for(int i = 0;i < objects.length;i++) {
			try {
				ps.setObject(i + 1, objects[i]);
			} catch (SQLException e) {
				e.printStackTrace();
			}
			
		}
	}
}

public interface QRHandler<T> {
	T handler(ResultSet rs) ;
}
好了,如果看懂了上面的源码,那你就算是基本会了DBUtils的使用了。

让我们来看一下DBUtils类的使用源码:

public class UseDBUtils {

	public static void main(String[] args) throws SQLException {
		//update();
		//query();
	}

	public static void query() throws SQLException {
		ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource();
		QueryRunner qr = new QueryRunner(comboPooledDataSource);
		String sql = "select * from users where username=?";
		BeanHandler<User> rsh = new BeanHandler<User>(User.class);//关于结构集处理器有好几中,还可以自己实现自己的结构集处理器
		User user = qr.query(sql, rsh, "zhoufeifei");
		System.out.println(user);
	}

	public static void update() throws SQLException {
		ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource();
		QueryRunner qr = new QueryRunner(comboPooledDataSource);
		String sql = "insert into users values(?,?)";
		Object[] params = {"zhoufeifei" , "123456"};
		int i = qr.update(sql, params);
		System.out.println(i);
	}

}




展开阅读全文

没有更多推荐了,返回首页