增加、删除、修改、通过ID查询、查询所有

回顾


PreparedStatement 预编译SQL执行对象


登录案例:


SQL使用替换符(?),按?号所在的位置去替换我们的变量。
CRUD

1、
JDBCUtils.java  PreparedStatement.java…….

增加、删除、修改、通过ID查询、查询所有
package com.ww.jdbc.junit;

import com.ww.jdbc_demo_01.javabean.User;
import com.ww.jdbc_demo_01.util.JdbcUtils;
import org.junit.Test;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class JdbcDemo05 {

    //增加
    @Test
    public void addUser() throws SQLException {
        //1、增加SQL语句
//        String sql="insert into user(username,password,nickname) values(?,?,?)";
        String sql="insert into user values(null,?,?,?)";
        //2、获取连接
        Connection connection= JdbcUtils.getConnection();
        //3、创建PreparedStatement对象
        PreparedStatement preparedStatement=connection.prepareStatement(sql);
        //4、替换替换符
        preparedStatement.setString(1,"lisi");
        preparedStatement.setString(2,"lisi123456");
        preparedStatement.setString(3,"李四");
        // 5、执行SQL
        preparedStatement.executeUpdate();
        // 6、释放资源
        JdbcUtils.closeAll(null,preparedStatement,connection);
    }

    //修改
    @Test
    public void updateUser() throws SQLException {
        //1、修改SQL语句
        String sql="update user set nickname=? where id=?";
        //2、获取连接
        Connection connection=JdbcUtils.getConnection();
        //3、创建PreparedStatement对象
        PreparedStatement preparedStatement=connection.prepareStatement(sql);
        //4、替换替换符
        preparedStatement.setString(1,"小李子");
        preparedStatement.setInt(2,2);
        //5、执行SQL
        preparedStatement.executeUpdate();//********
        //6、释放资源
        JdbcUtils.closeAll(null,preparedStatement,connection);
    }
    //删除
    @Test
    public void deleteUser() throws SQLException {
        //1、删除SQL语句
        String sql="delete from user where id=?";
        //2、获取连接
        Connection connection=JdbcUtils.getConnection();
        //3、创建PreparedStatement对象
        PreparedStatement preparedStatement=connection.prepareStatement(sql);
        //4、替换替换符
        preparedStatement.setInt(1,5);
        //5、执行SQL
        preparedStatement.executeUpdate();//********
        //6、释放资源
        JdbcUtils.closeAll(null,preparedStatement,connection);
    }

    //查询所有
    @Test
    public void selectAllUser() throws SQLException {
        //1、查询所有SQL语句
        String sql="select * from user";
        //2、获取连接
        Connection connection=JdbcUtils.getConnection();
        //3、创建PreparedStatement对象
        PreparedStatement preparedStatement=connection.prepareStatement(sql);
        //4、替换替换符(无参数)
        //5、执行SQL,拿到结果集
        ResultSet resultSet=preparedStatement.executeQuery();
        //6、取出结果集,存入到实体类;实体类存入到集合
        List<User> list=new ArrayList<User>();
        while(resultSet.next()){
            User user=new User();
            user.setId(resultSet.getInt("id"));
            user.setUsername(resultSet.getString("username"));
            user.setPassword(resultSet.getString("password"));
            user.setNickname(resultSet.getString("nickname"));

            list.add(user);
        }


        //7、释放资源


        JdbcUtils.closeAll(resultSet,preparedStatement,connection);
        //8、集合处理,(1)返回return (2)打印输出了
        for(User user:list){
            System.out.println(user.toString());
        }
    }

    //通过ID查询对应记录
    @Test
    public void selectByIdUser() throws SQLException {
        //1、通过ID查询的SQL语句
        String sql="select * from user where id=?";
        //2、获取连接
        Connection connection=JdbcUtils.getConnection();
        //3、创建PreparedStatement对象
        PreparedStatement preparedStatement=connection.prepareStatement(sql);
        //4、替换替换符
        preparedStatement.setInt(1,6);
        //5、执行SQL,拿到结果集
        ResultSet resultSet=preparedStatement.executeQuery();
        //6、取出结果集,存入到实体类;实体类存入到集合
        List<User> list=new ArrayList<User>();
        while(resultSet.next()){
            User user=new User();
            user.setId(resultSet.getInt("id"));
            user.setUsername(resultSet.getString("username"));
            user.setPassword(resultSet.getString("password"));
            user.setNickname(resultSet.getString("nickname"));

            list.add(user);
        }
        //7、释放资源
        JdbcUtils.closeAll(resultSet,preparedStatement,connection);
        //8、集合处理,(1)返回return (2)打印输出了
        for(User user:list){
            System.out.println(user.toString());
        }
    }
}

2、JDBC事务


setAutoCommit()  false,表示关闭自动提交;true,开启自动提交事务;
commit()
rollback()

package com.ww.jdbc.junit;

import com.ww.jdbc_demo_01.util.JdbcUtils;
import org.junit.Test;

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

public class JdbcDemo06 {
    //修改,增加事务步骤
    @Test
    public void updateUser(){
        //1、修改SQL语句
        String sql="update user set nickname=? where id=?";
        //2、获取连接
        Connection connection= JdbcUtils.getConnection();
        PreparedStatement preparedStatement= null;
        try {
            //事务---开启事务---把事务从自动,调整为手动提交
            connection.setAutoCommit(false);
            //3、创建PreparedStatement对象
            preparedStatement = connection.prepareStatement(sql);
            //4、替换替换符
            preparedStatement.setString(1,"小李子22222");
            preparedStatement.setInt(2,2);
            //5、执行SQL
            preparedStatement.executeUpdate();//******** 
            //事务---结束事务---事务提交
            connection.commit();
        } catch (SQLException e) {
            try {
                //事务---结束事务---事务回滚
                connection.rollback();
            } catch (SQLException ex) {
                throw new RuntimeException(ex);
            }
        } finally {
            //6、释放资源
            JdbcUtils.closeAll(null,preparedStatement,connection);
        }
    }
}


转账案例:


1、准备数据库表
account
2、需求
zs给ls转了100
3、分析
zs要从他的帐号里面扣出100
ls要往他的帐号增加100
4、代码
 //转账,通过事务实现
    @Test
    public void zhuangZhang(){
        //1、准备好SQL
        String sql="update account set money=money+? where uname=?";
//        update account set money=money+(-100) where uname='zs';
//        update account set money=money+100 where uname='ls';

        //获取连接
        Connection connection=JdbcUtils.getConnection();
        PreparedStatement preparedStatement01=null;
        PreparedStatement preparedStatement02=null;
        try {
            //开启手动提交事务
            connection.setAutoCommit(false);
            //创建预编译对象
            preparedStatement01=connection.prepareStatement(sql);
            preparedStatement02=connection.prepareStatement(sql);
            //替换替换符
            preparedStatement01.setDouble(1,-100);
            preparedStatement01.setString(2,"zs");

            preparedStatement02.setDouble(1,100);
            preparedStatement02.setString(2,"ls");

            //执行
            preparedStatement01.executeUpdate();
            preparedStatement02.executeUpdate();
//
//            int i=1;
//            i=i/0;

            connection.commit();
        } catch (SQLException e) {
            try {
                connection.rollback();
            } catch (SQLException ex) {
                throw new RuntimeException(ex);
            }
        } finally {
            JdbcUtils.closeAll(null,preparedStatement01,null);
            JdbcUtils.closeAll(null,preparedStatement02,connection);
        }
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值