网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
2.是statement接口的子接口
3.sql语句中可以传参。用?来占位,通过setXXX方法来给?赋值
4.提高性能
✏️预编译
String sql = “update teacher set name = ? where id =?”
pstmt = conn.prepareStatement(sql);
给占位符赋值,根据位置
pstmt.setString(1,”jj”);
pstmt.setInt(2.6);
pstmt.exectuteUpdate();
要运行就给他返回值 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) {
![img](https://img-blog.csdnimg.cn/img_convert/804f37dfb55254545e2345e97011292c.png)
![img](https://img-blog.csdnimg.cn/img_convert/9c5cf47f71e6a0916faaee8e52dd3299.png)
**网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。**
**[需要这份系统化的资料的朋友,可以添加戳这里获取](https://bbs.csdn.net/topics/618668825)**
**一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**
如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。**
**[需要这份系统化的资料的朋友,可以添加戳这里获取](https://bbs.csdn.net/topics/618668825)**
**一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**