当前位置:网站首页>Nurse level JDEC addition, deletion, modification and inspection exercise
Nurse level JDEC addition, deletion, modification and inspection exercise
2022-07-04 08:55:00 【Fate friend I】
We must persist in :
every last NB The characters , Have a hard time , But like SB Just stick to it , will NB!!!
I hope this article can be learned by you JAVAWEB Help you when you are in trouble !!!
List of articles
JDEC Add, delete, change and check exercises
Get ready

increase
Ideas

Code
package com.yang.example;
import com.alibaba.druid.pool.DruidDataSourceFactory;
import com.yang.pojo.Brand;
import javax.sql.DataSource;
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
/** * @author Fate friend I * @date 2022/7/2-22:31 */
public class BrandTestAdd {
public static void main(String[] args) throws Exception {
testAdd();
}
/** * add to * 1.SQL:insert into tb_brand(brand_name,company_name,ordered,description,status) values(?,?,?,?,?); * 2. Parameters : need , except id All parameter information except * 3. result :boolean */
public static void testAdd() throws Exception {
// Accept 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 conn = dataSource.getConnection();
//2. Definition SQL
@SuppressWarnings("SqlResolve")
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();
//6. Processing results
if(count>0)
{
System.out.println(" Add success !");
}
else {
System.out.println(" Add failure !");
}
//7. Release collection
pStmt.close();
conn.close();
}
}
result

Delete
Ideas

Code
package com.yang.example;
import com.alibaba.druid.pool.DruidDataSourceFactory;
import com.sun.org.apache.xpath.internal.SourceTree;
import javax.sql.DataSource;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.Properties;
/** * @author Fate friend I * @date 2022/7/3-9:13 */
public class BrandTestDelete {
public static void main(String[] args) throws Exception {
testDelete();
}
/** * Delete * delete from tb_brand where id=? */
public static void testDelete() throws Exception {
// Accept the parameters submitted by the page
int id=6;
//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 objects
Connection conn = dataSource.getConnection();
//2. Definition SQL
@SuppressWarnings("SqlResolve")
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();
//6. Processing results
if(count>0) {
System.out.println(" Delete successful !");
}else {
System.out.println(" Delete failed !");
}
//7. Release collection
pStmt.close();
conn.close();
}
}
result

Change
Ideas

Code
package com.yang.example;
import com.alibaba.druid.pool.DruidDataSourceFactory;
import javax.sql.DataSource;
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.Properties;
/** * @author Fate friend I * @date 2022/7/2-22:58 */
public class BrandTestUpdate {
public static void main(String[] args) throws Exception {
testUpdate();
}
/** * modify * 1. SQL update tb_brand set brand_name=?, company_name=?, ordered=?, description=?, status=? where id=? */
public static void testUpdate() throws Exception {
// Accept 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=6;
//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 conn = dataSource.getConnection();
//2. Definition SQL
@SuppressWarnings("SqlResolve")
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();
//6. Processing results
if(count>0)
{
System.out.println(" Modification successful !");
}
else {
System.out.println(" Modification failed !");
}
//7. Release collection
pStmt.close();
conn.close();
}
}
result

check
Ideas

Code
package com.yang.example;
import com.alibaba.druid.pool.DruidDataSourceFactory;
import com.yang.pojo.Brand;
import javax.sql.DataSource;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
/** * @author Fate friend I * @date 2022/7/2-15:24 * Addition, deletion, modification and query of brand data */
public class BrandTextSelect {
public static void main(String[] args) {
try {
testSelectAll();
} catch (Exception e) {
e.printStackTrace();
}
}
/** * Query all * 1.SQL;select * from tb_brand * 2. Parameters : Unwanted * 3. result :List<Brand> */
public static void testSelectAll() 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 conn = dataSource.getConnection();
//2. Definition SQL
@SuppressWarnings("SqlResolve")
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);
// Encapsulation set
brands.add(brand);
}
System.out.println(brands);
//7. Release collection
rs.close();
pStmt.close();
conn.close();
}
}
result

边栏推荐
- Review of last week's hot spots (6.27-7.3)
- Démarrage des microservices: passerelle
- Go zero micro service practical series (IX. ultimate optimization of seckill performance)
- CLion-控制台输出中文乱码
- How to re enable local connection when the network of laptop is disabled
- Awk from entry to soil (5) simple condition matching
- [attack and defense world | WP] cat
- 微服務入門:Gateway網關
- How to solve the problem of computer jam and slow down
- What is inner connection and outer connection? What are the uses and benefits
猜你喜欢
![C language - Introduction - Foundation - syntax - [main function, header file] (II)](/img/5a/c6a3c5cd8038d17c5b0ead2ad52764.png)
C language - Introduction - Foundation - syntax - [main function, header file] (II)

Educational Codeforces Round 119 (Rated for Div. 2)

Getting started with microservices: gateway gateway

Developers really review CSDN question and answer function, and there are many improvements~

Openfeign service interface call

保姆级JDEC增删改查练习

Codeforces Global Round 21(A-E)
![[untitled] forwarding least square method](/img/8b/4039715e42cb4dc3e1006e8094184a.png)
[untitled] forwarding least square method

微服务入门:Gateway网关

Four essential material websites for we media people to help you easily create popular models
随机推荐
Awk from getting started to digging in (11) detailed explanation of awk getline function
保姆级JDEC增删改查练习
老掉牙的 synchronized 锁优化,一次给你讲清楚!
User login function: simple but difficult
Internal learning
The basic syntax of mermaid in typera
C language - Introduction - Foundation - syntax - [variable, constant light, scope] (V)
Service call feign of "micro service"
Leetcode topic [array] - 121 - the best time to buy and sell stocks
L1 regularization and L2 regularization
2022 tower crane driver examination and tower crane driver examination questions and analysis
Guanghetong's high-performance 4g/5g wireless module solution comprehensively promotes an efficient and low-carbon smart grid
awk从入门到入土(6)正则匹配
微服务入门:Gateway网关
没有Kubernetes怎么玩Dapr?
C语言-入门-基础-语法-[变量,常亮,作用域](五)
Fault analysis | MySQL: unique key constraint failure
4 small ways to make your Tiktok video clearer
awk从入门到入土(14)awk输出重定向
Awk from entry to earth (15) awk executes external commands

