Python 官方文档:入门教程 => 点击学习
目录javaweb 使用DBUtils实现增删改查1、创建C3p0Utils类2、创建DBUtilsDao类3、创建测试类4、执行测试类Java DBUtils技术访问数据库DBUt
创建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查询、查询所有
1.数据表user原始数据如下:
执行后结果如下:
之中插入和删除都是针对第四个数据进行的操作,所以没有显现
Dbutils是操作数据库的组件,对传统操作数据库的类进行二次封装,可以把结果集转化成List。
DBUtils相对以往的连接数据库得到结果集的模式,代码更加简洁,访问更加迅速,这里我对一个我自级设计的Cuisine表做一个简单的例子。
对已有的菜系表Cuisine查找对应菜系编号cuid的全部数据.
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、实现数据查询的方法
// 实现数据查询
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;
}
以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。
--结束END--
本文标题: JavaWeb 使用DBUtils实现增删改查方式
本文链接: https://lsjlt.com/news/159141.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0