JavaWeb 使用DBUtils实现增删改查方式
这篇文章主要介绍了JavaWeb 使用DBUtils实现增删改查方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
目录
JavaWeb 使用DBUtils实现增删改查
1、创建C3p0Utils类
2、创建DBUtilsDao类
3、创建测试类
4、执行测试类
Java DBUtils技术访问数据库
DBUtils
介绍
对数据库的查询语句的代码
JavaWeb 使用DBUtils实现增删改查
1、创建C3p0Utils类
创建cn.itcast.jdbc.utils包
代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 | 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类
代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | 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类
代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | 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查找对应菜系编号cuid的全部数据.
1、菜系表的实体类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | // 菜系表的实体类 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、实现数据查询的方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | // 实现数据查询 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 ; } |
以上为个人经验,希望能给大家一个参考
原文链接:https://blog.csdn.net/qq_40788630/article/details/84723637