当前位置:网站首页>JDBC練習案例
JDBC練習案例
2022-07-02 23:30:00 【pengege666】
需求

環境准備
1.數據庫錶 tb_brand
-- 删除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);
2.在pojo包下實體類 Brand

/** * 品牌 * alt + 鼠標左鍵:整列編輯 * 在實體類中,基本數據類型建議使用其對應的包裝類型 */
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 +
'}';
}
}
JDBC操作
1.查詢所有

/** * Druid數據庫連接池演示 */
public class DruidDemo {
public static void fun() throws Exception {
//1. 獲取Connection
//3. 加載配置文件
Properties prop = new Properties();
prop.load(new FileInputStream("jdbc-demo/src/druid.properties"));
//4. 獲取連接池對象
DataSource dataSource = DruidDataSourceFactory.createDataSource(prop);
//5. 獲取數據庫連接 Connection
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");
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);
//7. 釋放資源
rs.close();
pstmt.close();
conn.close();
}
public static void main(String[] args) throws Exception {
fun();
}
}

2.添加數據
/** * Druid數據庫連接池演示 */
public class DruidDemo {
public static void fun() throws Exception {
// 接收頁面提交的參數
String brandName = "香飄飄";
String companyName = "香飄飄";
int ordered = 1;
String description = "繞地球一圈";
int status = 1;
//1. 獲取Connection
//3. 加載配置文件
Properties prop = new Properties();
prop.load(new FileInputStream("jdbc-demo/src/druid.properties"));
//4. 獲取連接池對象
DataSource dataSource = DruidDataSourceFactory.createDataSource(prop);
//5. 獲取數據庫連接 Connection
Connection conn = dataSource.getConnection();
//2. 定義SQL
String sql = "insert into tb_brand(brand_name, company_name, ordered, description, status) values(?,?,?,?,?);";
//3. 獲取pstmt對象
PreparedStatement pstmt = conn.prepareStatement(sql);
//4. 設置參數
pstmt.setString(1,brandName);
pstmt.setString(2,companyName);
pstmt.setInt(3,ordered);
pstmt.setString(4,description);
pstmt.setInt(5,status);
//5. 執行SQL
int count = pstmt.executeUpdate(); // 影響的行數
//6. 處理結果
System.out.println(count > 0);
//7. 釋放資源
pstmt.close();
conn.close();
}
public static void main(String[] args) throws Exception {
fun();
}

3.修改數據
public class DruidDemo {
public static void fun() throws Exception {
// 接收頁面提交的參數
String brandName = "香飄飄";
String companyName = "香飄飄";
int ordered = 1000;
String description = "繞地球三圈";
int status = 1;
int id = 4;
//1. 獲取Connection
//3. 加載配置文件
Properties prop = new Properties();
prop.load(new FileInputStream("jdbc-demo/src/druid.properties"));
//4. 獲取連接池對象
DataSource dataSource = DruidDataSourceFactory.createDataSource(prop);
//5. 獲取數據庫連接 Connection
Connection conn = dataSource.getConnection();
//2. 定義SQL
String sql = " update tb_brand\n" +
" set brand_name = ?,\n" +
" company_name= ?,\n" +
" ordered = ?,\n" +
" description = ?,\n" +
" status = ?\n" +
" where id = ?";
//3. 獲取pstmt對象
PreparedStatement pstmt = conn.prepareStatement(sql);
//4. 設置參數
pstmt.setString(1,brandName);
pstmt.setString(2,companyName);
pstmt.setInt(3,ordered);
pstmt.setString(4,description);
pstmt.setInt(5,status);
pstmt.setInt(6,id);
//5. 執行SQL
int count = pstmt.executeUpdate(); // 影響的行數
//6. 處理結果
System.out.println(count > 0);
//7. 釋放資源
pstmt.close();
conn.close();
}
public static void main(String[] args) throws Exception {
fun();
}
}

4.删除數據
public class DruidDemo {
public static void fun() throws Exception {
// 接收頁面提交的參數
int id = 4;
//1. 獲取Connection
//3. 加載配置文件
Properties prop = new Properties();
prop.load(new FileInputStream("jdbc-demo/src/druid.properties"));
//4. 獲取連接池對象
DataSource dataSource = DruidDataSourceFactory.createDataSource(prop);
//5. 獲取數據庫連接 Connection
Connection conn = dataSource.getConnection();
//2. 定義SQL
String sql = " delete from tb_brand where id = ?";
//3. 獲取pstmt對象
PreparedStatement pstmt = conn.prepareStatement(sql);
//4. 設置參數
pstmt.setInt(1,id);
//5. 執行SQL
int count = pstmt.executeUpdate(); // 影響的行數
//6. 處理結果
System.out.println(count > 0);
//7. 釋放資源
pstmt.close();
conn.close();
}
public static void main(String[] args) throws Exception {
fun();
}
}


边栏推荐
- ADC of stm32
- php 获取真实ip
- Fusion de la conversion et de la normalisation des lots
- 程序员版本的八荣八耻~
- Getting started with golang: for Range an alternative method of modifying the values of elements in slices
- 理想汽车×OceanBase:当造车新势力遇上数据库新势力
- 购买完域名之后能干什么事儿?
- How does win11 turn on visual control? Win11 method of turning on visual control
- Editor Caton
- Compose 中的 'ViewPager' 详解 | 开发者说·DTalk
猜你喜欢

Go basic data type

跨境电商如何通过打好数据底座,实现低成本稳步增长

Is 408 not fragrant? The number of universities taking the 408 examination this year has basically not increased!

公司里只有一个测试是什么体验?听听他们怎么说吧

Interface switching based on pyqt5 toolbar button -1

Load balancing cluster (LBC)

How can cross-border e-commerce achieve low-cost and steady growth by laying a good data base

(stinger) use pystinger Socks4 to go online and not go out of the network host

Redis expiration policy +conf record

数字图像处理实验目录
随机推荐
第三方支付功能测试点【杭州多测师_王sir】【杭州多测师】
Mapper代理开发
Go basic constant definition and use
【Proteus仿真】51单片机+LCD12864推箱子游戏
聊聊内存模型与内存序
Temperature measurement and display of 51 single chip microcomputer [simulation]
【直播预约】数据库OBCP认证全面升级公开课
Strictly abide by the construction period and ensure the quality, this AI data annotation company has done it!
Submit code process
【Redis笔记】压缩列表(ziplist)
可知论与熟能生巧
Go basic data type
程序员版本的八荣八耻~
Remote connection of raspberry pie by VNC viewer
Boost库链接错误解决方案
Use redis to realize self increment serial number
The use of 8255 interface chip and ADC0809
Application of containerization technology in embedded field
Cryptography -- the mode of block cipher
Getting started with golang: for Range an alternative method of modifying the values of elements in slices