SQL注入
在采用拼接sql语句的时候,将用户提供的非法信息编译进去,导致原sql语句的含义被扭曲了。
String a="z";
String b="yyy'or'1'='1";
String sql="select * from login where user='"+a+"'and password='"+b+"'";
//处理查询结果集
rs=stmt.executeQuery(sql);
//遍历查询结果集
//rs.getString(2)返回查询结果集的第几列数据
while(rs.next()){
System.out.println(rs.getString(2));
}
解决SQL注入
获取数据库操作对象的部分发生改变
import java.sql.*;
public class jdbcTest02 {
public static void main(String[] args) {
Connection conn=null;
//Statement stmt=null;
PreparedStatement ps=null;
ResultSet rs=null;
//注册驱动的第一种方式
/*try {
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
} catch (SQLException throwables) {
throwables.printStackTrace();
}*/
//注册驱动的第二种方式
try {
Class.forName("com.mysql.jdbc.Driver");
//获取连接
/*jdbc:mysql:是网络协议
127.0.0.1是ip地址
3306是数据库的端口号
bjpowernode是具体的数据库名
*/
String url="jdbc:mysql://127.0.0.1:3306/bjpowernode";
String user="root";
String password="123456";
conn= DriverManager.getConnection(url,user,password);
System.out.println("数据库连接对象"+conn);
//获取数据库操作对象
//stmt=conn.createStatement();
String sql="select * from login where ser=? and pass=?";
ps=conn.prepareStatement(sql);
//执行sql语句
ps.setString(1,"z");
ps.setString(2,"yyy'or'1'='1");
/*String sql="insert into dept(deptno,dname,loc) values(50,'人事部','北京')";
//返回值count是”该sql语句影响数据库中的记录条数“
int count=stmt.executeUpdate(sql);
System.out.println(count==1?"保存成功":"保存失败");*/
/*String a="z";
String b="yyy'or'1'='1";
String sql="select * from login where user='"+a+"'and password='"+b+"'";*/
//处理查询结果集
rs=ps.executeQuery();
//遍历查询结果集
//rs.getString(2)返回查询结果集的第几列数据
if(rs.next()){
System.out.println("登录成功");
}else{
System.out.println("登陆失败");
}
} catch (Exception e) {
e.printStackTrace();
}finally {
if (rs != null) {
try {
rs.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if (ps != null) {
try {
ps.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
}
}
Statement和PrepareStatement的区别
Statement存在SQL注入问题,PrepareStatement解决了SQL注入问题
Statement是编译一次执行一次,PrepareStatement是编译一次,可执行N次
PrepareStatement效率高一些
PrepareStatement会在编译阶段做类型的安全检查。
sql语句一样的话,只需要编译一次。
事务控制
JDBC的事务是自动提交机制。
JDBC只要执行任意一条iDML语句,就会提交一次。
import java.sql.*;
public class jdbcTest03 {
public static void main(String[] args) {
Connection conn=null;
PreparedStatement ps=null;
ResultSet rs=null;
try {
//注册驱动
Class.forName("com.mysql.jdbc.Driver");
//获取连接
conn= DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/bjpowernode","root","123456");
//将自动提交机制修改为手动提交
conn.setAutoCommit(false);//开启事务
//获取预编译的数据库操作对象
String sql="update account set bal=?where actno=?";
ps=conn.prepareStatement(sql);
ps.setDouble(1,10000);
ps.setInt(2,001);
int count=ps.executeUpdate();
String s=null;
s.toString();
ps.setDouble(1,10000);
ps.setInt(2,002);
count+=ps.executeUpdate();
System.out.println(count==2?"转账成功 ":"转账失败");
conn.commit();//提交事务
} catch (Exception e) {
//回滚事务
if (conn != null) {
try {
conn.rollback();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
e.printStackTrace();
}finally {
if (ps != null) {
try {
ps.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
}
}