BaseDao封装增删改查

本文介绍了如何通过BaseDao类,利用泛型设计和工具类封装数据库连接管理、增删改操作,以减少代码量并实现数据库操作的简洁与高效。
摘要由CSDN通过智能技术生成

BaseDao的好处:

减少代码量

采用泛型设计

工具类

获取连接和关闭连接

package Books;

import java.sql.*;

public class BooksDB {

    private String driver="com.mysql.jdbc.Driver";
    private String url="jdbc:mysql://localhost:3306/BooksTab";
    private String user="root";
    private String password="root";
    Connection conn=null;

    //    获取数据库链接对象
    public Connection getConn(){
        if(conn==null){
            try {
                Class.forName(driver);
                conn= DriverManager.getConnection(url,user,password);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
        return conn;
    }


    //    关闭数据库链接
    public void closeAll(Connection conn, Statement sta, ResultSet res){
        //关闭conn
        if(conn!=null){
            try {
                conn.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

//        关闭sta
        if(sta!=null){
            try {
                sta.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }


//        关闭res
        if(res!=null){
            try {
                res.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }



    //    增删改操作
    public int excuteUpdate(String sql,Object [] objs){
        int row=0;
//        获取链接
        Connection conn=getConn();
//        编写sql语句
        try {
//            预处理对象
            PreparedStatement pst= conn.prepareStatement(sql);
//            数据注入
            for (int i = 0; i < objs.length ; i++) {
                pst.setObject(i+1,objs[i]);
            }
//            处理数据
            row=pst.executeUpdate();
            closeAll(conn,pst,null);
//            释放资源
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return row;
    }

}

增删改方法

package Books;

public class BooksTest {
    BooksDB book=new BooksDB();
    public int Update(String name, String author, String price, String id){
        String sql= "UPDATE books set name=?,author=?,price=? where id=?";
        Object[] objects={name,author,price,id};
        int row= book.excuteUpdate(sql,objects);
        if(row>0){
            System.out.println("修改成功");
        }
        return row;
    }


    public int del(int id){
        String sql="DELETE from books where id=?";
        Object[] objects={id};
        int row=book.excuteUpdate(sql,objects);
        if(row>0){
            System.out.println("删除成功");
        }
        return row;
    }

    public int add(String name, String author, String price){
        String sql="insert into books(name,author,price)VALUES(?,?,?)";
        Object[] objects={name,author,price};
        int row=book.excuteUpdate(sql,objects);
        if(row>0){
            System.out.println("新增成功");
        }
        return row;
    }

}

实体类

package Books;

public class books {
    private int id;
    private String name;
    private String author;
    private int price;

    public books(int id, String name, String author, int price) {
        this.id = id;
        this.name = name;
        this.author = author;
        this.price = price;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public int getPrice() {
        return price;
    }

    public void setPrice(int price) {
        this.price = price;
    }
}

测试代码

写SQL语句

  String sql= "UPDATE books set name=?,author=?,price=? where id=?";
        Object[] objects={name,author,price,id};
        int row= book.excuteUpdate(sql,objects);
        if(row>0){
            System.out.println("修改成功");
        }
        return row;

数据库

CREATE database BooksTab
use BooksTab

CREATE TABLE Books(
id int AUTO_INCREMENT primary key not null, 
name VARCHAR(20) not null,
author VARCHAR(20) not null,
price int not null
)

insert into books(name,author,price)values
("Java入门到入狱","唐某某",159),("python入门到入土","孙某某",129),
("半小时漫画计算机","刘欣",89),("偷心记忆体","工作室",167)

select * from books
  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值