当前位置:网站首页>保姆级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();
}
}
结果

边栏推荐
- SSRF vulnerability exploitation - attack redis
- DM8 tablespace backup and recovery
- 1211 or chicken and rabbit in the same cage
- Call Baidu map to display the current position
- awk从入门到入土(18)gawk线上手册
- What if I forget the router password
- 微服務入門:Gateway網關
- Awk from getting started to digging in (9) circular statement
- Manjaro install wechat
- Codeforces Round #750 (Div. 2)(A,B,C,D,F1)
猜你喜欢
![Leetcode topic [array] -136- numbers that appear only once](/img/6d/f2e4b812e5dd872fbeb43732d6f27f.jpg)
Leetcode topic [array] -136- numbers that appear only once

Fault analysis | MySQL: unique key constraint failure

Take you to master the formatter of visual studio code

What if I forget the router password

Sequence model

Codeforces Round #750 (Div. 2)(A,B,C,D,F1)

snipaste 方便的截图软件,可以复制在屏幕上

DM8 uses different databases to archive and recover after multiple failures

A method for detecting outliers of data

4 small ways to make your Tiktok video clearer
随机推荐
How to solve the problem of computer jam and slow down
Question 49: how to quickly determine the impact of IO latency on MySQL performance
The second session of the question swiping and punching activity -- solving the switching problem with recursion as the background (I)
Fault analysis | MySQL: unique key constraint failure
2022 tower crane driver examination and tower crane driver examination questions and analysis
Four essential material websites for we media people to help you easily create popular models
学习Nuxt.js
[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-
go-zero微服务实战系列(九、极致优化秒杀性能)
Ehrlich sieve + Euler sieve + interval sieve
If the array values match each other, shuffle again - PHP
Codeforces Round #803 (Div. 2)(A-D)
Awk from entry to penetration (6) regular matching
How to pass custom object via intent in kotlin
Basic operations of databases and tables ----- view data tables
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 digging into the ground to getting started (10) awk built-in functions
How can we make a monthly income of more than 10000? We media people with low income come and have a look
Awk from entry to soil (5) simple condition matching
Li Kou today's question -1200 Minimum absolute difference

