当前位置:网站首页>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
边栏推荐
- Awk from getting started to digging in (9) circular statement
- Educational Codeforces Round 115 (Rated for Div. 2)
- 1211 or chicken and rabbit in the same cage
- Sequence model
- 09 softmax regression + loss function
- 根据数字显示中文汉字
- Fault analysis | MySQL: unique key constraint failure
- 没有Kubernetes怎么玩Dapr?
- There are 100 people eating 100 apples, one adult eating 4 apples, and four children eating 1 apple. How can they eat exactly 100 apples? Use any high-level language you are familiar with
- 2022 electrician (intermediate) examination question bank and electrician (intermediate) examination questions and analysis
猜你喜欢
What if I forget the router password
Snipaste convenient screenshot software, which can be copied on the screen
CLion-控制台输出中文乱码
Live in a dream, only do things you don't say
Codeforces Round #803 (Div. 2)(A-D)
NewH3C——ACL
What should I do if there is a problem with the graphics card screen on the computer
[untitled] forwarding least square method
manjaro安装微信
Clion console output Chinese garbled code
随机推荐
2022 tower crane driver examination and tower crane driver examination questions and analysis
Display Chinese characters according to numbers
【LeetCode 42】501. Mode in binary search tree
20220701 barbarat lemma proof
如何通过antd的upload控件,将图片以文件流的形式发送给服务器
Industry depression has the advantages of industry depression
Educational Codeforces Round 119 (Rated for Div. 2)
Codeforces Global Round 21(A-E)
Comparison between sentinel and hystrix
awk从入门到入土(18)gawk线上手册
C language - Introduction - Foundation - syntax - data type (4)
AI Winter Olympics | is the future coming? Enter the entrance of the meta universe - virtual digital human
FRP intranet penetration, reverse proxy
manjaro安装微信
MySQL relearn 1-centos install mysql5.7
LinkedList in the list set is stored in order
ArcGIS应用(二十二)Arcmap加载激光雷达las格式数据
Ehrlich sieve + Euler sieve + interval sieve
Awk from getting started to digging in (9) circular statement
C#实现一个万物皆可排序的队列