当前位置:网站首页>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();
}
}
边栏推荐
- 4 special cases! Schools in area a adopt the re examination score line in area B!
- 为什么RTOS系统要使用MPU?
- BBR encounters cubic
- Warning: implicitly declaring library function 'printf' with type 'int (const char *,...)‘
- RecyclerView结合ViewBinding的使用
- Boost库链接错误解决方案
- [adjustment] postgraduate enrollment of Northeast Petroleum University in 2022 (including adjustment)
- 阿里云有奖体验:如何使用 PolarDB-X
- Eight bit responder [51 single chip microcomputer]
- Talk about memory model and memory order
猜你喜欢
PotPlayer设置最小化的快捷键
4 special cases! Schools in area a adopt the re examination score line in area B!
Implementation of VGA protocol based on FPGA
Realize the linkage between bottomnavigationview and navigation
Simple square wave generating circuit [51 single chip microcomputer and 8253a]
购买完域名之后能干什么事儿?
What experience is there only one test in the company? Listen to what they say
Interface switching based on pyqt5 toolbar button -1
Win11麦克风测试在哪里?Win11测试麦克风的方法
[redis notes] compressed list (ziplist)
随机推荐
面试过了,起薪16k
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:
数字图像处理实验目录
[analysis of STL source code] imitation function (to be supplemented)
Sword finger offer II 099 Sum of minimum paths - double hundred code
[error record] the flutter reports an error (could not resolve io.flutter:flutter_embedding_debug:1.0.0.)
理想汽车×OceanBase:当造车新势力遇上数据库新势力
内网渗透 | 手把手教你如何进行内网渗透
Brief introduction to common sense of Zhongtai
Print out mode of go
一文掌握基于深度学习的人脸表情识别开发(基于PaddlePaddle)
Win11启用粘滞键关闭不了怎么办?粘滞键取消了但不管用怎么解决
[adjustment] postgraduate enrollment of Northeast Petroleum University in 2022 (including adjustment)
Three solutions to frequent sticking and no response of explorer in win11 system
CDN acceleration requires the domain name to be filed first
Redis expiration policy +conf record
购买完域名之后能干什么事儿?
Arduino - character judgment function
All things work together, and I will review oceanbase's practice in government and enterprise industry
【直播预约】数据库OBCP认证全面升级公开课