当前位置:网站首页>保姆级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();
}
}
结果
边栏推荐
- 2022 electrician (intermediate) examination question bank and electrician (intermediate) examination questions and analysis
- ctfshow web255 web 256 web257
- AcWing 244. Enigmatic cow (tree array + binary search)
- High order phase difference such as smear caused by myopic surgery
- How college students choose suitable computers
- awk从入门到入土(14)awk输出重定向
- AI Winter Olympics | is the future coming? Enter the entrance of the meta universe - virtual digital human
- How to play dapr without kubernetes?
- HMS core helps baby bus show high-quality children's digital content to global developers
- Li Kou today's question -1200 Minimum absolute difference
猜你喜欢
AcWing 244. Enigmatic cow (tree array + binary search)
std::is_ union,std::is_ class,std::integral_ constant
How to re enable local connection when the network of laptop is disabled
DM8 uses different databases to archive and recover after multiple failures
NewH3C——ACL
Educational Codeforces Round 115 (Rated for Div. 2)
【无标题】转发最小二乘法
How can we make a monthly income of more than 10000? We media people with low income come and have a look
Mouse over to change the transparency of web page image
How to choose solid state hard disk and mechanical hard disk in computer
随机推荐
C, Numerical Recipes in C, solution of linear algebraic equations, Gauss Jordan elimination method, source code
Redis sentinel mechanism
一文了解數據异常值檢測方法
FOC control
Codeforces Global Round 21(A-E)
Awk from getting started to digging in (9) circular statement
What if I forget the router password
C语言-入门-基础-语法-[变量,常亮,作用域](五)
Live in a dream, only do things you don't say
Four essential material websites for we media people to help you easily create popular models
2022 gas examination registration and free gas examination questions
Basic operations of databases and tables ----- view data tables
MySQL relearn 1-centos install mysql5.7
L1 regularization and L2 regularization
How to choose solid state hard disk and mechanical hard disk in computer
[error record] no matching function for call to 'cacheflush' cacheflush();)
Conversion of yolov5 XML dataset to VOC dataset
The basic syntax of mermaid in typera
DM8 uses different databases to archive and recover after multiple failures
How to play dapr without kubernetes?