JDBC 实现数据库的增删改查

该代码片段展示了如何在Java中使用Druid数据源进行数据库操作,包括查询所有品牌(selectAll)、添加新品牌(add)、更新品牌信息(update)和根据ID删除品牌(deleteById)。每个方法都通过加载druid.properties配置文件获取数据库连接。
摘要由CSDN通过智能技术生成
public class BrandTest {

    /*
    * 查询所有
    * */
    @Test
    public void testSelectAll() throws Exception {
        Properties prop = new Properties();
        System.out.println(System.getProperty("user.dir"));// 显示当前工作目录
        prop.load(new FileInputStream("src/druid.properties"));
        DataSource dataSource = DruidDataSourceFactory.createDataSource(prop);
        Connection conn = dataSource.getConnection();

        String sql = "select * from brand";
        PreparedStatement pstmt = conn.prepareStatement(sql);
        ResultSet rs = pstmt.executeQuery();
        ArrayList<Brand> list = new ArrayList<>();
        Brand brand = null;
        while (rs.next()){
            brand = new Brand();
            brand.setId(rs.getInt("id"));
            brand.setBrandName(rs.getString("brand_name"));
            brand.setCompanyName(rs.getString("company_name"));
            brand.setOrdered(rs.getInt("ordered"));
            brand.setDescription(rs.getString("discription"));
            brand.setStatus(rs.getInt("status"));
            list.add(brand);
        }
        System.out.println(list);
        conn.close();
        rs.close();
        pstmt.close();
    }

    /*
    * 添加
    * */
    @Test
    public void testAdd() throws Exception {

        String brandName = "香飘飘";
        String companyName = "香飘飘公司";
        int ordered = 1;
        String discription = "绕地球一圈!";
        int status = 1;

        Properties prop = new Properties();
        System.out.println(System.getProperty("user.dir"));
        prop.load(new FileInputStream("src/druid.properties"));
        DataSource dataSource = DruidDataSourceFactory.createDataSource(prop);
        Connection conn = dataSource.getConnection();

        String sql = "insert into brand(brand_name, company_name, ordered, discription, status) values(?,?,?,?,?)";
        PreparedStatement pstmt = conn.prepareStatement(sql);
        pstmt.setString(1, brandName);
        pstmt.setString(2, companyName);
        pstmt.setInt(3, ordered);
        pstmt.setString(4, discription);
        pstmt.setInt(5, status);
        int count = pstmt.executeUpdate();
        if (count > 0){
            System.out.println("添加成功!");
        }else {
            System.out.println("添加失败!");
        }
        conn.close();
        pstmt.close();
    }

    /*
    * 修改
    * */
    @Test
    public void testUpdate() throws Exception {

        String brandName = "香飘飘";
        String companyName = "香飘飘公司";
        int ordered = 100;
        String discription = "绕地球一圈!再绕一圈!";
        int status = 1;
        int id = 8;

        Properties prop = new Properties();
        System.out.println(System.getProperty("user.dir"));
        prop.load(new FileInputStream("src/druid.properties"));
        DataSource dataSource = DruidDataSourceFactory.createDataSource(prop);
        Connection conn = dataSource.getConnection();

        String sql = "update brand set brand_name = ?, company_name = ?, ordered = ?, discription = ?, status = ? " +
                "where id = ?";
        PreparedStatement pstmt = conn.prepareStatement(sql);
        pstmt.setString(1, brandName);
        pstmt.setString(2, companyName);
        pstmt.setInt(3, ordered);
        pstmt.setString(4, discription);
        pstmt.setInt(5, status);
        pstmt.setInt(6, id);

        int count = pstmt.executeUpdate();
        if (count > 0){
            System.out.println("修改成功!");
        }else {
            System.out.println("修改失败!");
        }
        conn.close();
        pstmt.close();
    }

    /*
     * 删除
     * */
    @Test
    public void testDeleteById() throws Exception {

        int id = 9;

        Properties prop = new Properties();
        System.out.println(System.getProperty("user.dir"));
        prop.load(new FileInputStream("src/druid.properties"));
        DataSource dataSource = DruidDataSourceFactory.createDataSource(prop);
        Connection conn = dataSource.getConnection();

        String sql = "delete from brand where id = ?";
        PreparedStatement pstmt = conn.prepareStatement(sql);
        pstmt.setInt(1, id);

        int count = pstmt.executeUpdate();
        if (count > 0){
            System.out.println("删除成功!");
        }else {
            System.out.println("删除失败!");
        }
        conn.close();
        pstmt.close();
    }
}

druid.properties

driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql:///test?useSSL=false&useServerPrepStmts=true
username=
password=
initialSize=5
maxActive=10
maxWait=3000

Brand.java

public class Brand  {
    private Integer id;
    private String brandName;
    private String companyName;
    private Integer ordered;
    private String description;
    private Integer status;

    public Integer getId() {
        return id;
    }

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

    public String getBrandName() {
        return brandName;
    }

    public void setBrandName(String brand_name) {
        this.brandName = brand_name;
    }

    public String getCompanyName() {
        return companyName;
    }

    public void setCompanyName(String company_name) {
        this.companyName = company_name;
    }

    public Integer getOrdered() {
        return ordered;
    }

    public void setOrdered(int ordered) {
        this.ordered = ordered;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public Integer getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    @Override
    public String toString() {
        return "Brand{" +
                "id=" + id +
                ", brand_name='" + brandName + '\'' +
                ", company_name='" + companyName + '\'' +
                ", ordered=" + ordered +
                ", description='" + description + '\'' +
                ", status=" + status +
                '}';
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

WuwuwuH_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值