JDBC联系-查询所有

 

 

 

复制代码
package pojo;

/*
*
* 品牌
*
* ALT + 鼠标左键整列选中编辑
* ALT + Shift 可以一个一个自主选中编辑
*
* 在实体类中,基本数据类型建议使用其对应的包装类型
* */
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 +
                '}';
    }
}
复制代码
复制代码
package example;

import com.alibaba.druid.pool.DruidDataSourceFactory;
import org.junit.Test;
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.util.ArrayList;
import java.util.List;
import java.util.Properties;

/*
* 品牌数据的增删改查
*
* */
public class BrandTest {
    /*
    * 查询所有
    * 1.select * from tb_brand;
    * 2.参数:不需要
    * 3.结果:List<Brand>
    * */
    @Test
    public void testBrand() throws Exception {
        //1.导入jar包
        //2.定义配置文件
        //3.加载配置文件
        Properties prop = new Properties();
        //获取数据库的位置信息,加载数据库
        prop.load(new FileInputStream("F:\\Develop\\code\\Demo2\\src\\druid.txt"));
        //4.获取连接池对象
        DataSource dataSource = DruidDataSourceFactory.createDataSource(prop);
        //5.获取数据库连接Connection
        Connection connection = dataSource.getConnection();
        //6.定义SQL
        String sql="select * from tb_brand;";
        //7.获取pstmt对象
        PreparedStatement pstmt = connection.prepareStatement(sql);
        //8.设置参数
        //9.执行sql
        ResultSet rs = pstmt.executeQuery();
        //10.处理结果 List<Brand> 封装Brand对象,装载List集合
        Brand brand=null;
        List<Brand> brands=new ArrayList<>();
        while (rs.next()){
            //获取数据
            int id = rs.getInt("id");
            String brandName = rs.getString("brand_name");
            String companyName = rs.getString("company_name");
            int ordered = rs.getInt("ordered");
            String description = rs.getString("description");
            int status = rs.getInt("status");
            //封装Brand对象
            brand = new Brand();
            brand.setId(id);
            brand.setBrandName(brandName);
            brand.setCompanyName(companyName);
            brand.setOrdered(ordered);
            brand.setDescription(description);
            brand.setStatus(status);
            //装载集合
            brands.add(brand);
        }
        System.out.println(brands);
        //11.释放资源
        rs.close();
        connection.close();
        pstmt.close();
    }
}
复制代码

 

posted @   Karlshell  阅读(10)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 25岁的心里话
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示