当前位置:网站首页>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();
}
}
边栏推荐
猜你喜欢
Third party payment function test point [Hangzhou multi tester _ Wang Sir] [Hangzhou multi tester]
基于Pyqt5工具栏按钮可实现界面切换-2
【STL源码剖析】仿函数(待补充)
Alibaba cloud award winning experience: how to use polardb-x
Win11自动关机设置在哪?Win11设置自动关机的两种方法
Redis expiration policy +conf record
采用VNC Viewer方式遠程連接樹莓派
Talk about memory model and memory order
Tiktok actual combat ~ number of likes pop-up box
Jinglianwen technology's low price strategy helps AI enterprises reduce model training costs
随机推荐
Connexion à distance de la tarte aux framboises en mode visionneur VNC
Potplayer set minimized shortcut keys
Convolution和Batch normalization的融合
Implementation of VGA protocol based on FPGA
Why can't the start method be called repeatedly? But the run method can?
程序员版本的八荣八耻~
(stinger) use pystinger Socks4 to go online and not go out of the network host
数字图像处理实验目录
Hisilicon VI access video process
Explain promise usage in detail
Redis expiration policy +conf record
Numerical solution of partial differential equations with MATLAB
Strictly abide by the construction period and ensure the quality, this AI data annotation company has done it!
万物并作,吾以观复|OceanBase 政企行业实践
请求与响应
Remote connection of raspberry pie by VNC viewer
ping域名报错unknown host,nslookup/systemd-resolve可以正常解析,ping公网地址通怎么解决?
为什么RTOS系统要使用MPU?
RuntimeError: no valid convolution algorithms available in CuDNN
Go basic constant definition and use