使用DAO(Data Access Object)数据访问对象设计,使用jdbc完成对数据库的访问,使用junit完成测试

 
        
            org.projectlombok
            lombok
            1.18.10
            compile
        
        
            junit
            junit
            4.12
            test
        
        
            mysql
            mysql-connector-java
            8.0.28
            compile
        
 

package com.csdn.fruit.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Serializable;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Fruit implements Serializable {
    private Integer fid;
    private String fname;
    private Integer price;
    private Integer fcount;
    private String remark;

    public Fruit(String fname, Integer price, Integer fcount, String remark) {
        this.fname = fname;
        this.price = price;
        this.fcount = fcount;
        this.remark = remark;
    }
    @Override
    public String toString() {
        return fname + "\t\t" + price + "\t\t" + fcount + "\t\t" + remark;
    }
}
package com.csdn.fruit.dao;
import com.csdn.fruit.pojo.Fruit;
import java.util.List;
//dao :Data Access Object 数据访问对象
//接口设计
public interface FruitDao {

    String URL = "jdbc:mysql:///fruitdb";
    String USER = "root";
    String PASSWORD = "123456";

    void addFruit(Fruit fruit);

    void delFruit(String fname);

    void updateFruit(Fruit fruit);

    List getFruitList();

    Fruit getFruitByFname(String fname);
}
package com.csdn.fruit.dao.impl;
import com.csdn.fruit.dao.FruitDao;
import com.csdn.fruit.pojo.Fruit;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class FruitDaoImpl implements FruitDao {
    @Override
    public void addFruit(Fruit fruit) {
        PreparedStatement psmt = null;
        Connection conn = null;
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            conn = DriverManager.getConnection(URL, USER, PASSWORD);

            String sql = "insert into t_fruit values (0,?,?,?,?)";

            psmt = conn.prepareStatement(sql);

            psmt.setString(1, fruit.getFname());
            psmt.setInt(2, fruit.getPrice());
            psmt.setInt(3, fruit.getFcount());
            psmt.setString(4, fruit.getRemark());

            psmt.executeUpdate();

        } catch (ClassNotFoundException | SQLException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                if (psmt!=null) {
                    psmt.close();
                }
                if (conn!=null&&!conn.isClosed()) {
                    conn.close();
                }
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
    }
    @Override
    public void delFruit(String fname) {
        PreparedStatement psmt = null;
        Connection conn = null;
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            conn = DriverManager.getConnection(URL, USER, PASSWORD);

            String sql = "delete from t_fruit where fname=?";

            psmt = conn.prepareStatement(sql);

            psmt.setString(1, fname);

            psmt.executeUpdate();

        } catch (ClassNotFoundException | SQLException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                if (psmt!=null) {
                    psmt.close();
                }
                if (conn!=null&&!conn.isClosed()) {
                    conn.close();
                }
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
    }
    @Override
    public void updateFruit(Fruit fruit) {
        PreparedStatement psmt = null;
        Connection conn = null;
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            conn = DriverManager.getConnection(URL, USER, PASSWORD);

            String sql = "update  t_fruit set fcount=? where fname=?";

            psmt = conn.prepareStatement(sql);

            psmt.setInt(1,fruit.getFcount());
            psmt.setString(2,fruit.getFname());

            psmt.executeUpdate();

        } catch (ClassNotFoundException | SQLException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                if (psmt!=null) {
                    psmt.close();
                }
                if (conn!=null&&!conn.isClosed()) {
                    conn.close();
                }
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
    }
    @Override
    public List getFruitList() {
        List fruitList = new ArrayList<>();
        PreparedStatement psmt = null;
        Connection conn = null;
        ResultSet rs = null;
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            conn = DriverManager.getConnection(URL, USER, PASSWORD);

            String sql = "select * from t_fruit";

            psmt = conn.prepareStatement(sql);

            rs = psmt.executeQuery();

            while (rs.next()) {
                Integer fid = rs.getInt(1);
                String fname = rs.getString(2);
                Integer price = rs.getInt("price");
                Integer fcount = rs.getInt("fcount");
                String remark = rs.getString("remark");

                Fruit fruit = new Fruit(fid, fname, price, fcount, remark);

                fruitList.add(fruit);
            }
            return fruitList;
        } catch (ClassNotFoundException | SQLException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                if (rs!=null) {
                    rs.close();
                }
                if (psmt!=null) {
                    psmt.close();
                }
                if (conn!=null&&!conn.isClosed()) {
                    conn.close();
                }
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
    }
    @Override
    public Fruit getFruitByFname(String fname) {
        Connection conn = null;
        PreparedStatement psmt = null;
        ResultSet rs = null;
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            conn = DriverManager.getConnection(URL, USER, PASSWORD);

            String sql = "select * from t_fruit where fname like ?";

            psmt = conn.prepareStatement(sql);

            psmt.setString(1, fname);

            rs = psmt.executeQuery();

            if (rs.next()) {
                int fid = rs.getInt(1);
                int price = rs.getInt(3);
                int fcount = rs.getInt("fcount");
                String remark = rs.getString("remark");

                return new Fruit(fid, fname, price, fcount, remark);
            }
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (rs!=null) {
                    rs.close();
                }
                if (psmt!=null) {
                    psmt.close();
                }
                if (conn!=null&&!conn.isClosed()) {
                    conn.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return null;
    }
}
package com.csdn.dao.impl;
import com.csdn.fruit.dao.FruitDao;
import com.csdn.fruit.dao.impl.FruitDaoImpl;
import com.csdn.fruit.pojo.Fruit;
import org.junit.Test;
import java.util.List;
public class FruitDaoImplTest {

    private FruitDao fruitDao = new FruitDaoImpl();

    @Test
    public void testAddFruit() {
        Fruit fruit = new Fruit("哈密瓜", 7, 77, "波罗蜜是一种神奇的水果!");
        fruitDao.addFruit(fruit);
    }

    @Test
    public void testDelFruit() {
        fruitDao.delFruit("哈密瓜");
    }

    @Test
    public void testUpdateFruit() {
        Fruit fruit = new Fruit("波罗蜜", 5, 1000, "好吃");
        fruitDao.updateFruit(fruit);
    }

    @Test
    public void testGetFruitList() {
        List fruitList = fruitDao.getFruitList();
        fruitList.forEach(System.out::println);
    }

    @Test
    public void testGetFruitByFname() {
        Fruit fruit = fruitDao.getFruitByFname("波罗蜜");
        System.out.println(fruit);
    }
}

你可能感兴趣的:(#,JDBC,1024程序员节,java,jdbc,DAO,反射)