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

边栏推荐
- Newh3c - network address translation (NAT)
- Awk from entry to earth (18) GAW K line manual
- [untitled] forwarding least square method
- Awk from entry to earth (12) awk can also write scripts to replace the shell
- From scratch, use Jenkins to build and publish pipeline pipeline project
- ctfshow web255 web 256 web257
- 【无标题】转发最小二乘法
- Basic discipline formula and unit conversion
- Service call feign of "micro service"
- C language - Introduction - Foundation - syntax - [operators, type conversion] (6)
猜你喜欢

微服务入门:Gateway网关
![Langage C - démarrer - base - syntaxe - [opérateur, conversion de type] (vi)](/img/3f/4d8f4c77d9fde5dd3f53ef890ecfa8.png)
Langage C - démarrer - base - syntaxe - [opérateur, conversion de type] (vi)

How can we make a monthly income of more than 10000? We media people with low income come and have a look

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

微服務入門:Gateway網關

没有Kubernetes怎么玩Dapr?

Ehrlich sieve + Euler sieve + interval sieve

Display Chinese characters according to numbers

【无标题】转发最小二乘法

Sequence model
随机推荐
awk从入土到入门(10)awk内置函数
DM8 uses different databases to archive and recover after multiple failures
A subclass must use the super keyword to call the methods of its parent class
ctfshow web255 web 256 web257
Go zero micro service practical series (IX. ultimate optimization of seckill performance)
awk从入门到入土(7)条件语句
C#,数值计算(Numerical Recipes in C#),线性代数方程的求解,Gauss-Jordan消去法,源代码
High order phase difference such as smear caused by myopic surgery
Leetcode topic [array] - 121 - the best time to buy and sell stocks
L1 regularization and L2 regularization
Awk from getting started to digging in (4) user defined variables
[CV] Wu Enda machine learning course notes | Chapter 9
Fault analysis | MySQL: unique key constraint failure
ArcGIS application (XXII) ArcMap loading lidar Las format data
Sequence model
Openfeign service interface call
Codeforces Round #750 (Div. 2)(A,B,C,D,F1)
go-zero微服务实战系列(九、极致优化秒杀性能)
微服務入門:Gateway網關
awk从入门到入土(5)简单条件匹配

