当前位置:网站首页>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
边栏推荐
- A subclass must use the super keyword to call the methods of its parent class
- The upper layer route cannot Ping the lower layer route
- Codeforces Global Round 21(A-E)
- Langage C - démarrer - base - syntaxe - [opérateur, conversion de type] (vi)
- C语言-入门-基础-语法-[标识符,关键字,分号,空格,注释,输入和输出](三)
- Awk from entry to earth (14) awk output redirection
- 微服务入门:Gateway网关
- Live in a dream, only do things you don't say
- Leetcode topic [array] -136- numbers that appear only once
- Talk about single case mode
猜你喜欢
High order phase difference such as smear caused by myopic surgery
[CV] Wu Enda machine learning course notes | Chapter 9
Newh3c - network address translation (NAT)
【无标题】转发最小二乘法
Turn: excellent managers focus not on mistakes, but on advantages
Clion console output Chinese garbled code
L1 regularization and L2 regularization
C语言-入门-基础-语法-[主函数,头文件](二)
CLion-控制台输出中文乱码
Four essential material websites for we media people to help you easily create popular models
随机推荐
DM8 tablespace backup and recovery
Cancel ctrl+alt+delete when starting up
Question 49: how to quickly determine the impact of IO latency on MySQL performance
C语言-入门-基础-语法-[变量,常亮,作用域](五)
老掉牙的 synchronized 锁优化,一次给你讲清楚!
Report on research and investment prospects of polyglycolic acid industry in China (2022 Edition)
Review of last week's hot spots (6.27-7.3)
manjaro安装微信
Snipaste convenient screenshot software, which can be copied on the screen
Developers really review CSDN question and answer function, and there are many improvements~
Sequence model
Leetcode topic [array] - 121 - the best time to buy and sell stocks
Display Chinese characters according to numbers
awk从入门到入土(11)awk getline函数详解
Relationship and operation of random events
Call Baidu map to display the current position
Manjaro install wechat
C#实现一个万物皆可排序的队列
Clion console output Chinese garbled code
C语言-入门-基础-语法-[主函数,头文件](二)