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

边栏推荐
- What sparks can applet container technology collide with IOT
- Guanghetong's high-performance 4g/5g wireless module solution comprehensively promotes an efficient and low-carbon smart grid
- How to solve the problem that computers often flash
- A method for detecting outliers of data
- 一文了解数据异常值检测方法
- MySQL relearn 1-centos install mysql5.7
- From scratch, use Jenkins to build and publish pipeline pipeline project
- 2022 examination questions for safety managers of metal and nonmetal mines (underground mines) and examination papers for safety managers of metal and nonmetal mines (underground mines)
- Redis 哨兵机制
- awk从入门到入土(12)awk也可以写脚本,替代shell
猜你喜欢

Educational Codeforces Round 115 (Rated for Div. 2)
](/img/dc/5c8077c10cdc7ad6e6f92dedfbe797.png)
C语言-入门-基础-语法-[变量,常亮,作用域](五)

Call Baidu map to display the current position

AI Winter Olympics | is the future coming? Enter the entrance of the meta universe - virtual digital human

awk从入门到入土(12)awk也可以写脚本,替代shell

Developers really review CSDN question and answer function, and there are many improvements~

Go zero micro service practical series (IX. ultimate optimization of seckill performance)

High order phase difference such as smear caused by myopic surgery

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

Conversion of yolov5 XML dataset to VOC dataset
随机推荐
[attack and defense world | WP] cat
Manjaro install wechat
Xcode 6 swift code completion does not work properly - Xcode 6 swift code completion not working
上周热点回顾(6.27-7.3)
The basic syntax of mermaid in typera
1211 or chicken and rabbit in the same cage
小程序容器技术与物联网 IoT 可以碰撞出什么样的火花
学习Nuxt.js
Awk from entry to earth (7) conditional statements
How to play dapr without kubernetes?
Famous blackmail software stops operation and releases decryption keys. Most hospital IOT devices have security vulnerabilities | global network security hotspot on February 14
How does Xiaobai buy a suitable notebook
awk从入门到入土(7)条件语句
Awk from entry to earth (12) awk can also write scripts to replace the shell
Awk from getting started to digging in (11) detailed explanation of awk getline function
Technology sharing | MySQL parallel DDL
The second session of the question swiping and punching activity -- solving the switching problem with recursion as the background (I)
09 softmax regression + loss function
[untitled] 2022 polymerization process analysis and polymerization process simulation examination
ctfshow web255 web 256 web257

