当前位置:网站首页>保姆级JDEC增删改查练习
保姆级JDEC增删改查练习
2022-07-04 08:47:00 【缘友一世】
大家要坚持:
每一个NB的人物,都有一段苦逼的岁月,但是像SB一样坚持下去,终将NB!!!
希望本文能在你学习JAVAWEB有困难的时候帮助到你!!!
JDEC增删改查练习
准备
增
思路
代码
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 缘友一世 * @date 2022/7/2-22:31 */
public class BrandTestAdd {
public static void main(String[] args) throws Exception {
testAdd();
}
/** * 添加 * 1.SQL:insert into tb_brand(brand_name,company_name,ordered,description,status) values(?,?,?,?,?); * 2. 参数:需要,除了id之外的所有参数信息 * 3. 结果:boolean */
public static void testAdd() throws Exception {
//接受页面提交的参数
String brandName="香飘飘";
String companyName="香飘飘";
int ordered=1;
String description="绕地球一圈";
int status=1;
//1. 获取Connection
//3.加载配置文件
Properties prop = new Properties();
prop.load(new FileInputStream("jdbc-demo/src/druid.properties"));
//4.获取连接池对象
DataSource dataSource = DruidDataSourceFactory.createDataSource(prop);
//5.获取数据库连接
Connection conn = dataSource.getConnection();
//2. 定义SQL
@SuppressWarnings("SqlResolve")
String sql="insert into tb_brand(brand_name,company_name,ordered,description,status) values(?,?,?,?,?);";
//3. 获取pStmt对象
PreparedStatement pStmt =conn.prepareStatement(sql);
//4.设置参数
pStmt.setString(1,brandName);
pStmt.setString(2,companyName);
pStmt.setInt(3,ordered);
pStmt.setString(4,description);
pStmt.setInt(5,status);
//5. 执行sql
int count = pStmt.executeUpdate();
//6.处理结果
if(count>0)
{
System.out.println("添加成功!");
}
else {
System.out.println("添加失败!");
}
//7. 释放集合
pStmt.close();
conn.close();
}
}
结果
删
思路
代码
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 缘友一世 * @date 2022/7/3-9:13 */
public class BrandTestDelete {
public static void main(String[] args) throws Exception {
testDelete();
}
/** * 删除 * delete from tb_brand where id=? */
public static void testDelete() throws Exception {
//接受页面提交的参数
int id=6;
//1.获取Connection
//3.加载配置文件
Properties prop = new Properties();
prop.load(new FileInputStream("jdbc-demo/src/druid.properties"));
//4.获取连接池对象
DataSource dataSource = DruidDataSourceFactory.createDataSource(prop);
//5.获取数据库对象
Connection conn = dataSource.getConnection();
//2.定义SQL
@SuppressWarnings("SqlResolve")
String sql="delete from tb_brand where id=?";
//3.获取pStmt对象
PreparedStatement pStmt = conn.prepareStatement(sql);
//4.设置参数
pStmt.setInt(1,id);
//5.执行sql
int count = pStmt.executeUpdate();
//6.处理结果
if(count>0) {
System.out.println("删除成功!");
}else {
System.out.println("删除失败!");
}
//7.释放集合
pStmt.close();
conn.close();
}
}
结果
改
思路
代码
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 缘友一世 * @date 2022/7/2-22:58 */
public class BrandTestUpdate {
public static void main(String[] args) throws Exception {
testUpdate();
}
/** * 修改 * 1. SQL update tb_brand set brand_name=?, company_name=?, ordered=?, description=?, status=? where id=? */
public static void testUpdate() throws Exception {
//接受页面提交的参数
String brandName="香飘飘";
String companyName="香飘飘";
int ordered=1000;
String description="绕地球三圈";
int status=1;
int id=6;
//1. 获取Connection
//3.加载配置文件
Properties prop = new Properties();
prop.load(new FileInputStream("jdbc-demo/src/druid.properties"));
//4.获取连接池对象
DataSource dataSource = DruidDataSourceFactory.createDataSource(prop);
//5.获取数据库连接
Connection conn = dataSource.getConnection();
//2. 定义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. 获取pStmt对象
PreparedStatement pStmt =conn.prepareStatement(sql);
//4.设置参数
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. 执行sql
int count = pStmt.executeUpdate();
//6.处理结果
if(count>0)
{
System.out.println("修改成功!");
}
else {
System.out.println("修改失败!");
}
//7. 释放集合
pStmt.close();
conn.close();
}
}
结果
查
思路
代码
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 缘友一世 * @date 2022/7/2-15:24 * 品牌数据的增删改查 */
public class BrandTextSelect {
public static void main(String[] args) {
try {
testSelectAll();
} catch (Exception e) {
e.printStackTrace();
}
}
/** * 查询所有 * 1.SQL;select * from tb_brand * 2.参数:不需要 * 3.结果:List<Brand> */
public static void testSelectAll() throws Exception {
//1. 获取Connection
//3.加载配置文件
Properties prop = new Properties();
prop.load(new FileInputStream("jdbc-demo/src/druid.properties"));
//4.获取连接池对象
DataSource dataSource = DruidDataSourceFactory.createDataSource(prop);
//5.获取数据库连接
Connection conn = dataSource.getConnection();
//2. 定义SQL
@SuppressWarnings("SqlResolve")
String sql="select * from tb_brand";
//3. 获取pStmt对象
PreparedStatement pStmt =conn.prepareStatement(sql);
//4.设置参数
//5. 执行sql
ResultSet rs=pStmt.executeQuery();
//6.处理结果 List<Brand>封装Brand对象,装载List集合
Brand brand=null;
List<Brand> brands=new ArrayList<>();
while(rs.next()) {
//获取数据
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");
//封装Brand对象
brand=new Brand();
brand.setId(id);
brand.setBrandName(brandName);
brand.setCompanyName(companyName);
brand.setOrdered(ordered);
brand.setDescription(description);
brand.setStatus(status);
//封装集合
brands.add(brand);
}
System.out.println(brands);
//7. 释放集合
rs.close();
pStmt.close();
conn.close();
}
}
结果
边栏推荐
- C语言-入门-基础-语法-[标识符,关键字,分号,空格,注释,输入和输出](三)
- Codeforces Round #793 (Div. 2)(A-D)
- Awk from entry to earth (18) GAW K line manual
- Learn nuxt js
- Display Chinese characters according to numbers
- Famous blackmail software stops operation and releases decryption keys. Most hospital IOT devices have security vulnerabilities | global network security hotspot on February 14
- A single element in an ordered array
- Go zero micro service practical series (IX. ultimate optimization of seckill performance)
- The second session of the question swiping and punching activity -- solving the switching problem with recursion as the background (I)
- 一文了解數據异常值檢測方法
猜你喜欢
How to play dapr without kubernetes?
HMS core helps baby bus show high-quality children's digital content to global developers
[CV] Wu Enda machine learning course notes | Chapter 9
ES6 summary
Codeforces Global Round 21(A-E)
ArcGIS应用(二十二)Arcmap加载激光雷达las格式数据
Industry depression has the advantages of industry depression
Codeforces Round #750 (Div. 2)(A,B,C,D,F1)
Sequence model
What if I forget the router password
随机推荐
Three paradigms of database design
ctfshow web255 web 256 web257
Démarrage des microservices: passerelle
[untitled] 2022 polymerization process analysis and polymerization process simulation examination
Awk from entry to earth (14) awk output redirection
ArcGIS application (XXII) ArcMap loading lidar Las format data
Codeforces Round #793 (Div. 2)(A-D)
到底什么才是DaaS数据即服务?别再被其他DaaS概念给误导了
The map set type is stored in the form of key value pairs, and the iterative traversal is faster than the list set
Awk from getting started to digging in (4) user defined variables
NewH3C——ACL
Basic operations of databases and tables ----- view data tables
09 softmax regression + loss function
Codeforces Round #803 (Div. 2)(A-D)
Learn nuxt js
Educational Codeforces Round 115 (Rated for Div. 2)
Educational Codeforces Round 119 (Rated for Div. 2)
es6总结
How college students choose suitable computers
MySQL relearn 1-centos install mysql5.7