您好,登錄后才能下訂單哦!
這篇文章主要講解了“JavaWeb如何使用DBUtils實現(xiàn)增刪改查”,文中的講解內(nèi)容簡單清晰,易于學(xué)習(xí)與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“JavaWeb如何使用DBUtils實現(xiàn)增刪改查”吧!
創(chuàng)建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; } }
在src目錄下,創(chuàng)建一個cn.itcast.jdbc.demo的包,在該包下創(chuàng)建一個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 { // 創(chuàng)建QueryRunner對象 QueryRunner runner = new QueryRunner(C3p0Utils.getDataSource()); // 寫SQL語句 String sql = "select * from user"; // 調(diào)用方法 List list = (List) runner.query(sql, new BeanListHandler(User.class)); return list; } // 查詢單個,返回對象 public User find(int id) throws SQLException { // 創(chuàng)建QueryRunner對象 QueryRunner runner = new QueryRunner(C3p0Utils.getDataSource()); // 寫SQL語句 String sql = "select * from user where id=?"; // 調(diào)用方法 User user = (User) runner.query(sql, new BeanHandler(User.class), new Object[] { id }); return user; } // 添加用戶的操作 public Boolean insert(User user) throws SQLException { // 創(chuàng)建QueryRunner對象 QueryRunner runner = new QueryRunner(C3p0Utils.getDataSource()); // 寫SQL語句 String sql = "insert into user (name,password) values (?,?)"; // 調(diào)用方法 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 { // 創(chuàng)建QueryRunner對象 QueryRunner runner = new QueryRunner(C3p0Utils.getDataSource()); // 寫SQL語句 String sql = "update user set name=?,password=? where id=?"; // 調(diào)用方法 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 { // 創(chuàng)建QueryRunner對象 QueryRunner runner = new QueryRunner(C3p0Utils.getDataSource()); // 寫SQL語句 String sql = "delete from user where id=?"; // 調(diào)用方法 int num = runner.update(sql, id); if (num > 0) return true; return false; } }
在cn.itcast.jdbc.demo包中創(chuàng)建測試類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(); } }
以上代碼由多個測試函數(shù)組成,依次為:插入、修改、刪除、根據(jù)id查詢、查詢所有
1.數(shù)據(jù)表user原始數(shù)據(jù)如下:
執(zhí)行后結(jié)果如下:
之中插入和刪除都是針對第四個數(shù)據(jù)進行的操作,所以沒有顯現(xiàn)
Dbutils是操作數(shù)據(jù)庫的組件,對傳統(tǒng)操作數(shù)據(jù)庫的類進行二次封裝,可以把結(jié)果集轉(zhuǎn)化成List。
DBUtils相對以往的連接數(shù)據(jù)庫得到結(jié)果集的模式,代碼更加簡潔,訪問更加迅速,這里我對一個我自級設(shè)計的Cuisine表做一個簡單的例子。
對已有的菜系表Cuisine查找對應(yīng)菜系編號cuid的全部數(shù)據(jù).
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、實現(xiàn)數(shù)據(jù)查詢的方法
// 實現(xiàn)數(shù)據(jù)查詢 public Cuisine getCuisine(Cuisine cu) {//得到對應(yīng)菜系的信息 // 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; }
感謝各位的閱讀,以上就是“JavaWeb如何使用DBUtils實現(xiàn)增刪改查”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對JavaWeb如何使用DBUtils實現(xiàn)增刪改查這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關(guān)知識點的文章,歡迎關(guān)注!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。