当前位置:网站首页>JDBC practice cases
JDBC practice cases
2022-07-02 23:30:00 【pengege666】
List of articles
demand
Environmental preparation
1. Database table tb_brand
-- Delete tb_brand surface
drop table if exists tb_brand;
-- establish tb_brand surface
create table tb_brand (
-- id Primary key
id int primary key auto_increment,
-- The brand name
brand_name varchar(20),
-- Company name
company_name varchar(20),
-- Sort field
ordered int,
-- Description information
description varchar(100),
-- state :0: Ban 1: Enable
status int
);
-- Add data
insert into tb_brand (brand_name, company_name, ordered, description, status)
values (' The three little squirrels ', ' Three squirrels Co., Ltd ', 5, ' It's delicious but not hot ', 0),
(' Huawei ', ' Huawei Technology Co., Ltd ', 100, ' Huawei is committed to bringing the digital world to everyone 、 Every family 、 Every organization , Building an intelligent world of interconnection of all things ', 1),
(' millet ', ' Xiaomi Technology Co., Ltd ', 50, 'are you ok', 1);
2. stay pojo Package entity classes Brand
/** * brand * alt + Left mouse button : Edit the entire column * In entity classes , For basic data types, it is recommended to use their corresponding packaging types */
public class Brand {
// id Primary key
private Integer id;
// The brand name
private String brandName;
// Company name
private String companyName;
// Sort field
private Integer ordered;
// Description information
private String description;
// state :0: Ban 1: Enable
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 operation
1. Query all
/** * Druid Database connection pool demo */
public class DruidDemo {
public static void fun() throws Exception {
//1. obtain Connection
//3. Load profile
Properties prop = new Properties();
prop.load(new FileInputStream("jdbc-demo/src/druid.properties"));
//4. Get connection pool object
DataSource dataSource = DruidDataSourceFactory.createDataSource(prop);
//5. Get database connection Connection
Connection conn = dataSource.getConnection();
//2. Definition SQL
String sql = "select * from tb_brand;";
//3. obtain pstmt object
PreparedStatement pstmt = conn.prepareStatement(sql);
//4. Set parameters
//5. perform SQL
ResultSet rs = pstmt.executeQuery();
//6. Processing results List<Brand> encapsulation Brand object , load List aggregate
Brand brand = null;
List<Brand> brands = new ArrayList<>();
while (rs.next()){
// get data
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");
// encapsulation Brand object
brand = new Brand();
brand.setId(id);
brand.setBrandName(brandName);
brand.setCompanyName(companyName);
brand.setOrdered(ordered);
brand.setDescription(description);
brand.setStatus(status);
// Load collection
brands.add(brand);
}
System.out.println(brands);
//7. Release resources
rs.close();
pstmt.close();
conn.close();
}
public static void main(String[] args) throws Exception {
fun();
}
}
2. Add data
/** * Druid Database connection pool demo */
public class DruidDemo {
public static void fun() throws Exception {
// Receive the parameters submitted by the page
String brandName = " Fragrant ";
String companyName = " Fragrant ";
int ordered = 1;
String description = " Circle the earth ";
int status = 1;
//1. obtain Connection
//3. Load profile
Properties prop = new Properties();
prop.load(new FileInputStream("jdbc-demo/src/druid.properties"));
//4. Get connection pool object
DataSource dataSource = DruidDataSourceFactory.createDataSource(prop);
//5. Get database connection Connection
Connection conn = dataSource.getConnection();
//2. Definition SQL
String sql = "insert into tb_brand(brand_name, company_name, ordered, description, status) values(?,?,?,?,?);";
//3. obtain pstmt object
PreparedStatement pstmt = conn.prepareStatement(sql);
//4. Set parameters
pstmt.setString(1,brandName);
pstmt.setString(2,companyName);
pstmt.setInt(3,ordered);
pstmt.setString(4,description);
pstmt.setInt(5,status);
//5. perform SQL
int count = pstmt.executeUpdate(); // Number of rows affected
//6. Processing results
System.out.println(count > 0);
//7. Release resources
pstmt.close();
conn.close();
}
public static void main(String[] args) throws Exception {
fun();
}
3. Modifying data
public class DruidDemo {
public static void fun() throws Exception {
// Receive the parameters submitted by the page
String brandName = " Fragrant ";
String companyName = " Fragrant ";
int ordered = 1000;
String description = " Three circles around the earth ";
int status = 1;
int id = 4;
//1. obtain Connection
//3. Load profile
Properties prop = new Properties();
prop.load(new FileInputStream("jdbc-demo/src/druid.properties"));
//4. Get connection pool object
DataSource dataSource = DruidDataSourceFactory.createDataSource(prop);
//5. Get database connection Connection
Connection conn = dataSource.getConnection();
//2. Definition SQL
String sql = " update tb_brand\n" +
" set brand_name = ?,\n" +
" company_name= ?,\n" +
" ordered = ?,\n" +
" description = ?,\n" +
" status = ?\n" +
" where id = ?";
//3. obtain pstmt object
PreparedStatement pstmt = conn.prepareStatement(sql);
//4. Set parameters
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. perform SQL
int count = pstmt.executeUpdate(); // Number of rows affected
//6. Processing results
System.out.println(count > 0);
//7. Release resources
pstmt.close();
conn.close();
}
public static void main(String[] args) throws Exception {
fun();
}
}
4. Delete data
public class DruidDemo {
public static void fun() throws Exception {
// Receive the parameters submitted by the page
int id = 4;
//1. obtain Connection
//3. Load profile
Properties prop = new Properties();
prop.load(new FileInputStream("jdbc-demo/src/druid.properties"));
//4. Get connection pool object
DataSource dataSource = DruidDataSourceFactory.createDataSource(prop);
//5. Get database connection Connection
Connection conn = dataSource.getConnection();
//2. Definition SQL
String sql = " delete from tb_brand where id = ?";
//3. obtain pstmt object
PreparedStatement pstmt = conn.prepareStatement(sql);
//4. Set parameters
pstmt.setInt(1,id);
//5. perform SQL
int count = pstmt.executeUpdate(); // Number of rows affected
//6. Processing results
System.out.println(count > 0);
//7. Release resources
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!
- Pandora IOT development board learning (HAL Library) - Experiment 3 key input experiment (learning notes)
- Getting started with golang: for Range an alternative method of modifying the values of elements in slices
- 富滇银行完成数字化升级|OceanBase数据库助力布局分布式架构中台
- vim区间删行注释
- I've been interviewed. The starting salary is 16K
- 万物并作,吾以观复|OceanBase 政企行业实践
- 潘多拉 IOT 开发板学习(HAL 库)—— 实验3 按键输入实验(学习笔记)
- Speech recognition Series 1: speech recognition overview
- Realize the linkage between bottomnavigationview and navigation
猜你喜欢
PotPlayer设置最小化的快捷键
Deep analysis of data storage in memory - C language
Ideal car × Oceanbase: when the new forces of car building meet the new forces of database
Tiktok actual combat ~ number of likes pop-up box
【ML】李宏毅三:梯度下降&分类(高斯分布)
How does win11 turn on visual control? Win11 method of turning on visual control
跨境电商如何通过打好数据底座,实现低成本稳步增长
基于FPGA的VGA协议实现
Detailed explanation of 'viewpager' in compose | developer said · dtalk
Go project operation method
随机推荐
Where is the win11 microphone test? Win11 method of testing microphone
Interface switching based on pyqt5 toolbar button -1
C# MVC创建一个视图摆脱布局的影响
(毒刺)利用Pystinger Socks4上线不出网主机
密码技术---分组密码的模式
VIM interval deletion note
请求与响应
Catalogue of digital image processing experiments
跨境电商如何通过打好数据底座,实现低成本稳步增长
4 special cases! Schools in area a adopt the re examination score line in area B!
Golang common settings - modify background
[proteus simulation] 51 MCU +lcd12864 push box game
CDN acceleration requires the domain name to be filed first
Cryptographic technology -- key and ssl/tls
How does win11 turn on visual control? Win11 method of turning on visual control
Arduino - character judgment function
What can I do after buying a domain name?
RecyclerView结合ViewBinding的使用
JSON数据传递参数
Use redis to realize self increment serial number