1.Statement对象用于执行静态SQL语句并返回其生成的结果的对象
2.在连接建立后,需要对数据库进行访问,执行命名或是SQL语句,可以通过Statement[存在SQL注入],PreparedStatement[预处理],CallableStatement[存储过程]
3.Statementj对象执行SQL语句,存在SQL注入风险
4.SQL注入是利用某些系统没有对用户输入的数据进行充分的检查,
而在用户输入数据中注入非法的SQL语句段或命令,恶意攻击数据库。sql injection.sql
5.要防范SQL注入,需要用PreparedStatement
-- 演示 sql 注入
-- 创建一张管理员表
CREATE TABLE admin (
`name` VARCHAR(32) NOT NULL UNIQUE,
pwd VARCHAR(32) NOT NULL DEFAULT '') CHARACTER SET utf8;
INSERT INTO admin VALUES('tom', '123');
-- 查找某个管理是否存在
SELECT *FROM admin WHERE NAME = 'tom' AND pwd = '123'
-- 输入用户名 为 1' or
-- 输入万能密码 为 or '1'= '1
SELECT *FROM admin WHERE NAME = '1' OR' AND pwd = 'OR '1'= '1'
PreparedStatement
1.PreparedStatement执行的SQL语句中的参数用问号(?)来表示,调用PreparedStatement对象的setXxx(0方法来设置这些参数.setXxx0方法有两个参数,第一个参数是要设置的SQL语句中的参数的索引(从1开始),第二个是设置的SQL语句中的参数的值
2.调用executeQuery0,返回ResultSet对象
3.调用executeUpdate(0:执行更新,包括增、删、修改
优点
1.不再使用+拼接sq语句,减少语法错误
2.有效的解决了sq注入问题!
3.大大减少了编译次数,效率较高
public class PreStatement {
public static void main(String[] args) throws Exception{
Scanner sc = new Scanner(System.in);
String name=sc.nextLine();
String pw = sc.nextLine();
Properties properties = new Properties();
properties.load(new FileInputStream("src\\mysql"));
String driver= properties.getProperty("driver");
String url=properties.getProperty("url");
String user=properties.getProperty("user");
String password=properties.getProperty("password");
Class.forName(driver);
Connection connection = DriverManager.getConnection(url, user, password);
String sql="select name , pwd from admin where name =? and pwd=?";
PreparedStatement ps = connection.prepareStatement(sql);
ps.setString(1,name);
ps.setString(2,pw);
//执行 select 语句使用 executeQuery
// 如果执行的是 dml(update, insert ,delete) executeUpdate()
// 这里执行 executeQuery
ResultSet resultSet = ps.executeQuery();
//如果查询到一条记录,则说明该管理存在
if (resultSet.next()){
System.out.println("登录成功");
}else {
System.out.println("登录失败");
}
resultSet.close();
ps.close();
connection.close();
}
}
在jdbc操作中,获取连接和释放资源是经常使用到可以将其封装DBC连接的工具类JDBCUtils
package JDBC.Tool;
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.*;
import java.util.Properties;
/**
* @author whlie(true){learn}
*/
public class JDBCUtils {
private static String user;
private static String password;
private static String url;
private static String driver;
/**
* 读取相关的属性值
*/
static {
try {
Properties properties = new Properties();
properties.load(new FileInputStream("src\\mysql"));
user = properties.getProperty("user");
password = properties.getProperty("password");
url = properties.getProperty("url");
driver = properties.getProperty("driver");
} catch (IOException e) {
//在实际开发中,我们可以这样处理
//1. 将编译异常转成 运行异常
//2. 调用者,可以选择捕获该异常,也可以选择默认处理该异常,比较方便.
throw new RuntimeException(e);
}
}
/**
* 连接数据库, 返回 Connection
*/
public static Connection getConnection() {
try {
return DriverManager.getConnection(url, user, password);
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
/**
* 关闭连接
* @param set 结果集
* @param statement
* @param connection
*/
public static void close(ResultSet set, Statement statement, Connection connection) {
try {
if (set != null) {
set.close();
}
if (statement != null) {
statement.close();
}
if (connection != null) {
connection.close();
}
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
使用工具类进行增加操作
public class Dml {
public static void main(String[] args){
Connection connection = JDBCUtils.getConnection();
String sql="insert into admin values(?,?)";
PreparedStatement ps=null;
try {
ps = connection.prepareStatement(sql);
ps.setString(1,"rick");
ps.setString(2,"123");
ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}finally {
JDBCUtils.close(null,ps,connection);
}
}
}
使用工具类进行select操作
@Test
public void select(){
Connection connection=null;
String sql="select *from admin";
PreparedStatement ps=null;
ResultSet set=null;
try {
connection=JDBCUtils.getConnection();
ps=connection.prepareStatement(sql);
set=ps.executeQuery();
while (set.next()){
String name = set.getString("name");
String pwd = set.getString("pwd");
System.out.println(name+"\t"+pwd);
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
JDBCUtils.close(set,ps,connection);
}
}