JdbcPreparedStatement(推荐使用)

JdbcPreparedStatement(推荐使用)

package jdbc;

import org.junit.Test;

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

/**
 * @author 小邱
 * @version 0.0.1
 * @description JdbcPreparedStatementTest
 * @since 2021/9/27 19:29
 */
public class JdbcPreparedStatementTest {
    /**
     * PreparedStatement 接口是 Statement 的子接口,它表示一条预编译过的 SQL 语句
     * PreparedStatement 对象所代表的 SQL 语句中的参数用问号(?)来表示,调用
     * PreparedStatement 对象的setXxx() 方法来设置这些参数.
     * setXxx() 方法有两个参数,第一个参数是要设置的 SQL 语句中的参数的索引(从 1开始),第二个是设置的 SQL 语句中的参数的值
     */

    //实现增操作
    @Test
    public void insertTest() throws Exception {
        //获取连接
        Connection connection = JdbcUtils.getConnection();
        //sql语句
        String sql = "insert into t_user(username,password,email) values(?,?,?)";
        //预编译sql语句
        PreparedStatement ps = connection.prepareStatement(sql);
        //填充占位符(占位符从1开始)
        ps.setString(1, "张三三");
        ps.setString(2, "123456");
        ps.setString(3, "zhangsan@qq.com");
        //执行sql语句
        ps.execute();
        //关闭资源
        JdbcUtils.close(connection, ps);
    }

    //实现删操作
    @Test
    public void deleteTest() throws SQLException {
        //获取连接
        Connection connection = JdbcUtils.getConnection();
        //sql语句
        String sql = "delete from t_user where username = ?";
        //预编译sql语句
        PreparedStatement ps = connection.prepareStatement(sql);
        //填充占位符(占位符从1开始)
        ps.setString(1, "??");

        //执行sql语句
        ps.execute();
        //关闭资源
        JdbcUtils.close(connection, ps);
    }

    //实现改操作
    @Test
    public void updateTest() throws SQLException {
        //获取连接
        Connection connection = JdbcUtils.getConnection();
        //sql语句
        String sql = "update t_user set username = ? where username = ?";
        //预编译sql语句
        PreparedStatement ps = connection.prepareStatement(sql);
        //填充占位符(占位符从1开始)
        ps.setString(1, "wangwu");
        ps.setString(2, "liSi");
        //执行sql语句
        ps.execute();
        //关闭资源
        JdbcUtils.close(connection, ps);
    }

    //实现增删改通用操作
    public void upDate(String sql, Object... args) {
        Connection connection = null;
        PreparedStatement ps = null;
        try {
            //获取连接
            connection = JdbcUtils.getConnection();
            //预编译sql语句
            ps = connection.prepareStatement(sql);
            //填充占位符(占位符从1开始)
            for (int i = 0; i < args.length; i++) {
                ps.setObject(i+1, args[i]);
            }
            //执行sql语句
            ps.execute();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            //关闭资源
            JdbcUtils.close(connection, ps);
        }
    }

    //实现增删改通用操作测试
    @Test
    public void upDateTest() {
        //增
        String sql = "insert into t_user(username,password,email) values(?,?,?)";
//        upDate(sql, "xiaoMing", "123abc", "xiaomin@qq.com");

        //改
        sql = "update t_user set username = ? where password = ?";
//        upDate(sql, "xiaoQing", "123abc");

        //删
        sql = "delete from t_user where username = ?";
        upDate(sql, "xiaoQing");
    }

}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值