JDBC基础,进阶版,提取重复代码

3 篇文章 0 订阅
1 篇文章 0 订阅

JDBC进阶版,提取重复代码,包装类

在这里插入图片描述

BookDao代码


import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class BookDao extends BaseDao {
    //查询:
    public List<Book> findAll(){
        List<Book> list = new ArrayList<Book>();
        try {
            getConnection();
            ps = connection.prepareStatement("select * from book");
            set = ps.executeQuery();
            while (set.next()) {
                int id = set.getInt("id");
                String name = set.getString("name");
                Date date = set.getDate("time");
                Book book = new Book(id, name, date);
                list.add(book);
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            closeAll();
        }
        return list;
    }
    public Book find(int fid){
        Book book = null;
        try {
            getConnection();
            ps = connection.prepareStatement("select * from book where id = ?");
            ps.setObject(1,fid);
            set = ps.executeQuery();
            while (set.next()) {
                int id = set.getInt("id");
                String name = set.getString("name");
                Date date = set.getDate("time");
                book = new Book(id, name, date);
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            closeAll();
        }
        return book;
    }
    public int insert(Book book){
        return update("insert into Book values(?,?,?)",book.getId(),book.getName(),book.getDate());
    }
    public int update(Book book){
        return update("update Book set name = ?,time=? where id = ?",book.getName(),book.getName(),book.getDate());
    }
    public int delete(int id){
        return update("delete from book where id = ?",id);
    }
}

BaseDao代码


import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

public class BaseDao {
    public Connection connection = null;
    public PreparedStatement ps = null;
    public ResultSet set = null;

    public static String username = null;
    public static String password = null;
    public static String driverName = null;
    public static String url = null;
//    public static final String url = "jdbc:mysql://localhost:3306/ceshi?serverTimezone = Asia/Shanghai";
    static {
        try {
            InputStream resourceAsStream = BaseDao.class.getResourceAsStream("/db.properties");
            Properties properties = new Properties();
            properties.load(resourceAsStream);

            username = properties.get("username").toString();
            password = properties.get("password").toString();
            driverName = properties.get("driverName").toString();
            url = properties.get("url").toString();

            Class.forName(driverName);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
}
    public void getConnection(){
        try {
            connection = DriverManager.getConnection(url,username,password);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }
    public int update(String sql,Object... params){
        int row = -1;
        try {
            getConnection();
            ps = connection.prepareStatement(sql);
            for (int index = 0; index < params.length; index++) {
                ps.setObject(index + 1, params[index]);
            }
            row = ps.executeUpdate();
        }catch(Exception e){
            e.printStackTrace();
        }finally {
            closeAll();
        }
        return row;
    }
    public void closeAll(){
        if(set!=null){
            try {
                set.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if(ps!=null){
            try {
                ps.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if(connection!=null){
            try {
                connection.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }

    }
}

src中的配置文件properties:

username=root
password=123456
driverName=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/ceshi?serverTimezone=Asia/Shanghai

entity里面的类Book


import java.util.Date;

public class Book {
    private int id;
    private String name;
    private Date date;

    public Book() {
    }

    public Book(int id, String name, Date date) {
        this.id = id;
        this.name = name;
        this.date = date;
    }

    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 Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }

    @Override
    public String toString() {
        return "Book{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", date=" + date +
                '}';
    }
}

test里面的测试类


import java.util.Date;
import java.util.List;

public class test3 {
    public static void main(String[] args) {
        BookDao bookDao = new BookDao();
//        List<Book> list = bookDao.findAll();
//        for (Book book:list){
//            System.out.println(book);
//        }
//        Book book = bookDao.find(2);
//        System.out.println(book);
        //插入
        int i = bookDao.insert(new Book(3,"刘",new Date(1999-11-01)));
        System.out.println(i);
        //更新
//        int i = bookDao.update(new Book(2,"zhang",new Date(2000-01-01)));
//        System.out.println(i);
        //删除
//          int i = bookDao.delete(3);
//          System.out.println(i);
    }

}

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值