• 2022-08-19 第六小组 瞒春 学习笔记


    🚩 前言

    🌻继续昨天的JDBC来学习并且进行一个小练习

    作者简介:大家好我是烫嘴的辛拉面,大家可以叫我拉面。
    个人主页: weixin_49405762的博客
    系列专栏: 经典算法习题集
    为大家推荐一款刷题神器哦 点击跳转进入网站

    ​✏️statement不足

    1.大量的字符串拼接,代码可读性降低
    2.sql注入

    ✒️SQL注入

    通过字符串的拼接可以得到一个恒等的sql语句,可以跳过某些判断。
    select * from user where
    username = ‘a’or 1=”and password = ‘xxxx’”

    select * from user where username = ‘zxcvzxcvzxcv’ and password = ‘b’ or ‘1’ = ‘1’

    preparedStatement :预编译(预加载)
    1.通过conn获取的对象
    2.是statement接口的子接口
    3.sql语句中可以传参。用?来占位,通过setXXX方法来给?赋值
    4.提高性能

    ✏️预编译

    String sql = “update teacher set name = ? where id =?”
    pstmt = conn.prepareStatement(sql);
    
    • 1
    • 2

    给占位符赋值,根据位置

    pstmt.setString(1,”jj”);
    pstmt.setInt(2.6);
    pstmt.exectuteUpdate();
    
    • 1
    • 2
    • 3

    要运行就给他返回值 int

    ✏️事务

    数据库事务:是数据库的特性
    Mysql的数据库引擎
    1.在Mysql中,只有使用了Innodb引擎的数据库才支持事物
    2.事物处理可以用来维护数据的完整性,要么全部执行,要么都不执行
    3.发生在DML中,增删改
    事物的四大特征ACID
    1.原子性 A
    一个事务,要么全部完成,要么都不完成
    2.一致性 C
    在事物开始之前和结束之后,数据库的完整性没有被破坏
    3.隔离性 I
    数据库是允许多个事物同时对数据进行处理,每个事务之间是相互隔离的
    4.持久性 D
    事务结束以后,对数据的增删改是永久性的。

    术语:提交事务,回滚事务(事务回滚)
    1、事务一旦提交,就不可能回滚。
    2、当一个连接对象被创建时,默认情况下自动提交事务。
    3、关闭连接时,数据会自动提交事务。

    ✏️案例

    1、创建一张银行信息表
    字段:主键 银行卡号,余额…
    2、封装方法,存款,取款,转账,所有的操作最终要数据持久化。
    3、查询余额的方法。
    4、开户、修改密码。

    package com.jsoft.afternoon.test;
    
    import com.jsoft.util.JDBCUtil;
    
    import java.io.IOException;
    import java.sql.Connection;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    
    public class AccountDao {
    
        private final Connection conn;
        {
            try {
                conn = JDBCUtil.getConnection();
            } catch (ClassNotFoundException e) {
                throw new RuntimeException(e);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    
        /**
         * 转账
         */
        public Integer transform(String out,String in,Double balance){
            // 取款之前要先查询
            ResultSet rs = null;
            PreparedStatement preparedStatement = null;
            PreparedStatement preparedStatement2 = null;
            double b = 0;
    
            String sql = "select balance from bank where accountid = ?";
    
            try {
                preparedStatement = conn.prepareStatement(sql);
                preparedStatement.setString(1,out);
    
                rs = preparedStatement.executeQuery();
                while(rs.next()) {
                    b = rs.getDouble("balance");
                }
    
                if(b >= balance) {
                    // 余额够
                    // 执行修改
                    conn.setAutoCommit(false);
                    sql = "update bank set balance = balance - ? where accountid = ?";
                    preparedStatement = conn.prepareStatement(sql);
                    preparedStatement.setDouble(1,balance);
                    preparedStatement.setString((int)2,out);
                    int i = preparedStatement.executeUpdate();
    
                    sql = "update bank set balance = balance + ? where accountid = ?";
                    preparedStatement2 = conn.prepareStatement(sql);
                    preparedStatement2.setDouble(1,balance);
                    preparedStatement2.setString((int)2,in);
                    i = preparedStatement2.executeUpdate();
    
                    conn.commit();
    
                    return i;
    
                }else{
                    // 余额不够
                    throw new RuntimeException("余额不足,转账失败");
                }
    
            } catch (SQLException e) {
                try {
                    conn.rollback();
                } catch (SQLException ex) {
                    throw new RuntimeException(ex);
                }
                throw new RuntimeException(e);
            } finally {
                JDBCUtil.close(conn,preparedStatement,rs);
                JDBCUtil.close(null,preparedStatement2);
            }
        }
    
        /**
         * 取款
         */
        public Integer out(String accountid,Double balance) {
            // 取款之前要先查询
            ResultSet rs = null;
            PreparedStatement preparedStatement = null;
            double b = 0;
    
            String sql = "select balance from bank where accountid = ?";
    
            try {
                preparedStatement = conn.prepareStatement(sql);
                preparedStatement.setString(1,accountid);
    
                rs = preparedStatement.executeQuery();
                while(rs.next()) {
                    b = rs.getDouble("balance");
                }
    
                if(b >= balance) {
                    // 余额够
                    // 执行修改
                    sql = "update bank set balance = balance - ? where accountid = ?";
                    preparedStatement = conn.prepareStatement(sql);
                    preparedStatement.setDouble(1,balance);
                    preparedStatement.setString((int)2,accountid);
    
                    int i = preparedStatement.executeUpdate();
                    return i;
    
                }else{
                    // 余额不够
                    throw new RuntimeException("余额不足,取款失败");
                }
    
            } catch (SQLException e) {
                throw new RuntimeException(e);
            } finally {
                JDBCUtil.close(conn,preparedStatement,rs);
            }
        }
    
        /**
         * 存款
         * @param accountid
         * @param balance
         * @return
         */
        public Integer in(String accountid,Double balance) {
            int i = 0;
    
            String sql = "update bank set balance = ? where accountid = ?";
            PreparedStatement preparedStatement = null;
            try {
                preparedStatement = conn.prepareStatement(sql);
                preparedStatement.setDouble(1,balance);
                preparedStatement.setString(2,accountid);
    
                i = preparedStatement.executeUpdate();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            } finally {
                JDBCUtil.close(conn,preparedStatement);
            }
    
            return i;
        }
    
        /**
         * 开户
         * @param accountid
         * @param balance
         * @return
         */
        public Integer add(String accountid,Double balance) {
    
            int i = 0;
    
            String sql = "insert into bank (accountid,balance) values (?,?)";
            PreparedStatement preparedStatement = null;
            try {
                 preparedStatement = conn.prepareStatement(sql);
                 preparedStatement.setString(1,accountid);
                 preparedStatement.setDouble(2,balance);
    
                i = preparedStatement.executeUpdate();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            } finally {
                JDBCUtil.close(conn,preparedStatement);
            }
    
            return i;
        }
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182

    必看

    算法对程序员来说及其重要,语言和开发平台不断变化,但是万变不离其宗的是那些算法和理论,刷算法最最最直白的原因就是找一个好的工作,那刷题一定是必不可少的
    现在算法刷题平台还是蛮多的,给大家介绍一个我认为与大厂关联最深的平台——牛客网
    在这里插入图片描述

    相较于其他平台,他们的题单更和工作,大厂靠拢,不光有面试必刷的101到题目,还有大量大厂真题,内容也全程免费,相较于其它会员费结算的来说 非常的友好
    在这里插入图片描述

    牛客网还支持ACM模式,没有练习过的一定要提前适应!像某团、某为,都要求自己处理输入输出,如果不提前练习会很吃亏的!
    牛客的题解更新迭代也很快,讨论区也有奇技淫巧的分享,能帮你把所有盲点扫清楚,整体来说还是非常推荐去练习的~
    传送门:牛客网

  • 相关阅读:
    华为云云耀云服务器L实例评测|带宽,磁盘,CPU,内存以及控制台监控测试
    OSPF协议:优点、初始化流程和管理
    设计模式-11种行为型模式
    【Harmony OS】【ARK UI】ets使用startAbility或startAbilityForResult方式调起Ability
    【Python毕业设计源码】django个性化服装系统
    lock4j--分布式锁中间件--​自定义获取锁失败的逻辑
    LeetCode讲解篇之151. 反转字符串中的单词
    timm模型无法联网下载采用本地读取
    猫头虎博主第5️⃣期赠书活动:《Java官方编程手册(第12版·Java 17)套装上下册》
    python:自动获取当前系统的路径(windows+linux)、xlsx文件转换为csv文件
  • 原文地址:https://blog.csdn.net/weixin_49405762/article/details/126431619