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


边栏推荐
- 【直播预约】数据库OBCP认证全面升级公开课
- 【Proteus仿真】51单片机+LCD12864推箱子游戏
- Detailed explanation of 'viewpager' in compose | developer said · dtalk
- Redis expiration policy +conf record
- 【ML】李宏毅三:梯度下降&分类(高斯分布)
- Eight bit responder [51 single chip microcomputer]
- Three solutions to frequent sticking and no response of explorer in win11 system
- I've been interviewed. The starting salary is 16K
- Use redis to realize self increment serial number
- Use of recyclerview with viewbinding
猜你喜欢

Bean加载控制

Potplayer set minimized shortcut keys

Go basic anonymous variable

Speech recognition Series 1: speech recognition overview

采用VNC Viewer方式遠程連接樹莓派

Mapper代理开发

Yolox enhanced feature extraction network panet analysis
![[ml] Li Hongyi III: gradient descent & Classification (Gaussian distribution)](/img/75/3f6203410dd2754e578e0baaaa9aef.png)
[ml] Li Hongyi III: gradient descent & Classification (Gaussian distribution)

海思 VI接入视频流程

“一个优秀程序员可抵五个普通程序员!”
随机推荐
简述中台的常识
Bean加载控制
C MVC creates a view to get rid of the influence of layout
@BindsInstance在Dagger2中怎么使用
Solution to boost library link error
Win11系统explorer频繁卡死无响应的三种解决方法
Potplayer set minimized shortcut keys
vim区间删行注释
Print out mode of go
How can cross-border e-commerce achieve low-cost and steady growth by laying a good data base
Win11麦克风测试在哪里?Win11测试麦克风的方法
Golang common settings - modify background
Go basic data type
详解Promise使用
抖音实战~点赞数量弹框
Pandora IOT development board learning (HAL Library) - Experiment 3 key input experiment (learning notes)
第三方支付功能测试点【杭州多测师_王sir】【杭州多测师】
2022 latest and complete interview questions for software testing
golang中new与make的区别
Redis expiration policy +conf record