创建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.ur;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 = "lect * from ur";// 调用方法list list = (list) runner.query(sql, new beanlisthandler(ur.class));return list;}// 查询单个,返回对象public ur find(int id) throws sqlexception {// 创建queryrunner对象queryrunner runner = new queryrunner(c3p0utils.getdatasource());// 写sql语句string sql = "lect * from ur where id=?";// 调用方法ur ur = (ur) runner.query(sql, new beanhandler(ur.class), new object[] { id });return ur;}// 添加用户的操作public boolean inrt(ur ur) throws sqlexception {// 创建queryrunner对象queryrunner runner = new queryrunner(c3p0utils.getdatasource());// 写sql语句string sql = "inrt into ur (name,password) values (?,?)";// 调用方法int笔记本没有声音 num = runner.update(sql,new object[] { ur.getname(), ur.getpassword() });if (num > 0)return true;return fal;}// 修改用户的操作public boolean update(ur ur) throws sqlexception {// 创建queryrunner对象queryrunner runner = new queryrunner(c3p0utils.getdatasource());// 写sql语句string sql = "update ur t name=?,password=? where id=?";// 调用方法int num = runner.update(sql, new object[] { ur.getname(), ur.getpassword(),ur.getid() });if (num > 0)return true;return fal;}// 删除用户的操作public boolean delete(int id) throws sqlexception {// 创建queryrunner对象queryrunner runner = new queryrunner(c3p0utils.getdatasource());// 写sql语句string sql = "delete from ur where id=?";// 地球的直径是多少调用方法int num = runner.update(sql, id);if (num > 0)return true;return fal;}}
在cn.itcast.jdbc.demo包中创建测试类dbutilsdaotest类
代码如下:
package cn.itcast.jdbc.demo;import java.sql.sqlexception;import java.util.list;import cn.itcast.chapter10.example.ur;public class dbutilsdaotest1 {private static dbutilsdao dao = new dbutilsdao();public static void testinrt() throws sqlexception {ur ur = new ur();ur.tname("zhaoliu");ur.tpassword("666666");boole会呼吸的痛许嵩an b = dao.inrt(ur);system.out.println("testinrt:"+b);}public static void testupdate() throws sqlexception {ur ur = new ur();ur.tname("zhaoqi");ur.tpassword("666777");ur.tid(1);boolean b = dao.update(ur);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 {ur ur = dao.find(2);system.out.println(ur.getid() + "," + ur.getname() + ","+ ur.getpassword());}public static void testfindall() throws sqlexception {list<ur> list = dao.findall();for(ur ur : list) {system.out.println(ur.getid() + "," + ur.getname() + ","+ ur.getpa关于艺术的作文ssword());}}public static void main(string[] args) throws sqlexception {testinrt();testupdate();testdelete();testfindbyid();testfindall();}}
以上代码由多个测试函数组成,依次为:插入、修改、删除、根据id查询、查询所有
1.数据表ur原始数据如下:
执行后结果如下:
之中插入和删除都是针对第四个数据进行的操作,所以没有显现
dbutils是操作数据库的组件,对传统操作数据库的类进行二次封装,可以把结果集转化成list。
dbutils相对以往的连接数据库得到结果集的模式,代码更加简洁,访问更加迅速,这里我对一个我自级设计的cuisine表做一个简单的例子。
对已有的菜系表cuisine查找对应菜系编号cuid的全部数据.
1、菜系表的实体类
// 菜系表的实体类public class cuisine {private static final long rialversionuid = 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;}publ红色的书ic void tcuid(int cuid) {this.cuid = cuid;}public string getcuname() {return cuname;}public void tcuname(string cuname) {this.cuname = cuname;}@overridepublic string tostring() {return "cuisine [cuid=" + cuid + ", cuname=" + cuname + "]";}}
2、实现数据查询的方法
// 实现数据查询public cuisine getcuisine(cuisine cu) {//得到对应菜系的信息// todo auto-generated method stubqueryrunner queryrunner = new queryrunner();if(cu.getcuid() != 0){string sql = "lect * 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;}
以上为个人经验,希望能给大家一个参考,也希望大家多多支持www.887551.com。
本文发布于:2023-04-04 00:36:17,感谢您对本站的认可!
本文链接:https://www.wtabcd.cn/fanwen/zuowen/bf801508a15d33748739b081e1bf52b8.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文word下载地址:JavaWeb 使用DBUtils实现增删改查方式.doc
本文 PDF 下载地址:JavaWeb 使用DBUtils实现增删改查方式.pdf
留言与评论(共有 0 条评论) |