最美情侣中文字幕电影,在线麻豆精品传媒,在线网站高清黄,久久黄色视频

歡迎光臨散文網(wǎng) 會員登陸 & 注冊

添加

2023-04-07 18:50 作者:終極向晚葵  | 我要投稿

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();
? ?}
}

testSelectAll()運行結(jié)果
testAdd()運行結(jié)果


添加的評論 (共 條)

分享到微博請遵守國家法律
叶城县| 金坛市| 密山市| 依安县| 厦门市| 元朗区| 图木舒克市| 油尖旺区| 尉犁县| 肥西县| 黑山县| 景谷| 鱼台县| 专栏| 泽州县| 北川| 麟游县| 天全县| 凤山县| 镇坪县| 望城县| 宣汉县| 东源县| 婺源县| 云和县| 察哈| 福安市| 特克斯县| 新田县| 石台县| 冕宁县| 皮山县| 呈贡县| 临夏市| 宁德市| 忻州市| 黄骅市| 甘洛县| 南宫市| 株洲县| 固阳县|