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

边栏推荐
- Leetcode topic [array] -136- numbers that appear only once
- C, Numerical Recipes in C, solution of linear algebraic equations, Gauss Jordan elimination method, source code
- DM8 uses different databases to archive and recover after multiple failures
- awk从入门到入土(5)简单条件匹配
- NewH3C——ACL
- [untitled] forwarding least square method
- Bishi blog (13) -- oral arithmetic test app
- How to pass custom object via intent in kotlin
- Ehrlich sieve + Euler sieve + interval sieve
- Horizon sunrise X3 PI (I) first boot details
猜你喜欢

Codeforces Round #803 (Div. 2)(A-D)

How to re enable local connection when the network of laptop is disabled

ArcGIS应用(二十二)Arcmap加载激光雷达las格式数据

CLion-控制台输出中文乱码

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

转:优秀的管理者,关注的不是错误,而是优势
![[error record] no matching function for call to 'cacheflush' cacheflush();)](/img/79/c00f9c835606b2dce1d342ec368d24.jpg)
[error record] no matching function for call to 'cacheflush' cacheflush();)

ctfshow web255 web 256 web257

根据数字显示中文汉字
![[untitled] forwarding least square method](/img/8b/4039715e42cb4dc3e1006e8094184a.png)
[untitled] forwarding least square method
随机推荐
LinkedList in the list set is stored in order
Langage C - démarrer - base - syntaxe - [opérateur, conversion de type] (vi)
How to play dapr without kubernetes?
AI Winter Olympics | is the future coming? Enter the entrance of the meta universe - virtual digital human
Cancel ctrl+alt+delete when starting up
Learn nuxt js
High order phase difference such as smear caused by myopic surgery
C语言-入门-基础-语法-[变量,常亮,作用域](五)
[BSP video tutorial] stm32h7 video tutorial phase 5: MDK topic, system introduction to MDK debugging, AC5, AC6 compilers, RTE development environment and the role of various configuration items (2022-
How can we make a monthly income of more than 10000? We media people with low income come and have a look
[attack and defense world | WP] cat
Display Chinese characters according to numbers
[untitled] 2022 polymerization process analysis and polymerization process simulation examination
C language - Introduction - Foundation - syntax - data type (4)
How to solve the problem that computers often flash
Research Report on research and investment prospects of China's testing machine industry (2022 Edition)
Basic discipline formula and unit conversion
【无标题】转发最小二乘法
Sequence model
Ehrlich sieve + Euler sieve + interval sieve

