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


边栏推荐
- 购买完域名之后能干什么事儿?
- Arduino - character judgment function
- Go basic data type
- 万物并作,吾以观复|OceanBase 政企行业实践
- [error record] the flutter reports an error (could not resolve io.flutter:flutter_embedding_debug:1.0.0.)
- Which common ports should the server open
- Interface switching based on pyqt5 toolbar button -2
- 详解Promise使用
- Potplayer set minimized shortcut keys
- Print out mode of go
猜你喜欢

阿里云有奖体验:如何使用 PolarDB-X

The use of 8255 interface chip and ADC0809

PotPlayer设置最小化的快捷键

"A good programmer is worth five ordinary programmers!"

【STL源码剖析】仿函数(待补充)

How difficult is it to be high? AI rolls into the mathematics circle, and the accuracy rate of advanced mathematics examination is 81%!

Convolution和Batch normalization的融合

面试过了,起薪16k

Go project operation method

Solution: exceptiole 'xxxxx QRTZ_ Locks' doesn't exist and MySQL's my CNF file append lower_ case_ table_ Error message after names startup
随机推荐
vim区间删行注释
面试过了,起薪16k
潘多拉 IOT 开发板学习(HAL 库)—— 实验4 串口通讯实验(学习笔记)
Typical case of data annotation: how does jinglianwen technology help enterprises build data solutions
Detailed explanation of 'viewpager' in compose | developer said · dtalk
Tronapi wave field interface - source code without encryption - can be opened twice - interface document attached - packaging based on thinkphp5 - detailed guidance of the author - July 1, 2022 08:43:
The concepts of terminal voltage, phase voltage and line voltage in FOC vector control and BLDC control are still unclear
Fudian bank completes the digital upgrade | oceanbase database helps to layout the distributed architecture of the middle office
Go basic anonymous variable
Hisilicon VI access video process
Li Kou brush questions (2022-6-28)
Sword finger offer II 099 Sum of minimum paths - double hundred code
RuntimeError: no valid convolution algorithms available in CuDNN
The difference between new and make in golang
公司里只有一个测试是什么体验?听听他们怎么说吧
Where is the win11 microphone test? Win11 method of testing microphone
密码技术---分组密码的模式
C# MVC创建一个视图摆脱布局的影响
Pandora IOT development board learning (HAL Library) - Experiment 3 key input experiment (learning notes)
【Redis笔记】压缩列表(ziplist)