目录
Mysql--preparedStatement(预编译)
我们之前用的Statement对象向数据库发送要执行的语句,但是Statement有很多的不足:
1.大量的字符串拼接,代码可读性降低。
2.sql注入:
SQL注入有一个BUG:
就是通过字符串的拼接,可以得到一个恒等的sql语句,可以跳过某些判断:
( "b' or '1' = '1" )--------这个语句返回值一定是true,所以可以通过任何的验证
例如:
这样无论用户名是什么都可以登陆成功。
public static void main(String[] args) {
login("zxcvzxcvzxcv","b' or '1' = '1");
}
public static void login(String username,String password) {
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
conn = JDBCUtil.getConnection();
stmt = conn.createStatement();
String sql = "select * from user where username = '"
+ username + "' and password = '" + password + "'";
System.out.println("sql:" + sql);
rs = stmt.executeQuery(sql);
if(rs.next()){
System.out.println("登录成功,欢迎回来:" + username);
}else {
System.out.println("账号或密码错误!");
}
} catch (SQLException e) {
throw new RuntimeException(e);
} finally {
JDBCUtil.close(conn,stmt,rs);
}
}
PreparedStatement(预编译(预加载)接口)
预编译语句PreparedStatement 是java.sql中的一个接口,它是Statement的子接口。通过Statement对象执行SQL语句时,需要将SQL语句发送给DBMS,由DBMS首先进行编译后再执行。预编译语句和Statement不同,在创建PreparedStatement 对象时就指定了SQL语句,该语句立即发送给DBMS进行编译。当该编译语句被执行时,DBMS直接运行编译后的SQL语句,而不需要像其他SQL语句那样首先将其编译。
使用:
1.通过connection获取的对象
2.是Statement接口的子接口
3.sql语句中可以传参。用?占位,通过setXXX方法来给?赋值
eg:setString(索引,值);
4.提高性能
5.避免sql注入
例子:
public void test03() {
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
conn = JDBCUtil.getConnection();
String sql = "select * from user where username = ? and password = ?";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1,"aaa");
pstmt.setString(2,"b' or '1' = '1");
rs = pstmt.executeQuery();
if(rs.next()) {
System.out.println("登录成功...");
}else {
System.out.println("账号或密码错误...");
}
}catch (SQLException e) {
throw new RuntimeException(e);
}finally {
JDBCUtil.close(conn,pstmt,rs);
}
}
Mysql--数据库事务
数据库事务就是数据库的特性
Msyql的数据库引擎:
1.在MySQL中,只有使用了Innodb引擎的数据库才支持事务
2.事务处理可以用来维护数据的完整性。保证sql语句要么全部执行,要么全部不执行。
3. 发生在DML中,增删改。
事务的四大特征ACID:
1、原子性A(Atomicity):一个事务,要么全部完成,要么全部不完成。
2、一致性C(Consistency): 在事务开始之前和事务结束之后,数据库的完整性没有被破坏。
3、隔离性I(Isolation):数据库允许多个事务同时对数据进行处理。每个事务之间是相互隔离,不受其他事务干扰。
4、持久性D(Durability):事务结束以后,对数据的增删改是永久性的,系统必须保证该事务对数据库的改变不会丢失,并且数据库出现故障。
术语:提交事务,回滚事务(事务回滚)
1.事务一旦提交,就不可能回滚
2.当一个连接对象被创建时,默认情况下自动提交事务。
3.关闭连接时,数据会自动提交事务。
操作事务的步骤:
1.关闭事务的自动提交:true:开启(默认) false:关闭
当做出增删改操作,把变化发生在内存中,提交事务,才会真正提交给数据库。
开启一个事务:
connection对象.setAutoCommit(false);
提交事务:
connection对象.commit();
事务回滚:
connection对象.rollback();
eg:
public void test01() {
Connection conn = null;
PreparedStatement pstmt1 = null;
PreparedStatement pstmt2 = null;
try {
conn = JDBCUtil.getConnection();
// 关闭事务的自动提交
// true:开启(默认) false:关闭
// 开启一个事务
conn.setAutoCommit(false);
// 把id为1的账户余额-1000
String sql1 = "update bank set balance = balance - 1000 where id = 1";
pstmt1 = conn.prepareStatement(sql1);
pstmt1.executeUpdate();
String sql2 = "update bank set balance = balance + 1000 where id = 2";
pstmt2 = conn.prepareStatement(sql2);
pstmt2.executeUpdate();
int i = 10 / 0;
// 提交事务
conn.commit();
System.out.println("转账成功...");
} catch (Exception e) {
try {
// 事务回滚
conn.rollback();
} catch (SQLException ex) {
throw new RuntimeException(ex);
}
throw new RuntimeException(e);
} finally {
JDBCUtil.close(conn,pstmt1);
JDBCUtil.close(null,pstmt2);
}
}
总结
今天学了编译的进阶版预编译,还有事务,虽然没太听懂,但是老师通过一个银行转账程序将代码用生活实例生动地展示了出来,然而依旧没弄懂