当前位置:网站首页>JDBC Exercise case
JDBC Exercise case
2022-07-02 23:30:00 【Pendege666】
Catalogue des articles
Besoins

Préparation environnementale
1.Table de base de données tb_brand
-- Supprimertb_brandTableau
drop table if exists tb_brand;
-- Créationtb_brandTableau
create table tb_brand (
-- id Clé primaire
id int primary key auto_increment,
-- Marque nominative
brand_name varchar(20),
-- Nom de l'entreprise
company_name varchar(20),
-- Trier les champs
ordered int,
-- Informations descriptives
description varchar(100),
-- Statut:0:Désactiver 1:Activer
status int
);
-- Ajouter des données
insert into tb_brand (brand_name, company_name, ordered, description, status)
values ('Trois écureuils.', 'Trois écureuils GmbH', 5, 'C'est bon.', 0),
('Huawei', 'Huawei Technology Co., Ltd.', 100, 'Huawei s'engage à apporter le monde numérique à tout le monde、Par famille、Par organisation,Construire un monde intelligent où tout est connecté', 1),
('Millet', 'Millet Technology Co., Ltd.', 50, 'are you ok', 1);
2.InpojoClasse d'entité sous emballage Brand

/** * Marque (s) * alt + Bouton gauche de la souris:Édition de colonnes entières * Dans une classe d'entité,Type de données de base il est recommandé d'utiliser son type d'emballage correspondant */
public class Brand {
// id Clé primaire
private Integer id;
// Marque nominative
private String brandName;
// Nom de l'entreprise
private String companyName;
// Trier les champs
private Integer ordered;
// Informations descriptives
private String description;
// Statut:0:Désactiver 1:Activer
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 +
'}';
}
}
JDBCFonctionnement
1.Rechercher tout

/** * DruidDémonstration du pool de connexion à la base de données */
public class DruidDemo {
public static void fun() throws Exception {
//1. AccèsConnection
//3. Charger le profil
Properties prop = new Properties();
prop.load(new FileInputStream("jdbc-demo/src/druid.properties"));
//4. Obtenir l'objet du pool de connexion
DataSource dataSource = DruidDataSourceFactory.createDataSource(prop);
//5. Obtenir une connexion à la base de données Connection
Connection conn = dataSource.getConnection();
//2. DéfinitionSQL
String sql = "select * from tb_brand;";
//3. AccèspstmtObjet
PreparedStatement pstmt = conn.prepareStatement(sql);
//4. Définir les paramètres
//5. Mise en œuvreSQL
ResultSet rs = pstmt.executeQuery();
//6. Résultats du traitement List<Brand> EncapsulationBrandObjet,ChargementListEnsemble
Brand brand = null;
List<Brand> brands = new ArrayList<>();
while (rs.next()){
//Obtenir des données
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");
//EncapsulationBrandObjet
brand = new Brand();
brand.setId(id);
brand.setBrandName(brandName);
brand.setCompanyName(companyName);
brand.setOrdered(ordered);
brand.setDescription(description);
brand.setStatus(status);
//Charger la collection
brands.add(brand);
}
System.out.println(brands);
//7. Libérer des ressources
rs.close();
pstmt.close();
conn.close();
}
public static void main(String[] args) throws Exception {
fun();
}
}

2.Ajouter des données
/** * DruidDémonstration du pool de connexion à la base de données */
public class DruidDemo {
public static void fun() throws Exception {
// Recevoir les paramètres soumis par la page
String brandName = "Ça sent bon.";
String companyName = "Ça sent bon.";
int ordered = 1;
String description = "Un tour autour de la terre";
int status = 1;
//1. AccèsConnection
//3. Charger le profil
Properties prop = new Properties();
prop.load(new FileInputStream("jdbc-demo/src/druid.properties"));
//4. Obtenir l'objet du pool de connexion
DataSource dataSource = DruidDataSourceFactory.createDataSource(prop);
//5. Obtenir une connexion à la base de données Connection
Connection conn = dataSource.getConnection();
//2. DéfinitionSQL
String sql = "insert into tb_brand(brand_name, company_name, ordered, description, status) values(?,?,?,?,?);";
//3. AccèspstmtObjet
PreparedStatement pstmt = conn.prepareStatement(sql);
//4. Définir les paramètres
pstmt.setString(1,brandName);
pstmt.setString(2,companyName);
pstmt.setInt(3,ordered);
pstmt.setString(4,description);
pstmt.setInt(5,status);
//5. Mise en œuvreSQL
int count = pstmt.executeUpdate(); // Nombre de lignes affectées
//6. Résultats du traitement
System.out.println(count > 0);
//7. Libérer des ressources
pstmt.close();
conn.close();
}
public static void main(String[] args) throws Exception {
fun();
}

3.Modifier les données
public class DruidDemo {
public static void fun() throws Exception {
// Recevoir les paramètres soumis par la page
String brandName = "Ça sent bon.";
String companyName = "Ça sent bon.";
int ordered = 1000;
String description = "Trois cercles autour de la terre";
int status = 1;
int id = 4;
//1. AccèsConnection
//3. Charger le profil
Properties prop = new Properties();
prop.load(new FileInputStream("jdbc-demo/src/druid.properties"));
//4. Obtenir l'objet du pool de connexion
DataSource dataSource = DruidDataSourceFactory.createDataSource(prop);
//5. Obtenir une connexion à la base de données Connection
Connection conn = dataSource.getConnection();
//2. DéfinitionSQL
String sql = " update tb_brand\n" +
" set brand_name = ?,\n" +
" company_name= ?,\n" +
" ordered = ?,\n" +
" description = ?,\n" +
" status = ?\n" +
" where id = ?";
//3. AccèspstmtObjet
PreparedStatement pstmt = conn.prepareStatement(sql);
//4. Définir les paramètres
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. Mise en œuvreSQL
int count = pstmt.executeUpdate(); // Nombre de lignes affectées
//6. Résultats du traitement
System.out.println(count > 0);
//7. Libérer des ressources
pstmt.close();
conn.close();
}
public static void main(String[] args) throws Exception {
fun();
}
}

4.Supprimer les données
public class DruidDemo {
public static void fun() throws Exception {
// Recevoir les paramètres soumis par la page
int id = 4;
//1. AccèsConnection
//3. Charger le profil
Properties prop = new Properties();
prop.load(new FileInputStream("jdbc-demo/src/druid.properties"));
//4. Obtenir l'objet du pool de connexion
DataSource dataSource = DruidDataSourceFactory.createDataSource(prop);
//5. Obtenir une connexion à la base de données Connection
Connection conn = dataSource.getConnection();
//2. DéfinitionSQL
String sql = " delete from tb_brand where id = ?";
//3. AccèspstmtObjet
PreparedStatement pstmt = conn.prepareStatement(sql);
//4. Définir les paramètres
pstmt.setInt(1,id);
//5. Mise en œuvreSQL
int count = pstmt.executeUpdate(); // Nombre de lignes affectées
//6. Résultats du traitement
System.out.println(count > 0);
//7. Libérer des ressources
pstmt.close();
conn.close();
}
public static void main(String[] args) throws Exception {
fun();
}
}


边栏推荐
- 20220527_ Database process_ Statement retention
- BBR encounters cubic
- Why does RTOS system use MPU?
- 2016. maximum difference between incremental elements
- [adjustment] postgraduate enrollment of Northeast Petroleum University in 2022 (including adjustment)
- I've been interviewed. The starting salary is 16K
- Win11麦克风测试在哪里?Win11测试麦克风的方法
- The use of 8255 interface chip and ADC0809
- 潘多拉 IOT 开发板学习(HAL 库)—— 实验3 按键输入实验(学习笔记)
- 【STL源码剖析】仿函数(待补充)
猜你喜欢

C MVC creates a view to get rid of the influence of layout
![[ml] Li Hongyi III: gradient descent & Classification (Gaussian distribution)](/img/75/3f6203410dd2754e578e0baaaa9aef.png)
[ml] Li Hongyi III: gradient descent & Classification (Gaussian distribution)

Cryptography -- the mode of block cipher

"A good programmer is worth five ordinary programmers!"

详解Promise使用

Redis expiration policy +conf record

PotPlayer设置最小化的快捷键

内网渗透 | 手把手教你如何进行内网渗透

Getting started with golang: for Range an alternative method of modifying the values of elements in slices

How does win11 turn on visual control? Win11 method of turning on visual control
随机推荐
Call vs2015 with MATLAB to compile vs Project
Compose 中的 'ViewPager' 详解 | 开发者说·DTalk
ADC of stm32
Why can't the start method be called repeatedly? But the run method can?
All things work together, and I will review oceanbase's practice in government and enterprise industry
C MVC creates a view to get rid of the influence of layout
Win11系统explorer频繁卡死无响应的三种解决方法
Win11麦克风测试在哪里?Win11测试麦克风的方法
Win11自动关机设置在哪?Win11设置自动关机的两种方法
Win11启用粘滞键关闭不了怎么办?粘滞键取消了但不管用怎么解决
一文掌握基于深度学习的人脸表情识别开发(基于PaddlePaddle)
SharedPreferences 保存List<Bean> 到本地并解决com.google.gson.internal.LinkedTreeMap cannot be cast to异常
Deep analysis of data storage in memory - C language
Third party payment function test point [Hangzhou multi tester _ Wang Sir] [Hangzhou multi tester]
Typical case of data annotation: how does jinglianwen technology help enterprises build data solutions
Redis expiration policy +conf record
Explain promise usage in detail
CDN acceleration requires the domain name to be filed first
SharedPreferences save list < bean > to local and solve com google. gson. internal. Linkedtreemap cannot be cast to exception
Go project operation method