JavaWeb 使用DBUtils實現增刪改查方式

JavaWeb 使用DBUtils實現增刪改查

1、創建C3p0Utils類

創建cn.itcast.jdbc.utils包

代碼如下:

package cn.itcast.jdbc.utils;
import javax.sql.DataSource;
import com.mchange.v2.c3p0.ComboPooledDataSource;
public class C3p0Utils {
	private static DataSource ds;
	static {
		ds = new ComboPooledDataSource();
	}
	public static DataSource getDataSource() {
		return ds;
	}
}

2、創建DBUtilsDao類

在src目錄下,創建一個cn.itcast.jdbc.demo的包,在該包下創建一個DBUtilsDao類

代碼如下:

package cn.itcast.jdbc.demo;
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 cn.itcast.chapter10.example.User;
import cn.itcast.jdbc.utils.C3p0Utils;
public class DBUtilsDao {
	// 查詢所有,返回List集合
	public List findAll() throws SQLException {
		// 創建QueryRunner對象
		QueryRunner runner = new QueryRunner(C3p0Utils.getDataSource());
		// 寫SQL語句
		String sql = "select * from user";
		// 調用方法
		List list = (List) runner.query(sql,
                     new BeanListHandler(User.class));
		return list;
	}
	// 查詢單個,返回對象
	public User find(int id) throws SQLException {
		// 創建QueryRunner對象
		QueryRunner runner = new QueryRunner(C3p0Utils.getDataSource());
		// 寫SQL語句
		String sql = "select * from user where id=?";
		// 調用方法
		User user = (User) runner.query(sql, 
                 new BeanHandler(User.class), new Object[] { id });
		return user;
	}
	// 添加用戶的操作
	public Boolean insert(User user) throws SQLException {
		// 創建QueryRunner對象
		QueryRunner runner = new QueryRunner(C3p0Utils.getDataSource());
		// 寫SQL語句
		String sql = "insert into user (name,password) values (?,?)";
		// 調用方法
		int num = runner.update(sql,
				new Object[] { user.getName(), user.getPassword() });
		if (num > 0)
			return true;
		return false;
	}
	// 修改用戶的操作
	public Boolean update(User user) throws SQLException {
		// 創建QueryRunner對象
		QueryRunner runner = new QueryRunner(C3p0Utils.getDataSource());
		// 寫SQL語句
		String sql = "update  user set name=?,password=? where id=?";
		// 調用方法
		int num = runner.update(sql, new Object[] { user.getName(),
                     user.getPassword(),user.getId() });
		if (num > 0)
			return true;
		return false;
	}
	// 刪除用戶的操作
	public Boolean delete(int id) throws SQLException {
		// 創建QueryRunner對象
		QueryRunner runner = new QueryRunner(C3p0Utils.getDataSource());
		// 寫SQL語句
		String sql = "delete from user where id=?";
		// 調用方法
		int num = runner.update(sql, id);
		if (num > 0)
			return true;
		return false;
	}
}

3、創建測試類

在cn.itcast.jdbc.demo包中創建測試類DBUtilsDaoTest類

代碼如下:

package cn.itcast.jdbc.demo;
import java.sql.SQLException;
import java.util.List;
import cn.itcast.chapter10.example.User;
public class DBUtilsDaoTest1 {
	private static DBUtilsDao dao = new DBUtilsDao();	
	public static void testInsert() throws SQLException {
		User user = new User();
		user.setName("zhaoliu");
		user.setPassword("666666");
		boolean b = dao.insert(user);
		System.out.println("testInsert:"+b);
	}
	
	public static void testupdate() throws SQLException {
		User user = new User();
		user.setName("zhaoqi");
		user.setPassword("666777");
		user.setId(1);
		boolean b = dao.update(user);
		System.out.println("testupdate:"+b);
	}
	
	public static void testdelete() throws SQLException {
		boolean b = dao.delete(4);
		System.out.println("testdelete:"+b);
	}
	
	public static void testfindById() throws SQLException {
		User user = dao.find(2);
		System.out.println(user.getId() + "," + user.getName() + ","
				+ user.getPassword());
	}
	
	public static void testfindAll() throws SQLException {
		List<User> list = dao.findAll();
		for(User user : list) {
		System.out.println(user.getId() + "," + user.getName() + ","
				+ user.getPassword());
		}
	}
	public static void main(String[] args) throws SQLException {
		testInsert();
		testupdate();
		testdelete();
		testfindById();
		testfindAll();
	}
}

以上代碼由多個測試函數組成,依次為:插入、修改、刪除、根據id查詢、查詢所有

4、執行測試類

1.數據表user原始數據如下:

執行後結果如下:

之中插入和刪除都是針對第四個數據進行的操作,所以沒有顯現

Java DBUtils技術訪問數據庫

DBUtils

Dbutils是操作數據庫的組件,對傳統操作數據庫的類進行二次封裝,可以把結果集轉化成List。

介紹

DBUtils相對以往的連接數據庫得到結果集的模式,代碼更加簡潔,訪問更加迅速,這裡我對一個我自級設計的Cuisine表做一個簡單的例子。

Cuisine表

對數據庫的查詢語句的代碼

對已有的菜系表Cuisine查找對應菜系編號cuid的全部數據.

1、菜系表的實體類

// 菜系表的實體類
public class Cuisine {
	private static final long serialVersionUID = 1L;	
	private int cuid;	
	private String cuname;	
	public Cuisine() {
		super();
		// TODO Auto-generated constructor stub
		this.cuid = 0;
		this.cuname = "";
	}
	public Cuisine(int cuid, String cuname) {
		super();
		this.cuid = cuid;
		this.cuname = cuname;
	}
	public int getCuid() {
		return cuid;
	}
	public void setCuid(int cuid) {
		this.cuid = cuid;
	}
	public String getCuname() {
		return cuname;
	}
	public void setCuname(String cuname) {
		this.cuname = cuname;
	}
	@Override
	public String toString() {
		return "Cuisine [cuid=" + cuid + ", cuname=" + cuname + "]";
	}	
}

2、實現數據查詢的方法

// 實現數據查詢
	public Cuisine getCuisine(Cuisine cu) {//得到對應菜系的信息
		// TODO Auto-generated method stub
		QueryRunner queryRunner = new QueryRunner();
		if(cu.getCuid() != 0){
			String sql = "select * from cuisine where cuid = ?";
			try {
				return queryRunner.query(DBUtilsPro.getConnection(),sql,cu.getCuid(),new BeanHandler(Cuisine.class));
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}
		return null;
	}

以上為個人經驗,希望能給大傢一個參考,也希望大傢多多支持WalkonNet。

推薦閱讀: