当前位置:网站首页>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();
}
}
边栏推荐
- Tiktok actual combat ~ number of likes pop-up box
- Explain promise usage in detail
- BBR encounters cubic
- (毒刺)利用Pystinger Socks4上线不出网主机
- Simple square wave generating circuit [51 single chip microcomputer and 8253a]
- Data set - fault diagnosis: various data and data description of bearings of Western Reserve University
- VIM interval deletion note
- 非路由组件之头部组件和底部组件书写
- 2022 latest and complete interview questions for software testing
- 20220527_ Database process_ Statement retention
猜你喜欢
一文掌握基于深度学习的人脸表情识别开发(基于PaddlePaddle)
2022年最新最全软件测试面试题大全
Third party payment function test point [Hangzhou multi tester _ Wang Sir] [Hangzhou multi tester]
What can I do after buying a domain name?
Alibaba cloud award winning experience: how to use polardb-x
[analysis of STL source code] imitation function (to be supplemented)
(毒刺)利用Pystinger Socks4上线不出网主机
Print out mode of go
Highly available cluster (HAC)
聊聊内存模型与内存序
随机推荐
一文掌握基于深度学习的人脸表情识别开发(基于PaddlePaddle)
All things work together, and I will review oceanbase's practice in government and enterprise industry
Speech recognition Series 1: speech recognition overview
可知论与熟能生巧
SQL advanced syntax
Tiktok actual combat ~ number of likes pop-up box
Intranet penetration | teach you how to conduct intranet penetration hand in hand
Compose 中的 'ViewPager' 详解 | 开发者说·DTalk
非路由组件之头部组件和底部组件书写
"A good programmer is worth five ordinary programmers!"
Prometheus deployment
Connexion à distance de la tarte aux framboises en mode visionneur VNC
Solving ordinary differential equations with MATLAB
PotPlayer设置最小化的快捷键
ping域名报错unknown host,nslookup/systemd-resolve可以正常解析,ping公网地址通怎么解决?
Explain promise usage in detail
Load balancing cluster (LBC)
@How to use bindsinstance in dagger2
【STL源码剖析】仿函数(待补充)
Golang common settings - modify background