查询所有数据
- 获取Connection
- 定义SQL:selection *from tb_brand;
- 获取PerparedStatement对象
- 设置参数:不需要
- 执行SQL
- 处理结果:List<Brand>
- 释放资源
没有系统学过这些,只会套用框架,所以这次我要从涛开始学习,系统学习javaweb,并保证不落下安卓应用开发。
其实也挺简单的,高懂得话就需要一点一点来,全在代码里
package Dutils; import Bean.Brand; import com.alibaba.druid.pool.DruidDataSourceFactory; import javax.sql.DataSource; import java.io.FileInputStream; import java.io.FileNotFoundException; 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 text { /** * 查询所有 * 1.sql: select *from tb_brand; * 2.参数设置 * 3.结果:List<brand> */ public void TextSelectall() throws Exception { //1.获取connect,调用druid连接池 Properties prop = new Properties(); prop.load(new FileInputStream("src/druid.properties")); DataSource dataSource = DruidDataSourceFactory.createDataSource(prop); Connection conn = dataSource.getConnection(); //2.定义sql语句 String sql = "select *from tb_brand"; //3.获取pstmt对象 PreparedStatement pstmt = conn.prepareStatement(sql); //4.设置参数 //查询全部内容不需要设置参数 //5.执行SQL ResultSet rs = pstmt.executeQuery(); //6.处理结果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"); String Ordered = rs.getString("ordered"); String Description = rs.getString("description"); int Status = rs.getInt("status"); //封装Brand brand = new Brand(); brand.setId(Id); brand.setBrand_name(BrandName); brand.setCompany_name(CompanyName); brand.setOrdered(Ordered); brand.setDescription(Description); brand.setStatus(Status); //装载集合 brands.add(brand); } System.out.println(brands); //7.释放资源 rs.close(); pstmt.close(); conn.close(); } }
package Bean; public class Brand { private int id; private String brand_name; private String company_name; private String ordered; private String description; private int status; public Brand() { } public Brand(int id, String brand_name, String company_name, String ordered, String description, int status) { this.id = id; this.brand_name = brand_name; this.company_name = company_name; this.ordered = ordered; this.description = description; this.status = status; } /** * 获取 * @return id */ public int getId() { return id; } /** * 设置 * @param id */ public void setId(int id) { this.id = id; } /** * 获取 * @return brand_name */ public String getBrand_name() { return brand_name; } /** * 设置 * @param brand_name */ public void setBrand_name(String brand_name) { this.brand_name = brand_name; } /** * 获取 * @return company_name */ public String getCompany_name() { return company_name; } /** * 设置 * @param company_name */ public void setCompany_name(String company_name) { this.company_name = company_name; } /** * 获取 * @return ordered */ public String getOrdered() { return ordered; } /** * 设置 * @param ordered */ public void setOrdered(String ordered) { this.ordered = ordered; } /** * 获取 * @return description */ public String getDescription() { return description; } /** * 设置 * @param description */ public void setDescription(String description) { this.description = description; } /** * 获取 * @return status */ public int getStatus() { return status; } /** * 设置 * @param status */ public void setStatus(int status) { this.status = status; } public String toString() { return "Bean{id = " + id + ", brand_name = " + brand_name + ", company_name = " + company_name + ", ordered = " + ordered + ", description = " + description + ", status = " + status + "}"; } }
标签:JDBC,return,String,brand,练习,查询,ordered,public,name From: https://www.cnblogs.com/yzx-sir/p/17162091.html