JDBC以及数据库连接池的学习记录(Druid)

 

BrandTest

package example;

import com.alibaba.druid.pool.DruidDataSourceFactory;
import pojo.Brand;

import javax.sql.DataSource;
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

public class BrandTest {

    public static void main(String[] args) throws Exception {

        Properties properties = new Properties();
        properties.load(new FileInputStream("111/src/druid.properties"));

        DataSource dataSource = DruidDataSourceFactory.createDataSource(properties);

        //修改
        String brandName = "德鲁伊";
        String companyName = "zhengys";
        int ordered = 1000;
        String description = "zhengys学习中";
        int status = 1;
        int id = 1;

        String sqlIUpdate = "update tb_brand set " +
                "brand_name = ?," +
                "company_name = ?," +
                "ordered = ?," +
                "description = ?," +
                "status = ? " +
                "where id = ?";
        System.out.println(sqlIUpdate);

        Connection connection1 = dataSource.getConnection();
        PreparedStatement preparedStatement = connection1.prepareStatement(sqlIUpdate);

        preparedStatement.setString(1,brandName);
        preparedStatement.setString(2,companyName);
        preparedStatement.setInt(3,ordered);
        preparedStatement.setString(4,description);
        preparedStatement.setInt(5,status);
        preparedStatement.setInt(6,id);

        int i1 = preparedStatement.executeUpdate();

        System.out.println(i1 > 0);

        preparedStatement.close();
        connection1.close();

        //查询
        Connection connection = dataSource.getConnection();

        Statement statement = connection.createStatement();

        String sqlSelect = "select * from tb_brand";
        ResultSet resultSet = statement.executeQuery(sqlSelect);

        List<Brand> brands = new ArrayList<Brand>();

        while(resultSet.next()) {
            Brand brand = new Brand();
            brand.setId(resultSet.getInt("id"));
            brand.setBrandName(resultSet.getString("brand_name"));
            brand.setCompanyName(resultSet.getString("company_name"));
            brand.setOrdered(resultSet.getInt("ordered"));
            brand.setDescription(resultSet.getString("description"));
            brand.setStatus(resultSet.getInt("status"));
            brands.add(brand);
        }

        resultSet.close();
        statement.close();
        connection.close();

        for(Brand brand:brands){
            System.out.println(brand);
        }

    }
}

Brand

package pojo;

public class Brand {
    // id 主键
    private Integer id;
    // 品牌名称
    private String brandName;
    // 企业名称
    private String companyName;
    // 排序字段
    private Integer ordered;
    // 描述信息
    private String description;
    // 状态:0:禁用  1:启用
    private Integer status;

    public Integer getId() {
        return id;
    }

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

    public String getBrandName() {
        return brandName;
    }

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

    public String getCompanyName() {
        return companyName;
    }

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

    public Integer getOrdered() {
        return ordered;
    }

    public void setOrdered(Integer 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(Integer status) {
        this.status = status;
    }

    @Override
    public String toString() {
        return "Brand{" +
                "id=" + id +
                ", brandName='" + brandName + '\'' +
                ", companyName='" + companyName + '\'' +
                ", ordered=" + ordered +
                ", description='" + description + '\'' +
                ", status=" + status +
                '}';
    }
}

druid.properties

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值