当前位置:网站首页>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();
}
}
边栏推荐
- 为什么RTOS系统要使用MPU?
- C MVC creates a view to get rid of the influence of layout
- 内网渗透 | 手把手教你如何进行内网渗透
- Editor Caton
- PotPlayer设置最小化的快捷键
- C#中Linq用法汇集
- Solving ordinary differential equations with MATLAB
- 解决:exceptiole ‘xxxxx.QRTZ_LOCKS‘ doesn‘t exist以及mysql的my.cnf文件追加lower_case_table_names后启动报错
- PHP get real IP
- 面试过了,起薪16k
猜你喜欢
What can I do after buying a domain name?
【Redis笔记】压缩列表(ziplist)
Writing of head and bottom components of non routing components
Strictly abide by the construction period and ensure the quality, this AI data annotation company has done it!
Hisilicon VI access video process
Deep analysis of data storage in memory - C language
(stinger) use pystinger Socks4 to go online and not go out of the network host
RuntimeError: no valid convolution algorithms available in CuDNN
Third party payment function test point [Hangzhou multi tester _ Wang Sir] [Hangzhou multi tester]
Realize the linkage between bottomnavigationview and navigation
随机推荐
ping域名报错unknown host,nslookup/systemd-resolve可以正常解析,ping公网地址通怎么解决?
采用VNC Viewer方式遠程連接樹莓派
程序员版本的八荣八耻~
Pandora IOT development board learning (HAL Library) - Experiment 4 serial port communication experiment (learning notes)
2022年最新最全软件测试面试题大全
What experience is there only one test in the company? Listen to what they say
Load balancing cluster (LBC)
[live broadcast appointment] database obcp certification comprehensive upgrade open class
Alibaba cloud award winning experience: how to use polardb-x
php 获取真实ip
Solving ordinary differential equations with MATLAB
FOC矢量控制及BLDC控制中的端电压、相电压、线电压等概念别还傻傻分不清楚
golang中new与make的区别
Troubleshooting the cause of the crash when STM32 serial port dam receives 253 bytes
Warning: implicitly declaring library function 'printf' with type 'int (const char *,...)‘
Makefile configuration of Hisilicon calling interface
Speech recognition Series 1: speech recognition overview
Brief introduction to common sense of Zhongtai
[adjustment] postgraduate enrollment of Northeast Petroleum University in 2022 (including adjustment)
20220527_ Database process_ Statement retention