JDBC深入学习
JDBC深入学习
一、Sql注入问题?
sql注入的根本原是用户输入的信息中含有sql语句的关键字,并且这些关键字参与sql语句的编译过程,导致sql语句的原意被扭曲,进而达到sql注入。
二、解决sql注入的问题
- 只要用户提供的信息不参与sql语句的编译过程,问题就解决了
- 即使用户提供的信息中含有SQL语句的关键字,但是没有参与编译,不起作用
- 要想用户信息不参与sql语句的编译,那么必须使用8java.sql.PreparedStatement
- PreparedStatement接口继承了java.sql.Statement
- PreparedStatement是属于预编译的数据库操作对象。
- PreparedStatement的原理是:预先对sql语句的框架进行编译,然后再给sql语句传”值“。
三、解决sql注入的关键是什么?
用户提供的信息中即使含有SQL关键字,但是这些关键字并没有参与编译,不起作用。
四、对比一下Statement与PreparedStatement?
- Statement存在sql注入问题,PreparedStatement不存在SQL注入问题。
- Statement编译一次执行一次,PreparedStatement是编译一次,可执行n次,PreparedStatement的效率更高
- PreparedStatement会在编译阶段做类型安全检查
综上所述:PreparedStatement使用较多,只有在极少数情况下使用statement
五、什么情况下使用statement呢?
业务方面必须要求SQL注入的时候
Statement支持sql注入,凡是业务方面要求需要进行SQL语句拼接,必须使用Statement。
演示statement作用
先贴图
这是使用PreparedStatement错
PreparedStatement代码展示
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("输入desc或asc,desc就是降序,asc就是升序!");
System.out.println("请输入:");
String keyWords = s.nextLine();
//使用资源绑定器绑定属性与配置文件
ResourceBundle bundle = ResourceBundle.getBundle("jdbc");
String driver = bundle.getString("driver");
String url = bundle.getString("url");
String user = bundle.getString("user");
String password = bundle.getString("password");
//执行SQL
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
//1、注册驱动
Class.forName(driver);
//2、获取连接
conn = DriverManager.getConnection(url, user, password);
//3、获取预编译数据库操作对象
String sql = "select name from t_user order by name ?";
ps = conn.prepareStatement(sql);
ps.setString(1,keyWords);
//4、执行sql语句
rs = ps.executeQuery();
//5、处理查询语句结果集
while (rs.next()) {
System.out.println(rs.getString("name"));
}
} catch (Exception e) {
e.printStackTrace();
} finally {
//6、关闭资源
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
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("输入desc或asc,desc就是降序,asc就是升序!");
System.out.println("请输入:");
String keyWords = s.nextLine();
//使用资源绑定器绑定属性与配置文件
ResourceBundle bundle = ResourceBundle.getBundle("jdbc");
String driver = bundle.getString("driver");
String url = bundle.getString("url");
String user = bundle.getString("user");
String password = bundle.getString("password");
//执行SQL
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
//1、注册驱动
Class.forName(driver);
//2、获取连接
conn = DriverManager.getConnection(url, user, password);
//3、获取预编译数据库操作对象
stmt = conn.createStatement();
//4、执行sql语句
String sql = "select uname from t_user order by uname " + keyWords;
rs = stmt.executeQuery(sql);
//5、处理查询语句结果集
while (rs.next()) {
System.out.println(rs.getString("uname"));
}
} catch (Exception e) {
e.printStackTrace();
} finally {
//6、关闭资源
if (rs != null) {
try {
rs.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if (stmt == null) {
try {
stmt.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
}
得出结果
六、PreparedStatement实现增删改
import java.sql.*;
import java.util.ResourceBundle;
/**
* PreparedStatement实现增删改
*/
public class JdbcStudy6 {
public static void main(String[] args) {
//使用资源绑定器绑定属性与配置文件
ResourceBundle bundle = ResourceBundle.getBundle("jdbc");
String driver = bundle.getString("driver");
String url = bundle.getString("url");
String user = bundle.getString("user");
String password = bundle.getString("password");
//执行SQL
Connection conn = null;
PreparedStatement ps = null;
try {
//1、注册驱动
Class.forName(driver);
//2、获取连接
conn = DriverManager.getConnection(url,user,password);
//3、获取预编译的数据库操作对象
/* String sql = "insert into t_roles(ro_id,ro_name) values(?,?)";
ps = conn.prepareStatement(sql);
ps.setInt(1,5);
ps.setString(2,"用户");*/
/* String sql = "update t_roles set ro_name = ? where ro_id = ?";
ps = conn.prepareStatement(sql);
ps.setString(1,"管理员");
ps.setInt(2,5);*/
String sql = "delete from t_roles where ro_id = ?";
ps = conn.prepareStatement(sql);
ps.setInt(1,5);
//4、执行sql
int count = ps.executeUpdate();
System.out.println(count);
//5、处理查询语句结果集
} catch (Exception e) {
e.printStackTrace();
}finally {
//6、关闭资源
if (ps != null) {
try {
ps.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
}
}

浙公网安备 33010602011771号