添加
package com.itheima.example;
import com.alibaba.druid.pool.DruidDataSourceFactory;
import com.itheima.pojo.Brand;
import org.junit.Test;
import javax.sql.DataSource;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
/*
*
* 品牌數(shù)據(jù)的增刪改查操作
* ?*/
public class BrandTest {
/* ?* 1.查詢所有
? * 2.參數(shù);不需要
? * 3.結(jié)果;List<Brand>
? * */
? ?@Test
? ?public void testSelectAll() throws Exception {
? ? ? ?//1.獲取連接的Connection對象
? ? ? ?//3.加載配置文件
? ? ? ?Properties prop =new Properties();
? ? ? ?prop.load(new FileInputStream("src/druid.properties"));//注意去掉“jdbc_demo/”
? ? ? ?//4.獲取連接池對象
? ? ? ?DataSource dataSource = DruidDataSourceFactory.createDataSource(prop);
? ? ? ?//5.獲取數(shù)據(jù)庫連接
? ? ? ?Connection conn = dataSource.getConnection();
? ? ? ?//定義sql
? ? ? ?String sql = "select * from tb_brand";
? ? ? ?//獲取pstmt對象
? ? ? ?PreparedStatement pstmt = conn.prepareStatement(sql);
? ? ? ?//設(shè)置參數(shù)
? ? ? ?//執(zhí)行SQL
? ? ? ?ResultSet rs = pstmt.executeQuery();
? ? ? ?//處理結(jié)果List<brand>封裝Brand對象,裝載List集合
? ? ? ?Brand brand =null;
? ? ? ?List<Brand>brands = new ArrayList<>();
? ? ? ?while(rs.next()){
? ? ? ? ? ?//獲取數(shù)據(jù)
? ? ? ? ? ?int id = rs.getInt("id");//alt+enter
? ? ? ? ? ?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.setBranName(brandName);
? ? ? ? ? ?brand.setOrdered(ordered);
? ? ? ? ? ?brand.getCompanyName(companyName);
? ? ? ? ? ?brand.setStatus(status);
? ? ? ? ? ?brand.setDescription(description);
? ? ? ? ? ?//裝載集合
? ? ? ? ? ?brands.add(brand);
? ? ? ?}
? ? ? ?System.out.println(brands);
? ? ? ?//7.釋放資源
? ? ? ?rs.close();
? ? ? ?pstmt.close();
? ? ? ?conn.close();
? ?}
? ?/*添加
? ?*1.sql:select * from tb-brand
? ?*2.參數(shù);不需要
? ?* 3.結(jié)果List<Brand>
? ?* */
? ?@Test
? ?public void testAdd() throws Exception {
? ? ? ?//1.獲取連接的Connection對象
? ? ? ?//3.加載配置文件
? ? ? ?String brandName ="香飄飄";
? ? ? ?String companyName ="香飄飄";
? ? ? ?int ordered =1;
? ? ? ?String description ="繞地球一圈";
? ? ? ?int status =1;
? ? ? ?Properties prop =new Properties();
? ? ? ?prop.load(new FileInputStream("src/druid.properties"));//注意去掉“jdbc_demo/”
? ? ? ?//4.獲取連接池對象
? ? ? ?DataSource dataSource = DruidDataSourceFactory.createDataSource(prop);
? ? ? ?//5.獲取數(shù)據(jù)庫連接
? ? ? ?Connection conn = dataSource.getConnection();
? ? ? ?//定義sql
? ? ? ?String sql = "insert into tb_brand(brand_name,company_name,ordered,description,status)values(?,?,?,?,?)";
? ? ? ?//獲取pstmt對象
? ? ? ?PreparedStatement pstmt = conn.prepareStatement(sql);
? ? ? ?//設(shè)置參數(shù)
? ? ? ?pstmt.setString(1,brandName);
? ? ? ?pstmt.setString(2,companyName);
? ? ? ?pstmt.setInt(3,ordered);
? ? ? ?pstmt.setString(4,description);
? ? ? ?pstmt.setInt(5,status);
? ? ? ?//執(zhí)行SQL
? ? ? ?int count = pstmt.executeUpdate();//影響行數(shù)
? ? ? ?//6.處理結(jié)果
? ? ? ?System.out.println(count>0);
? ? ? ?//7.釋放資源
? ? ? ?pstmt.close();
? ? ? ?conn.close();
? ?}
}

