当前位置:网站首页>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();
}
}
边栏推荐
- [proteus simulation] 51 MCU +lcd12864 push box game
- Third party payment function test point [Hangzhou multi tester _ Wang Sir] [Hangzhou multi tester]
- 简述中台的常识
- "A good programmer is worth five ordinary programmers!"
- PotPlayer设置最小化的快捷键
- 基于FPGA的VGA协议实现
- Sword finger offer II 099 Sum of minimum paths - double hundred code
- [ml] Li Hongyi III: gradient descent & Classification (Gaussian distribution)
- php 获取真实ip
- I've been interviewed. The starting salary is 16K
猜你喜欢
ADC of stm32
[adjustment] postgraduate enrollment of Northeast Petroleum University in 2022 (including adjustment)
基于Pyqt5工具栏按钮可实现界面切换-2
公司里只有一个测试是什么体验?听听他们怎么说吧
RecyclerView结合ViewBinding的使用
Third party payment function test point [Hangzhou multi tester _ Wang Sir] [Hangzhou multi tester]
Explain promise usage in detail
Strictly abide by the construction period and ensure the quality, this AI data annotation company has done it!
Win11如何开启目视控制?Win11开启目视控制的方法
潘多拉 IOT 开发板学习(HAL 库)—— 实验4 串口通讯实验(学习笔记)
随机推荐
购买完域名之后能干什么事儿?
基于Pyqt5工具栏按钮可实现界面切换-1
Simple square wave generating circuit [51 single chip microcomputer and 8253a]
Convolution和Batch normalization的融合
FOC矢量控制及BLDC控制中的端电压、相电压、线电压等概念别还傻傻分不清楚
Bean加载控制
Win11如何开启目视控制?Win11开启目视控制的方法
潘多拉 IOT 开发板学习(HAL 库)—— 实验3 按键输入实验(学习笔记)
What can I do after buying a domain name?
Pandora IOT development board learning (HAL Library) - Experiment 3 key input experiment (learning notes)
RuntimeError: no valid convolution algorithms available in CuDNN
SharedPreferences save list < bean > to local and solve com google. gson. internal. Linkedtreemap cannot be cast to exception
CDN 加速,需要域名先备案
Yolox enhanced feature extraction network panet analysis
PHP get real IP
Implementation of VGA protocol based on FPGA
Highly available cluster (HAC)
内网渗透 | 手把手教你如何进行内网渗透
Alibaba cloud award winning experience: how to use polardb-x
【ML】李宏毅三:梯度下降&分类(高斯分布)