ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

20220819 第一组 于芮 JDBC(第三十六天)

2022-08-19 21:04:25  阅读:142  来源: 互联网

标签:第三十六 于芮 null preparedStatement sql balance 20220819 conn accountid


 

小白成长记——第三十六天

 

   今天延续了昨天的JDBC的学习,继续深入研究了JDBC,大部分时间还是在做案例,来看一下今天的学习笔记吧!

Statement的不足:
1、大量的字符串拼接,代码可读性降低。
2、sql注入 SQL注入:BUG
通过字符串的拼接,可以得到一个恒等的sql语句,可以跳过某些判断。
PreparedStatement:预编译(预加载)接口
1、通过conn获取的对象
2、是Statement接口的子接口
3、sql语句中可以传参。用?占位,通过setXXX方法来给?赋值
4、提高性能
5、避免sql注入
获取元数据
元数据:表格本身的数据, 表格的列名,结果集的列名
数据库事务:是数据库的特性 Mysql的数据库引擎
1.在MySQL中,只有使用了Innodb引擎的数据库才支持事务
2.事务处理可以用来维护数据的完整性。保证sql语句要么全部执行,
要么全部不执行。
3. 发生在DML中,增删改。 事务的四大特征ACID
1、原子性 A。
一个事务,要么全部完成,要么全部不完成。
2、一致性 C。
在事务开始之前和事务结束之后,数据库的完整性没有被破坏。
3、隔离性 Isolation
数据库允许多个事务同时对数据进行处理。每个事务之间是相互隔离。
4、持久性 D
事务结束以后,对数据的增删改是永久性的。
术语:提交事务,回滚事务(事务回滚) 1、事务一旦提交,就不可能回滚。
2、当一个连接对象被创建时,默认情况下自动提交事务。
3、关闭连接时,数据会自动提交事务。 操作事务的步骤:
1、关闭事务的自动提交
当做出增删改操作,把变化发生在内存中,提交事务,才会真正提交给数据库

   然后今天的案例是一个连接数据库的简易银行系统,通过连接数据库,在Java中执行SQL语句,完成开户,转账,存款,来看一下代码吧!

  1 package M0819.Account;
  2 
  3 import L0818.JDBDUtil;
  4 
  5 import java.sql.Connection;
  6 import java.sql.PreparedStatement;
  7 import java.sql.ResultSet;
  8 import java.sql.SQLException;
  9 
 10 public class accountDao {
 11     private static Connection conn;
 12 
 13     {
 14         try {
 15             conn = JDBDUtil.getConnection();
 16         } catch (Exception e) {
 17             throw new RuntimeException(e);
 18         }
 19     }
 20     //转账
 21     public static Integer transform(String out, String in, Double balance){
 22         ResultSet rs=null;
 23         PreparedStatement preparedStatement=null;
 24         PreparedStatement preparedStatement1=null;
 25         double b=0;
 26         String sql= "select balance from account1 where accountid = ?";
 27         try {
 28             preparedStatement=conn.prepareStatement(sql);
 29             preparedStatement.setString(1,out);
 30             rs=preparedStatement.executeQuery();
 31             while (rs.next()){
 32                 b=rs.getDouble("balance");
 33             }
 34             if(b>=balance){
 35                 //余额够
 36                 //执行修改
 37                 conn.setAutoCommit(false);
 38                 sql= "update account1 set balance = balance - ? where accountid = ?";
 39                 preparedStatement=conn.prepareStatement(sql);
 40                 preparedStatement.setDouble(1,balance);
 41                 preparedStatement.setString((int) 2,out);
 42                 int i=preparedStatement.executeUpdate();
 43                 sql="update bank set balance = balance + ? where accountid = ?";
 44                 preparedStatement1=conn.prepareStatement(sql);
 45                 preparedStatement1.setString((int)2,in);
 46                 i=preparedStatement1.executeUpdate();
 47                 conn.commit();
 48                 return  i;
 49             }else {
 50                 //余额不够
 51                 throw new RuntimeException("余额不足,转账失败");
 52             }
 53         } catch (SQLException e) {
 54             throw new RuntimeException(e);
 55         }finally {
 56             JDBDUtil.close(conn,preparedStatement,rs);
 57             JDBDUtil.close(null,preparedStatement1,null);
 58         }
 59     }
 60     //取款
 61     public Integer out(String accountid,Double balance)
 62     {
 63         //取款之前先查询
 64         ResultSet rs=null;
 65         PreparedStatement preparedStatement=null;
 66         double b=0;
 67         String sql="select balance from account1 where accountid = ?";
 68         try {
 69             preparedStatement=conn.prepareStatement(sql);
 70             preparedStatement.setString(1,accountid);
 71             rs=preparedStatement.executeQuery();
 72             while(rs.next()){
 73                 b=rs.getDouble("balance");
 74             }
 75             if(b>=balance){
 76                 //余额够,执行修改
 77                 sql="update account set balance = balance - ? where accountid = ?";
 78                 preparedStatement=conn.prepareStatement(sql);
 79                 preparedStatement.setDouble(1,balance);
 80                 preparedStatement.setString((int)2,accountid);
 81                 int i=preparedStatement.executeUpdate();
 82                 return  i;
 83             }else {
 84                 //余额不够
 85                 throw new RuntimeException("余额不足,无法转账");
 86             }
 87         } catch (SQLException e) {
 88             throw new RuntimeException(e);
 89         }
 90     }
 91     //存款
 92     public  Integer in(String accountid,Double balance){
 93         int i=0;
 94         String sql= "update account1 set balance = ? where accountid = ?";
 95         PreparedStatement preparedStatement=null;
 96         try {
 97             preparedStatement=conn.prepareStatement(sql);
 98             preparedStatement.setDouble(1,balance);
 99             preparedStatement.setString(2,accountid);
100             i=preparedStatement.executeUpdate();
101         } catch (SQLException e) {
102             throw new RuntimeException(e);
103         }finally {
104             JDBDUtil.close(conn,preparedStatement,null);
105         }
106         return i;
107     }
108     //开户
109     public Integer add(String accountid,Double balance){
110         int i=0;
111         String sql= "insert into account1 (accountid,balance) values (?,?)";
112         PreparedStatement preparedStatement=null;
113         try {
114             preparedStatement=conn.prepareStatement(sql);
115             preparedStatement.setString(1,accountid);
116             preparedStatement.setDouble(2,balance);
117             i=preparedStatement.executeUpdate();
118         } catch (SQLException e) {
119             throw new RuntimeException(e);
120         }finally {
121             JDBDUtil.close(conn,preparedStatement,null);
122         }
123         return  i;
124     }
125 }

 

标签:第三十六,于芮,null,preparedStatement,sql,balance,20220819,conn,accountid
来源: https://www.cnblogs.com/dijiuzu/p/16603276.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有