当前位置:网站首页>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();
}
}


边栏推荐
- Strictly abide by the construction period and ensure the quality, this AI data annotation company has done it!
- Go basic constant definition and use
- Solution: exceptiole 'xxxxx QRTZ_ Locks' doesn't exist and MySQL's my CNF file append lower_ case_ table_ Error message after names startup
- Solution to boost library link error
- vim区间删行注释
- 万物并作,吾以观复|OceanBase 政企行业实践
- Deep analysis of data storage in memory - C language
- What experience is there only one test in the company? Listen to what they say
- 海思调用接口之Makefile配置
- Go project operation method
猜你喜欢

Solution: exceptiole 'xxxxx QRTZ_ Locks' doesn't exist and MySQL's my CNF file append lower_ case_ table_ Error message after names startup

MarkDown基本语法

Implementation of VGA protocol based on FPGA

ADC of stm32

Potplayer set minimized shortcut keys

Load balancing cluster (LBC)

密码技术---分组密码的模式

C#中Linq用法汇集

Jinglianwen technology's low price strategy helps AI enterprises reduce model training costs

Eight honors and eight disgraces of the programmer version~
随机推荐
Go basic constant definition and use
Use the scroll bar of souI when using the real window in souI
富滇银行完成数字化升级|OceanBase数据库助力布局分布式架构中台
Makefile configuration of Hisilicon calling interface
Submit code process
What if win11 can't turn off the sticky key? The sticky key is cancelled but it doesn't work. How to solve it
Temperature measurement and display of 51 single chip microcomputer [simulation]
Win11自动关机设置在哪?Win11设置自动关机的两种方法
RuntimeError: no valid convolution algorithms available in CuDNN
Compose 中的 'ViewPager' 详解 | 开发者说·DTalk
Solution: exceptiole 'xxxxx QRTZ_ Locks' doesn't exist and MySQL's my CNF file append lower_ case_ table_ Error message after names startup
Realize the linkage between bottomnavigationview and navigation
Potplayer set minimized shortcut keys
Connexion à distance de la tarte aux framboises en mode visionneur VNC
简述中台的常识
(毒刺)利用Pystinger Socks4上线不出网主机
FOC矢量控制及BLDC控制中的端电压、相电压、线电压等概念别还傻傻分不清楚
一文掌握基于深度学习的人脸表情识别开发(基于PaddlePaddle)
阿里云有奖体验:如何使用 PolarDB-X
RuntimeError: no valid convolution algorithms available in CuDNN