CC00105.bigdatajava——|Java&MySQL.数据库连接池和DBUtils.V14|——|MySQL.v15|QueryRunner类|实现插入/修改|

一、QueryRunner实现增、删、改操作
### --- 核心方法

——>        update(Connection conn, String sql, Object... params)
参数说明
Connection conn 数据库连接对象, 自动模式创建QueryRun 可以不传 ,手动模式必须传递
String sql占位符形式的SQL ,使用 ? 号占位符
Object... paramObject类型的 可变参,用来设置占位符上的参数
### --- 步骤

——>        1.创建QueryRunner(手动或自动)
——>        2.占位符方式 编写SQL
——>        3.设置占位符参数
——>        4.执行
二、添加
@Test
public void testInsert() throws SQLException {

    //1.创建 QueryRunner 手动模式创建
    QueryRunner qr = new QueryRunner();

    //2.编写 占位符方式 SQL
    String sql = "insert into employee values(?,?,?,?,?,?)";

    //3.设置占位符的参数
    Object[] param = {null,"张百万",20,"女",10000,"1990-12-26"};

    //4.执行 update方法
    Connection con = DruidUtils.getConnection();
    int i = qr.update(con, sql, param);

    //5.释放资源
    DbUtils.closeQuietly(con);
}
三、修改
//修改操作 修改姓名为张百万的员工工资
@Test
public void testUpdate() throws SQLException {

    //1.创建QueryRunner对象 自动模式,传入数据库连接池
    QueryRunner qr = new QueryRunner(DruidUtils.getDataSource());
    
    //2.编写SQL
    String sql = "update employee set salary = ? where ename = ?";

    //3.设置占位符参数
    Object[] param = {0,"张百万"};

    //4.执行update, 不需要传入连接对象
    qr.update(sql,param);
}
四、删除
//删除操作 删除id为1 的数据
@Test
public void testDelete() throws SQLException {
        QueryRunner qr = new QueryRunner(DruidUtils.getDataSource());
        String sql = "delete from employee where eid = ?";
        //只有一个参数,不需要创建数组
        qr.update(sql,1);
   }
五 、sql语句
package com.yanqi.testDBUtils;

import com.yanqi.utils.DbcpUtils;
import com.yanqi.utils.DruidUtils;
import org.apache.commons.dbutils.DbUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.junit.Test;

import java.sql.Connection;
import java.sql.SQLException;

/*
 *   使用QueryRunner对象 完成增删改
 *       update(Connection con,String sql,Object... param) 方法
 *
 * */
public class DBUtilsDemo02 {

    //插入操作
    @Test
    public void testInsert() throws SQLException {

        //1.创建 QueryRunner 手动模式创建
        QueryRunner qr = new QueryRunner();

        //2.编写 占位符方式 SQL
        String sql = "insert into employee values(?,?,?,?,?,?)";

        //3.设置占位符的参数
        Object[] param = {null,"张百万",20,"女",10000,"1990-12-26"};

        //4.执行 update方法
        Connection con = DruidUtils.getConnection();
        int i = qr.update(con, sql, param);

        //5.释放资源
        DbUtils.closeQuietly(con);
    }

    //修改操作 修改姓名为 张百万的员工的工资为 15000
    @Test
    public void testUpdate() throws SQLException {

        //1.创建 核心类 自动模式 需要传递 数据库连接池对象
        QueryRunner qr = new QueryRunner(DruidUtils.getDataSource());

        //2.编写SQl
        String sql = "update employee set salary = ? where ename = ?";

        //3.设置占位符的参数
        Object[] param = {15000,"张百万"};

        //4.执行修改操作 自动模式不需要传入connection对象
        qr.update(sql,param);

    }

    //删除操作 删除id为1的 记录
    @Test
    public void testDelete() throws SQLException {

        QueryRunner qr = new QueryRunner(DruidUtils.getDataSource());

        String sql = "delete from employee where eid = ?";

        //如果只有一个参数的话 不需要创建数组
        qr.update(sql,1);
    }

}
六、打印输出
D:\JAVA\jdk-11.0.2\bin\java.exe -ea -Didea.test.cyclic.buffer.size=1048576 "-javaagent:D:\IntelliJIDEA\IntelliJ IDEA 2019.3.3\lib\idea_rt.jar=58639:D:\IntelliJIDEA\IntelliJ IDEA 2019.3.3\bin" -Dfile.encoding=UTF-8 -classpath "D:\IntelliJIDEA\IntelliJ IDEA 2019.3.3\lib\idea_rt.jar;D:\IntelliJIDEA\IntelliJ IDEA 2019.3.3\plugins\junit\lib\junit5-rt.jar;D:\IntelliJIDEA\IntelliJ IDEA 2019.3.3\plugins\junit\lib\junit-rt.jar;E:\NO.Z.10000——javaproject\NO.H.00002.mysql\mysql\out\production\mysql.jdbc_task06;F:\NO.Z.02000——soft——IT.实验\20210801——Hadoop\20210801.2——Hadoop——第二阶段——预科班\预科班第二阶段模块一\05 任务五 (JDBC)\03_软件\myJar\dom4j-1.6.1.jar;F:\NO.Z.02000——soft——IT.实验\20210801——Hadoop\20210801.2——Hadoop——第二阶段——预科班\预科班第二阶段模块一\05 任务五 (JDBC)\03_软件\myJar\druid-1.0.9.jar;F:\NO.Z.02000——soft——IT.实验\20210801——Hadoop\20210801.2——Hadoop——第二阶段——预科班\预科班第二阶段模块一\05 任务五 (JDBC)\03_软件\myJar\c3p0-0.9.5.2.jar;F:\NO.Z.02000——soft——IT.实验\20210801——Hadoop\20210801.2——Hadoop——第二阶段——预科班\预科班第二阶段模块一\05 任务五 (JDBC)\03_软件\myJar\commons-dbcp-1.4.jar;F:\NO.Z.02000——soft——IT.实验\20210801——Hadoop\20210801.2——Hadoop——第二阶段——预科班\预科班第二阶段模块一\05 任务五 (JDBC)\03_软件\myJar\jaxen-1.1-beta-6.jar;F:\NO.Z.02000——soft——IT.实验\20210801——Hadoop\20210801.2——Hadoop——第二阶段——预科班\预科班第二阶段模块一\05 任务五 (JDBC)\03_软件\myJar\commons-pool-1.5.6.jar;F:\NO.Z.02000——soft——IT.实验\20210801——Hadoop\20210801.2——Hadoop——第二阶段——预科班\预科班第二阶段模块一\05 任务五 (JDBC)\03_软件\myJar\commons-dbutils-1.6.jar;F:\NO.Z.02000——soft——IT.实验\20210801——Hadoop\20210801.2——Hadoop——第二阶段——预科班\预科班第二阶段模块一\05 任务五 (JDBC)\03_软件\myJar\mchange-commons-java-0.2.12.jar;F:\NO.Z.02000——soft——IT.实验\20210801——Hadoop\20210801.2——Hadoop——第二阶段——预科班\预科班第二阶段模块一\05 任务五 (JDBC)\03_软件\myJar\mysql-connector-java-5.1.37-bin.jar;C:\Users\Administrator\.m2\repository\org\testng\testng\6.14.3\testng-6.14.3.jar;C:\Users\Administrator\.m2\repository\com\beust\jcommander\1.72\jcommander-1.72.jar;C:\Users\Administrator\.m2\repository\org\apache-extras\beanshell\bsh\2.0b6\bsh-2.0b6.jar;C:\Users\Administrator\.m2\repository\org\junit\jupiter\junit-jupiter\5.4.2\junit-jupiter-5.4.2.jar;C:\Users\Administrator\.m2\repository\org\junit\jupiter\junit-jupiter-api\5.4.2\junit-jupiter-api-5.4.2.jar;C:\Users\Administrator\.m2\repository\org\apiguardian\apiguardian-api\1.0.0\apiguardian-api-1.0.0.jar;C:\Users\Administrator\.m2\repository\org\opentest4j\opentest4j\1.1.1\opentest4j-1.1.1.jar;C:\Users\Administrator\.m2\repository\org\junit\platform\junit-platform-commons\1.4.2\junit-platform-commons-1.4.2.jar;C:\Users\Administrator\.m2\repository\org\junit\jupiter\junit-jupiter-params\5.4.2\junit-jupiter-params-5.4.2.jar;C:\Users\Administrator\.m2\repository\org\junit\jupiter\junit-jupiter-engine\5.4.2\junit-jupiter-engine-5.4.2.jar;C:\Users\Administrator\.m2\repository\org\junit\platform\junit-platform-engine\1.4.2\junit-platform-engine-1.4.2.jar;C:\Users\Administrator\.m2\repository\junit\junit\4.12\junit-4.12.jar;C:\Users\Administrator\.m2\repository\org\hamcrest\hamcrest-core\1.3\hamcrest-core-1.3.jar" com.intellij.rt.junit.JUnitStarter -ideVersion5 -junit4 com.yanqi.testDBUtils.DBUtilsDemo02
8月 05, 2021 10:26:57 下午 com.alibaba.druid.pool.DruidDataSource info
信息: {dataSource-1} inited

Process finished with exit code 0
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yanqi_vip

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值