当前位置:网站首页>保姆级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();
}
}
结果
边栏推荐
- Awk from entry to penetration (6) regular matching
- [attack and defense world | WP] cat
- A method for detecting outliers of data
- Awk from entry to earth (7) conditional statements
- Awk from digging into the ground to getting started (10) awk built-in functions
- Codeforces Round #803 (Div. 2)(A-D)
- NewH3C——ACL
- MySQL relearn 1-centos install mysql5.7
- 【无标题】转发最小二乘法
- Redis sentinel mechanism
猜你喜欢
DM8 command line installation and database creation
What exactly is DAAS data as a service? Don't be misled by other DAAS concepts
Four essential material websites for we media people to help you easily create popular models
DM8 database recovery based on point in time
Bishi blog (13) -- oral arithmetic test app
Azure ad domain service (II) configure azure file share disk sharing for machines in the domain service
09 softmax regression + loss function
Take you to master the formatter of visual studio code
Educational Codeforces Round 119 (Rated for Div. 2)
A method for detecting outliers of data
随机推荐
AI Winter Olympics | is the future coming? Enter the entrance of the meta universe - virtual digital human
没有Kubernetes怎么玩Dapr?
How to re enable local connection when the network of laptop is disabled
awk从入门到入土(18)gawk线上手册
User login function: simple but difficult
How college students choose suitable computers
L1 regularization and L2 regularization
If the array values match each other, shuffle again - PHP
Awk from entry to earth (14) awk output redirection
Newh3c - network address translation (NAT)
std::is_ union,std::is_ class,std::integral_ constant
学习Nuxt.js
老掉牙的 synchronized 锁优化,一次给你讲清楚!
[attack and defense world | WP] cat
2022 electrician (intermediate) examination question bank and electrician (intermediate) examination questions and analysis
Codeforces Round #750 (Div. 2)(A,B,C,D,F1)
swatch
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 entry to earth (8) array
NewH3C——ACL