JavaWeb 使用DBUtils實現增刪改查
阿新 • • 發佈:2018-12-03
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原始資料如下:
執行後結果如下:
之中插入和刪除都是針對第四個資料進行的操作,所以沒有顯現