这篇文章主要讲解了“javaweb如何使用DBUtils实现增删改查”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“JavaWEB如何使用DBUtils实现增删改查”吧!JavaWeb 使用D
这篇文章主要讲解了“javaweb如何使用DBUtils实现增删改查”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“JavaWEB如何使用DBUtils实现增删改查”吧!
创建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目录下,创建一个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;}}
在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查询、查询所有
数据表user原始数据如下:
执行后结果如下:
之中插入和删除都是针对第四个数据进行的操作,所以没有显现
Dbutils是操作数据库的组件,对传统操作数据库的类进行二次封装,可以把结果集转化成List。
DBUtils相对以往的连接数据库得到结果集的模式,代码更加简洁,访问更加迅速,这里我对一个我自级设计的Cuisine表做一个简单的例子。
对已有的菜系表Cuisine查找对应菜系编号cuid的全部数据.
菜系表的实体类
// 菜系表的实体类public class Cuisine {private static final long serialVersionUID = 1L;private int cuid;private String cuname;public Cuisine() {super();// TODO Auto-generated constructor stubthis.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;}@Overridepublic String toString() {return "Cuisine [cuid=" + cuid + ", cuname=" + cuname + "]";}}
实现数据查询的方法
// 实现数据查询public Cuisine getCuisine(Cuisine cu) {//得到对应菜系的信息// TODO Auto-generated method stubQueryRunner 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 blocke1.printStackTrace();}}return null;}
感谢各位的阅读,以上就是“JavaWeb如何使用DBUtils实现增删改查”的内容了,经过本文的学习后,相信大家对JavaWeb如何使用DBUtils实现增删改查这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是编程网,小编将为大家推送更多相关知识点的文章,欢迎关注!
--结束END--
本文标题: JavaWeb如何使用DBUtils实现增删改查
本文链接: https://lsjlt.com/news/301251.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0