MySql表代码
-- 删除tb_brand表
drop table if exists tb_brand;
-- 创建tb_brand表
create table tb_brand
(-- id 主键id int primary key auto_increment,-- 品牌名称brand_name varchar(20),-- 企业名称company_name varchar(20),-- 排序字段ordered int,-- 描述信息description varchar(100),-- 状态:0:禁用 1:启用status int
);
-- 添加数据
insert into tb_brand (brand_name, company_name, ordered, description, status)
values ('三只松鼠', '三只松鼠股份有限公司', 5, '好吃不上火', 0),('华为', '华为技术有限公司', 100, '华为致力于把数字世界带入每个人、每个家庭、每个组织,构建万物互联的智能世界', 1),('小米', '小米科技有限公司', 50, 'are you ok', 1);SELECT * FROM tb_brand;
package example;import org.gjt.mm.mysql.Driver;
import org.junit.Test;
import pojo.Account;import java.sql.*;
import java.util.ArrayList;
import java.util.List;public class BrandTest {@Testpublic void tsetSelectAll() throws Exception {//查询所有sql//注册链接String url = "jdbc:mysql://127.0.0.1:3306/heimalearnmysql?useSSL=false";String username = "root";String password = "1234";Connection connection = DriverManager.getConnection(url, username, password);//定义sqlString sql = "select * from heimalearnmysql.tb_brand";//获取statement对象Statement statement = connection.createStatement();//执行sqlResultSet resultSet = statement.executeQuery(sql);//集合存储啦List<Account> accounts = new ArrayList<>();//处理结果while (resultSet.next()){Integer id = resultSet.getInt(1);String brand_name = resultSet.getString(2);String company_name=resultSet.getString(3);Integer ordered= resultSet.getInt(4);String description=resultSet.getString(5);Integer status=resultSet.getInt(6);Account account = new Account(id , brand_name , company_name,ordered,description,status);accounts.add(account);}for (Account account : accounts) {System.out.println(account);}}
}
Account 类代码
package pojo;public class Account {// id 主键private Integer id;// 品牌名称private String brand_name;// 企业名称private String company_name;// 排序字段private Integer ordered;// 描述信息private String description;// 状态:0:禁用 1:启用private Integer status;public Account(Integer id, String brand_name, String company_name, Integer ordered, String description, Integer status) {this.id = id;this.brand_name = brand_name;this.company_name = company_name;this.ordered = ordered;this.description = description;this.status = status;}public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getBrand_name() {return brand_name;}public void setBrand_name(String brand_name) {this.brand_name = brand_name;}public String getCompany_name() {return company_name;}public void setCompany_name(String company_name) {this.company_name = company_name;}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;}@Overridepublic String toString() {return "Account{" +"id=" + id +", brand_name='" + brand_name + '\'' +", company_name='" + company_name + '\'' +", ordered=" + ordered +", description='" + description + '\'' +", status=" + status +'}';}
}```